From 525e69b976a569bf2143adff788f82d68cefa129 Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 4 Nov 2026 11:01:05 +0800 Subject: [PATCH] gui: a page served anew reloads once, not once per read (visamzh on X) 667317159's servedAnew reloads a magpie web page once another version of magpie answers for it. Clicked back into on the Settings page, the page reads /api/update twice, for the badge and for the Version row, and each answer called location.reload(). When the second answer comes a few ms after the first, Chromium starts the reload again and the page is loaded twice. The whole GUI suite at 667317159 on the Linux box caught it: update-served-anew's "en at 420" and "zh at 420" counted 3 loads (gui-full.log). servedAnew now reloads once per page. The test answers the second post-update read 5 ms after the first, as a busy machine does, and counts the loads after the page has settled. app.js's comments on it are shorter, so app.js is 264 bytes smaller than origin/main's; TestCoreScriptBudget is red on main already (1534819 > 1533802, #5139) and stays red, 264 bytes less over. Run (Linux box, magpie-desktop, through wsl-run, at e17b6cefc + this): - node --test internal/gui/tests/update-served-anew.test.cjs three times: 34/34 each (Chromium and WebKit, 5 languages, 1100 and 420). - go test -tags nogui -run 'Budget|Script|Asset|I18n|Boot' ./internal/gui/: only TestCoreScriptBudget red, the same at origin/main e17b6cefc alone (#5139). Broken by hand: servedAnew calling location.reload() on every answer again: the 10 Chromium "clicked back into" subtests red (3 loads). WebKit passes either way; it doesn't start a second reload. Not tried: the whole GUI suite again with this; mote (busy with other sessions' suites). --- internal/gui/assets/app.js | 17 ++++++----------- internal/gui/tests/update-served-anew.test.cjs | 12 +++++++----- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 9278261fe..14afa0ce6 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -4545,16 +4545,13 @@ async function backAsNew(was) { } } -// servedAnew reloads a browser's page (magpie web, or the app's page) -// that another version of magpie now answers for, and says whether it did. -// The page restarted to update itself reloads in backAsNew; one whose -// magpie restarted for another's update (the app's window put it in, and -// magpie web followed, or the app restarted under its own page) ran on as -// the old page, its Update pill still offering the version already in -// (visamzh on X). +// servedAnew reloads, once, a browser's page that another version of +// magpie now answers for (visamzh on X): its magpie restarted into an +// update put in elsewhere. The badge and the Version row both ask. +let reloadingAnew = false; function servedAnew(u) { if (!web || !pageVersion || !u?.current || u.current === pageVersion) return false; - location.reload(); + if (!reloadingAnew) { reloadingAnew = true; location.reload(); } return true; } @@ -24136,9 +24133,7 @@ setInterval(async () => { } window.addEventListener("focus", () => loadBack()); setInterval(renderUpdateBadge, 15 * 60 * 1000); // a window left open still hears of a new version -// a browser's page on screen hears within a minute that its magpie -// restarted into an update put in elsewhere (servedAnew), without a click -// into it first +// a browser's page on screen hears of it within a minute (servedAnew) if (web) setInterval(() => { if (!document.hidden) renderUpdateBadge(); }, 60e3); // renderPluginDot puts a dot on Plugins while a plugin's update waits for // the reader (someone else's plugin, or one pinned; the community's update diff --git a/internal/gui/tests/update-served-anew.test.cjs b/internal/gui/tests/update-served-anew.test.cjs index e302d20ba..c09e0e8aa 100644 --- a/internal/gui/tests/update-served-anew.test.cjs +++ b/internal/gui/tests/update-served-anew.test.cjs @@ -40,6 +40,9 @@ function server(lang, web, ctl) { if (url.pathname === "/api/state") return json({ agents: [], profiles: [], settings: settingsPayload(lang), fx: settingsPayload(lang).fx }); if (url.pathname === "/api/settings") return json(settingsPayload(lang)); if (url.pathname === "/api/update") { + // the badge's and the Version row's reads answer a few ms apart, as + // on a busy machine: a page that reloads for each one is loaded twice + if (ctl.version !== "0.1.400" && ctl.reads++) await new Promise((r) => setTimeout(r, 5)); return json(ctl.version === "0.1.400" ? { state: "ready", current: "0.1.400", latest: "0.1.401" } : { state: "latest", current: "0.1.401", latest: "0.1.401" }); @@ -76,23 +79,22 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", t.after(() => browser.close()); for (const width of [1100, 420]) for (const lang of langs) { await t.test(`${lang} at ${width}: clicked back into`, async () => { - const ctl = { version: "0.1.400", pages: 0 }; + const ctl = { version: "0.1.400", pages: 0, reads: 0 }; const { page, errors } = await open(browser, width, lang, true, ctl); // the app's window put 0.1.401 in, and magpie web restarted into it ctl.version = "0.1.401"; await page.evaluate(() => window.dispatchEvent(new Event("focus"))); await page.waitForFunction(() => !window.oldPage && document.readyState === "complete"); - assert.equal(ctl.pages, 2, "loaded again once"); await page.waitForTimeout(400); assert.equal(await pill(page).isHidden(), true, "the reloaded page offers no update"); - assert.equal(ctl.pages, 2, "no reload after the first"); + assert.equal(ctl.pages, 2, "loaded again once"); assert.deepEqual(errors, []); await page.context().close(); }); } for (const lang of langs) { await t.test(`${lang}: left on screen, within a minute`, async () => { - const ctl = { version: "0.1.400", pages: 0 }; + const ctl = { version: "0.1.400", pages: 0, reads: 0 }; const { page, errors } = await open(browser, 1100, lang, true, ctl, true); ctl.version = "0.1.401"; await page.clock.runFor(61e3); @@ -103,7 +105,7 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", }); } await t.test("the app's own window doesn't reload", async () => { - const ctl = { version: "0.1.400", pages: 0 }; + const ctl = { version: "0.1.400", pages: 0, reads: 0 }; const { page, errors } = await open(browser, 1100, "en", false, ctl); ctl.version = "0.1.401"; await page.evaluate(() => window.dispatchEvent(new Event("focus")));