Skip to content

Commit 8aa95b4

Browse files
dumbmoronteemingc
andauthored
feat: update service worker when new version is detected (#12448)
closes #3667 --------- Co-authored-by: Tee Ming <[email protected]>
1 parent 6910444 commit 8aa95b4

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

.changeset/unlucky-cars-swim.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': minor
3+
---
4+
5+
feat: update service worker when new version is detected

packages/kit/src/runtime/client/client.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,19 @@ function native_navigation(url) {
146146
return new Promise(() => {});
147147
}
148148

149+
/**
150+
* Checks whether a service worker is registered, and if it is,
151+
* tries to update it.
152+
*/
153+
async function update_service_worker() {
154+
if ('serviceWorker' in navigator) {
155+
const registration = await navigator.serviceWorker.getRegistration(base || '/');
156+
if (registration) {
157+
await registration.update();
158+
}
159+
}
160+
}
161+
149162
function noop() {}
150163

151164
/** @type {import('types').CSRRoute[]} */
@@ -1003,6 +1016,8 @@ async function load_route({ id, invalidating, url, params, route, preload }) {
10031016
// Referenced node could have been removed due to redeploy, check
10041017
const updated = await stores.updated.check();
10051018
if (updated) {
1019+
// Before reloading, try to update the service worker if it exists
1020+
await update_service_worker();
10061021
return await native_navigation(url);
10071022
}
10081023

@@ -1327,6 +1342,8 @@ async function navigate({
13271342
} else if (/** @type {number} */ (navigation_result.props.page.status) >= 400) {
13281343
const updated = await stores.updated.check();
13291344
if (updated) {
1345+
// Before reloading, try to update the service worker if it exists
1346+
await update_service_worker();
13301347
await native_navigation(url);
13311348
}
13321349
}

0 commit comments

Comments
 (0)