Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/unlucky-cars-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': minor
---

feat: update service worker when new version is detected
17 changes: 17 additions & 0 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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[]} */
Expand Down Expand Up @@ -1001,6 +1014,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);
}

Expand Down Expand Up @@ -1325,6 +1340,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);
}
}
Expand Down
Loading