From 4359bedde42a26ff38121af978e100f93c926fd5 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Tue, 1 Dec 2026 19:10:49 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Reload=20the=20page=20when=20a?= =?UTF-8?q?=20new=20service=20worker=20takes=20over?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The worker is registered with autoUpdate and claims its clients, so after a deploy the new worker took the page over while the old page kept running with its lazy route chunks purged from the cache: navigating then failed until the user reloaded by hand. Once a worker controls the page, a controllerchange now reloads it, one time per load. The check that a controller already exists keeps the first visit from reloading, where the brand new worker claims the page right after registering. The block runs in production only, so tests never touch it. --- web/src/routes/+layout.svelte | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/web/src/routes/+layout.svelte b/web/src/routes/+layout.svelte index 780f151..0444136 100644 --- a/web/src/routes/+layout.svelte +++ b/web/src/routes/+layout.svelte @@ -38,7 +38,10 @@ onMount(() => { void resolveGate().then((g) => (gate = g)); if (import.meta.env.PROD && 'serviceWorker' in navigator) { - navigator.serviceWorker.register('/sw.js', { scope: '/' }).catch(() => {}); + const sw = navigator.serviceWorker; + // A new worker takes the page over once it is active; its lazy route chunks are gone by then, so load the page anew. + if (sw.controller) sw.addEventListener('controllerchange', () => location.reload(), { once: true }); + void sw.register('/sw.js', { scope: '/' }).catch(() => {}); } const tickInterval = setInterval(() => tick++, 15_000); return () => clearInterval(tickInterval);