Skip to content

Commit

Permalink
feat: update service worker when new version is detected (#12448)
Browse files Browse the repository at this point in the history
closes #3667

---------

Co-authored-by: Tee Ming <[email protected]>
  • Loading branch information
dumbmoron and eltigerchino authored Oct 10, 2024
1 parent 6910444 commit 8aa95b4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
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 @@ -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);
}

Expand Down Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 8aa95b4

Please sign in to comment.