🐛(service-worker) fix sw registration and page reload logic
When a new service worker is installed, the page was reloaded to ensure the new service worker took control, it is not a big issue in normal browsing mode because the service worker is only updated once in a while (every release). However, in incognito mode, the service worker has to be re-registered on each new session, which means that the page was reloading each time the user opened a new incognito window, creating a bad user experience. We now take in consideration the case where the service-worker is installed for the first time, and don't reload if it is this case.
This commit is contained in:
@@ -6,6 +6,10 @@ and this project adheres to
|
|||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
🐛(service-worker) fix sw registration and page reload logic #1500
|
||||||
|
|
||||||
## [3.8.1] - 2025-10-17
|
## [3.8.1] - 2025-10-17
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|||||||
@@ -3,9 +3,14 @@ import { useEffect } from 'react';
|
|||||||
export const useSWRegister = () => {
|
export const useSWRegister = () => {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (
|
if (
|
||||||
'serviceWorker' in navigator &&
|
!('serviceWorker' in navigator) ||
|
||||||
process.env.NEXT_PUBLIC_SW_DEACTIVATED !== 'true'
|
process.env.NEXT_PUBLIC_SW_DEACTIVATED === 'true'
|
||||||
) {
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const hadControllerAtStart = !!navigator.serviceWorker.controller;
|
||||||
|
|
||||||
navigator.serviceWorker
|
navigator.serviceWorker
|
||||||
.register(`/service-worker.js`)
|
.register(`/service-worker.js`)
|
||||||
.then((registration) => {
|
.then((registration) => {
|
||||||
@@ -31,12 +36,26 @@ export const useSWRegister = () => {
|
|||||||
|
|
||||||
let refreshing = false;
|
let refreshing = false;
|
||||||
const onControllerChange = () => {
|
const onControllerChange = () => {
|
||||||
if (refreshing) {
|
if (!hadControllerAtStart || refreshing) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
refreshing = true;
|
refreshing = true;
|
||||||
|
|
||||||
|
if (document.visibilityState === 'visible') {
|
||||||
window.location.reload();
|
window.location.reload();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const onVisible = () => {
|
||||||
|
if (document.visibilityState === 'visible') {
|
||||||
|
window.location.reload();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
document.addEventListener('visibilitychange', onVisible, { once: true });
|
||||||
|
};
|
||||||
|
|
||||||
navigator.serviceWorker.addEventListener(
|
navigator.serviceWorker.addEventListener(
|
||||||
'controllerchange',
|
'controllerchange',
|
||||||
onControllerChange,
|
onControllerChange,
|
||||||
@@ -48,6 +67,5 @@ export const useSWRegister = () => {
|
|||||||
onControllerChange,
|
onControllerChange,
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
}
|
|
||||||
}, []);
|
}, []);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user