mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 15:17:30 +00:00
fix(client): the version notice asks about a change during the session only
Three gaps in the notice from the previous commit, found in review. A page served from the network and claimed later by its own build's worker counted as already taken over, so Reload went to that worker, got the old build again and spent the session's reload: no notice and no new build until the next start. Only a worker that replaced the one that served the page skips the handover now, as at launch. A reload that came before the new worker could take over (a slow precache, a failed update, an install gone redundant) also spent that guard, so the launch behind it could no longer finish the handover with its own reload. That reload leaves the guard alone now. An install whose server never reports the bundle's version (a hand-set APP_VERSION) showed the notice in every session, and Reload could not clear it. The store keeps the first version a page hears of, from the app config at launch, and the notice only asks when a later report differs: a different build at launch is the launch handover's to fix. The server's wire-envelope test pins the welcome frame's fields and now expects the version beside the socket id. The wiki's update page says what an open tab shows after an update.
This commit is contained in:
@@ -517,16 +517,19 @@ describe('offersNewBuild: whether the notice offers a reload', () => {
|
||||
})
|
||||
|
||||
describe('switchToServerVersion: the reload the user asked for', () => {
|
||||
const guard = () => sessionStorage.getItem('trek_app_version_reload')
|
||||
|
||||
afterEach(() => {
|
||||
sessionStorage.removeItem('trek_app_version_reload')
|
||||
vi.useRealTimers()
|
||||
})
|
||||
|
||||
it('FE-UTIL-VERSION-050: without a worker it reloads at once, under a fresh URL', async () => {
|
||||
it('FE-UTIL-VERSION-050: without a worker it reloads at once, under a fresh URL, and spends the guard', async () => {
|
||||
const { switchToServerVersion } = await loadModule()
|
||||
await switchToServerVersion('4.3.4')
|
||||
expect(reload).toHaveBeenCalledTimes(1)
|
||||
expect(plainReload).not.toHaveBeenCalled()
|
||||
expect(guard()).toBe('4.3.4')
|
||||
})
|
||||
|
||||
it('FE-UTIL-VERSION-051: waits for the new worker to take over, records the version, then reloads', async () => {
|
||||
@@ -543,6 +546,7 @@ describe('switchToServerVersion: the reload the user asked for', () => {
|
||||
await switching
|
||||
expect(marker()).toBe('4.3.4')
|
||||
expect(reload).toHaveBeenCalledTimes(1)
|
||||
expect(guard()).toBe('4.3.4')
|
||||
})
|
||||
|
||||
it('FE-UTIL-VERSION-052: an unchanged sw.js has nothing to wait for, so it reloads and leaves the marker', async () => {
|
||||
@@ -554,9 +558,10 @@ describe('switchToServerVersion: the reload the user asked for', () => {
|
||||
expect(registration.update).toHaveBeenCalled()
|
||||
expect(marker()).toBe('4.3.3')
|
||||
expect(reload).toHaveBeenCalledTimes(1)
|
||||
expect(guard()).toBe('4.3.4')
|
||||
})
|
||||
|
||||
it('FE-UTIL-VERSION-053: an install that goes redundant still ends in the reload', async () => {
|
||||
it('FE-UTIL-VERSION-053: an install that goes redundant still reloads, but leaves the guard to the next launch', async () => {
|
||||
const { registration } = installWorker()
|
||||
const incoming = updateInstalls(registration)
|
||||
const { switchToServerVersion } = await loadModule()
|
||||
@@ -566,6 +571,8 @@ describe('switchToServerVersion: the reload the user asked for', () => {
|
||||
incoming.become('redundant')
|
||||
await switching
|
||||
expect(reload).toHaveBeenCalledTimes(1)
|
||||
// The launch on the other side retries the handover and reloads by itself.
|
||||
expect(guard()).toBeNull()
|
||||
})
|
||||
|
||||
it('FE-UTIL-VERSION-054: a failed update or a worker that never takes over does not leave the user waiting', async () => {
|
||||
@@ -574,8 +581,8 @@ describe('switchToServerVersion: the reload the user asked for', () => {
|
||||
let { switchToServerVersion } = await loadModule()
|
||||
await switchToServerVersion('4.3.4')
|
||||
expect(reload).toHaveBeenCalledTimes(1)
|
||||
expect(guard()).toBeNull()
|
||||
|
||||
sessionStorage.removeItem('trek_app_version_reload')
|
||||
const stuck = installWorker()
|
||||
updateInstalls(stuck.registration)
|
||||
;({ switchToServerVersion } = await loadModule())
|
||||
@@ -584,9 +591,10 @@ describe('switchToServerVersion: the reload the user asked for', () => {
|
||||
await vi.advanceTimersByTimeAsync(20_000)
|
||||
await switching
|
||||
expect(reload).toHaveBeenCalledTimes(2)
|
||||
expect(guard()).toBeNull()
|
||||
})
|
||||
|
||||
it('FE-UTIL-VERSION-055: a worker the browser swapped in already answers the reload, so nothing is asked of it', async () => {
|
||||
it('FE-UTIL-VERSION-055: a worker that replaced the one that served the page already answers the reload', async () => {
|
||||
const { container, registration } = installWorker()
|
||||
const { switchToServerVersion } = await loadModule()
|
||||
container.controller = new FakeWorker('activated')
|
||||
@@ -596,6 +604,25 @@ describe('switchToServerVersion: the reload the user asked for', () => {
|
||||
expect(reload).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('FE-UTIL-VERSION-057: a worker that only claimed a page from the network holds this build, so the handover still runs', async () => {
|
||||
const { container, registration } = installWorker(null)
|
||||
const { switchToServerVersion } = await loadModule()
|
||||
// registerSW installed this build's worker after the page loaded, and it claimed the page.
|
||||
const claimed = new FakeWorker('activated')
|
||||
container.controller = claimed
|
||||
registration.active = claimed
|
||||
updateInstalls(registration)
|
||||
|
||||
const switching = switchToServerVersion('4.3.4')
|
||||
await vi.waitFor(() => expect(registration.update).toHaveBeenCalled())
|
||||
expect(reload).not.toHaveBeenCalled()
|
||||
|
||||
container.takeOver()
|
||||
await switching
|
||||
expect(marker()).toBe('4.3.4')
|
||||
expect(reload).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('FE-UTIL-VERSION-056: a registration lookup that throws still reloads', async () => {
|
||||
const { container } = installWorker()
|
||||
container.getRegistration.mockRejectedValue(new Error('SecurityError'))
|
||||
|
||||
@@ -128,9 +128,14 @@ const SWITCH_WAIT_MS = 20_000
|
||||
* The user asked for the build the server runs now, from the notice a release
|
||||
* deployed during the session raises. The same handover as a launch, but the
|
||||
* reload is theirs to ask for, so it always comes: once the new worker has
|
||||
* taken over, or when there is none to wait for, or after SWITCH_WAIT_MS. The
|
||||
* marker only moves on a takeover; anything else is left to the next launch,
|
||||
* as there.
|
||||
* taken over, or when there is none to wait for, or after SWITCH_WAIT_MS.
|
||||
*
|
||||
* Only a reload that should land on the new build spends the session's reload
|
||||
* guard: after a takeover, without a worker, or with an unchanged sw.js. One
|
||||
* that comes before the new worker could take over (a slow precache, a failed
|
||||
* update) leaves it free, so the launch on the other side finishes the
|
||||
* handover and reloads by itself, as any launch does. The marker only moves on
|
||||
* a takeover.
|
||||
*/
|
||||
export async function switchToServerVersion(version: string): Promise<void> {
|
||||
let reg: ServiceWorkerRegistration | undefined
|
||||
@@ -139,34 +144,46 @@ export async function switchToServerVersion(version: string): Promise<void> {
|
||||
} catch {
|
||||
reg = undefined
|
||||
}
|
||||
// Without a worker the reload goes to the network. A worker other than the
|
||||
// one that served this page has taken over already, and answers it with the
|
||||
// new build.
|
||||
if (reg && currentController() === servedBy) await waitForTakeover(reg, version)
|
||||
reloadOnce(version)
|
||||
// Without a worker the reload goes to the network. A worker that replaced the
|
||||
// one that served this page answers it with the new build already; one that
|
||||
// only claimed a page served from the network holds this page's own build.
|
||||
const replaced = servedBy !== null && currentController() !== servedBy
|
||||
if (!reg || replaced) {
|
||||
reloadOnce(version)
|
||||
return
|
||||
}
|
||||
const outcome = await waitForTakeover(reg, version)
|
||||
if (outcome === 'stalled') reloadFresh()
|
||||
else reloadOnce(version)
|
||||
}
|
||||
|
||||
function waitForTakeover(reg: ServiceWorkerRegistration, version: string): Promise<void> {
|
||||
/** How a requested handover ended: the new worker took over, there was none, or it did not get there. */
|
||||
type Takeover = 'took-over' | 'unchanged' | 'stalled'
|
||||
|
||||
function waitForTakeover(reg: ServiceWorkerRegistration, version: string): Promise<Takeover> {
|
||||
return new Promise(resolve => {
|
||||
const done = (): void => {
|
||||
let settled = false
|
||||
const done = (outcome: Takeover): void => {
|
||||
if (settled) return
|
||||
settled = true
|
||||
clearTimeout(timer)
|
||||
resolve()
|
||||
resolve(outcome)
|
||||
}
|
||||
const timer = setTimeout(done, SWITCH_WAIT_MS)
|
||||
const timer = setTimeout(() => done('stalled'), SWITCH_WAIT_MS)
|
||||
navigator.serviceWorker.addEventListener('controllerchange', () => {
|
||||
markApplied(version)
|
||||
done()
|
||||
done('took-over')
|
||||
}, { once: true })
|
||||
reg.update().then(() => {
|
||||
const incoming = reg.installing ?? reg.waiting
|
||||
if (!incoming) {
|
||||
done()
|
||||
done('unchanged')
|
||||
return
|
||||
}
|
||||
incoming.addEventListener('statechange', () => {
|
||||
if (incoming.state === 'redundant') done()
|
||||
if (incoming.state === 'redundant') done('stalled')
|
||||
})
|
||||
}, done)
|
||||
}, () => done('stalled'))
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user