diff --git a/.changeset/unlucky-cars-swim.md b/.changeset/unlucky-cars-swim.md new file mode 100644 index 000000000000..119db1bb464b --- /dev/null +++ b/.changeset/unlucky-cars-swim.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': minor +--- + +feat: update service worker when new version is detected diff --git a/packages/kit/src/runtime/client/client.js b/packages/kit/src/runtime/client/client.js index 715ef84cff9e..2f4099c6cb50 100644 --- a/packages/kit/src/runtime/client/client.js +++ b/packages/kit/src/runtime/client/client.js @@ -146,6 +146,19 @@ function native_navigation(url) { return new Promise(() => {}); } +/** + * Checks whether a service worker is registered, and if it is, + * tries to update it. + */ +async function update_service_worker() { + if ('serviceWorker' in navigator) { + const registration = await navigator.serviceWorker.getRegistration(base || '/'); + if (registration) { + await registration.update(); + } + } +} + function noop() {} /** @type {import('types').CSRRoute[]} */ @@ -1003,6 +1016,8 @@ async function load_route({ id, invalidating, url, params, route, preload }) { // Referenced node could have been removed due to redeploy, check const updated = await stores.updated.check(); if (updated) { + // Before reloading, try to update the service worker if it exists + await update_service_worker(); return await native_navigation(url); } @@ -1327,6 +1342,8 @@ async function navigate({ } else if (/** @type {number} */ (navigation_result.props.page.status) >= 400) { const updated = await stores.updated.check(); if (updated) { + // Before reloading, try to update the service worker if it exists + await update_service_worker(); await native_navigation(url); } }