mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-26 22:57:32 +00:00
fix(pwa): recover from a dead chunk and a broken precache after an update (#2524)
After an update, a tab or a device whose service worker still serves the previous build asks for chunks the update removed. The server answered those requests with index.html and a 200, so the import failed with a MIME error, and everything caching in between was free to keep the HTML as the chunk. A precache install that met a missing file stored it, and Workbox reuses hashed entries by URL, so the device showed "A new version is available" on every load, whatever the reload or the cleared browser cache, and carried the broken entry into every later release that keeps the file's name. The SPA fallback now answers a GET for a build file that is not on disk with a plain 404 (no-store) instead of index.html. A precache install refuses it, so the worker whose cache is complete stays in charge until the new build is. index.html is sent relative to the public dir, which also stops an install living under a dot directory from answering every deep link with a 404. On the client, the reload after a dead chunk (the automatic one and the button on the update screen) no longer goes straight back to the worker that served the broken shell. When that same worker is still in charge and the server answers, its precache and registration are dropped first, so the reload reaches the build the server runs now. Offline, and in offline mode, the shell is kept. Every recovery reload, and the reload of the version handover, asks for the page under a one-off query parameter, so a proxy or CDN that keeps index.html against its headers cannot hand back the stale shell either. A chunk the entry imports statically fails before any of that code runs. shell-guard.js, a small classic script loaded ahead of the entry, watches for exactly that failure and runs the same recovery, once per session. A stylesheet, font or icon that holds a page raises no error at all, so once the app has started and the browser is idle, it looks through the precache for entries that hold a page under a build file's name, fetches each from the server past every cache and puts it back in place. A repaired stylesheet is loaded again, and so are the app's stylesheets when a font was repaired, since the browser does not try a face that failed again in the same document. Only the headers are read, a precache found sound is not looked at again until its entries change, nothing is fetched while nothing is broken, and nothing happens offline, in offline mode or without storage. An answer that is not the file (an auth wall, its login page, a server error) leaves the entry for the next launch. A page the recovery loaded from the network already runs the version the server reports, so the version handover now records it instead of reloading it a second time once the new worker takes over. A page loaded past an older worker that is still in charge, as a hard reload does, still asks for the new worker and records the version only once that has taken over, so the next tab or launch cannot get the old shell without a handover. Dropping the worker ends a Web Push subscription with it, so on such a device the settings card shows push as off until it is switched on again. A missing sw-push.js now fails the worker install with a 404 rather than index.html; the comments that described the old answer say so.
This commit is contained in:
@@ -17,25 +17,20 @@
|
||||
* version and session.
|
||||
*/
|
||||
|
||||
import { reloadFresh } from './chunkReload'
|
||||
import { currentController, servingWorker } from './serviceWorkerShell'
|
||||
|
||||
const VERSION_KEY = 'trek_app_version'
|
||||
const RELOAD_KEY = 'trek_app_version_reload'
|
||||
|
||||
/** The worker controlling this page, or null. Reading it can throw where site data is blocked. */
|
||||
function currentController(): ServiceWorker | null {
|
||||
try {
|
||||
return 'serviceWorker' in navigator ? navigator.serviceWorker.controller : null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
// The version this bundle was built as (vite.config.js). A bare tsc run has none,
|
||||
// and then no version counts as this bundle's own.
|
||||
const BUNDLE_VERSION: string = typeof __TREK_UI_VERSION__ === 'string' ? __TREK_UI_VERSION__ : ''
|
||||
|
||||
// The worker that served this page, noted when the app loads. The browser checks
|
||||
// sw.js on every navigation by itself, and when that check installs the new build
|
||||
// before the server has told us its version, the takeover is over before anyone
|
||||
// listens for it. A controller other than this one means the new shell is only a
|
||||
// reload away. A page served without a worker came from the network, so a worker
|
||||
// claiming it later is no takeover.
|
||||
const servedBy = currentController()
|
||||
// The worker that served this page, noted when the app loads (serviceWorkerShell).
|
||||
// When the browser's own check of sw.js installs the new build before the server
|
||||
// has told us its version, the takeover is over before anyone listens for it.
|
||||
const servedBy = servingWorker()
|
||||
|
||||
function markApplied(version: string): void {
|
||||
try { localStorage.setItem(VERSION_KEY, version) } catch { /* site data blocked */ }
|
||||
@@ -54,10 +49,18 @@ function reloadOnce(version: string): void {
|
||||
} catch {
|
||||
return
|
||||
}
|
||||
window.location.reload()
|
||||
// Under a URL no cache has seen yet: without a worker, a proxy that keeps
|
||||
// index.html against its headers would answer a plain reload with the very
|
||||
// shell this page runs, and the marker has moved on already (#2524).
|
||||
reloadFresh()
|
||||
}
|
||||
|
||||
async function handOver(reg: ServiceWorkerRegistration, version: string): Promise<void> {
|
||||
/**
|
||||
* Asks the worker for the new build and moves the marker once it has taken over.
|
||||
* `runsIt` is a page that already runs the new bundle, so the takeover needs no
|
||||
* reload to show it.
|
||||
*/
|
||||
async function handOver(reg: ServiceWorkerRegistration, version: string, runsIt: boolean): Promise<void> {
|
||||
// Listening before asking for the update, so a quick takeover is not missed.
|
||||
// Giving up disarms the listener: the reload belongs to this launch, and one
|
||||
// arriving later in the session would hit the user in the middle of an edit.
|
||||
@@ -65,7 +68,7 @@ async function handOver(reg: ServiceWorkerRegistration, version: string): Promis
|
||||
navigator.serviceWorker.addEventListener('controllerchange', () => {
|
||||
if (!armed) return
|
||||
markApplied(version)
|
||||
reloadOnce(version)
|
||||
if (!runsIt) reloadOnce(version)
|
||||
}, { once: true })
|
||||
|
||||
try {
|
||||
@@ -80,7 +83,7 @@ async function handOver(reg: ServiceWorkerRegistration, version: string): Promis
|
||||
// sw.js is unchanged, so the release changed nothing the precache holds and
|
||||
// the bundle this page runs is the current one.
|
||||
armed = false
|
||||
if (currentController() === servedBy) markApplied(version)
|
||||
if (runsIt || currentController() === servedBy) markApplied(version)
|
||||
return
|
||||
}
|
||||
// Precaching is all or nothing: one failed request out of several hundred and
|
||||
@@ -129,14 +132,31 @@ export async function reconcileAppVersion(reported: unknown): Promise<void> {
|
||||
// Left for the next launch. A reload now would only reach the old worker again.
|
||||
return
|
||||
}
|
||||
// Without a worker the page came from the network and runs the new bundle
|
||||
// already, so the reload only confirms it. A controller other than the one
|
||||
// that served the page means the browser finished the handover by itself,
|
||||
// and the reload is what shows the new build.
|
||||
|
||||
// A page that came from the network and was built as this version runs it
|
||||
// already. That is where the way out of a broken app shell lands, before the
|
||||
// new app could move the marker (#2524): the old worker is gone, and the one
|
||||
// registered since is this build. Reloading once it has taken over would only
|
||||
// interrupt the user a few seconds in, with nothing new to show. An older
|
||||
// worker still in charge (this page was loaded past it, as a hard reload
|
||||
// does) would serve its own build to the next tab or launch, so it is handed
|
||||
// over like any other, just without the reload.
|
||||
const runsIt = !servedBy && version === BUNDLE_VERSION
|
||||
if (runsIt && (!reg?.active || reg.active === currentController())) {
|
||||
markApplied(version)
|
||||
return
|
||||
}
|
||||
|
||||
// Without a worker the page came from the network, yet was not built as this
|
||||
// version: a proxy or CDN handed out an index.html it kept, or the server
|
||||
// reports a version its bundle was not built as, and then the reload only
|
||||
// confirms what runs. A controller other than the one that served the page
|
||||
// means the browser finished the handover by itself, and the reload is what
|
||||
// shows the new build.
|
||||
if (!reg || (servedBy && currentController() !== servedBy)) {
|
||||
markApplied(version)
|
||||
reloadOnce(version)
|
||||
return
|
||||
}
|
||||
await handOver(reg, version)
|
||||
await handOver(reg, version, runsIt)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user