Skip to content

Commit d1ea70b

Browse files
committed
♻️(service-worker) improve SW registration and update handling
It is apparently a bad practice to add the version number to the service worker file name. This prevents the browser from properly updating the service worker when a new version is available. We improve the update handling by a more usual pattern.
1 parent 91eba31 commit d1ea70b

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ and this project adheres to
3030
- ♿ add h1 for SR on 40X pages and remove alt texts #1438
3131
- ♿ update labels and shared document icon accessibility #1442
3232
- 🍱(frontend) Fonts GDPR compliants #1453
33+
- ♻️(service-worker) improve SW registration and update handling #1473
3334

3435
### Fixed
3536

src/frontend/apps/impress/src/features/service-worker/hooks/useSWRegister.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const useSWRegister = () => {
77
process.env.NEXT_PUBLIC_SW_DEACTIVATED !== 'true'
88
) {
99
navigator.serviceWorker
10-
.register(`/service-worker.js?v=${process.env.NEXT_PUBLIC_BUILD_ID}`)
10+
.register(`/service-worker.js`)
1111
.then((registration) => {
1212
registration.onupdatefound = () => {
1313
const newWorker = registration.installing;
@@ -29,11 +29,13 @@ export const useSWRegister = () => {
2929
console.error('Service worker registration failed:', err);
3030
});
3131

32-
const currentController = navigator.serviceWorker.controller;
32+
let refreshing = false;
3333
const onControllerChange = () => {
34-
if (currentController) {
35-
window.location.reload();
34+
if (refreshing) {
35+
return;
3636
}
37+
refreshing = true;
38+
window.location.reload();
3739
};
3840
navigator.serviceWorker.addEventListener(
3941
'controllerchange',

0 commit comments

Comments
 (0)