Skip to content

Commit b347432

Browse files
authored
fix: register client module and prompt (#633)
* fix: register client module and prompt * chore: update register module
1 parent 865fdad commit b347432

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/client/build/register.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,25 @@ export function registerSW(options: RegisterSWOptions = {}) {
6262
window.location.reload()
6363
})
6464
wb.addEventListener('installed', (event) => {
65-
if (!event.isUpdate) {
65+
if (event.isUpdate === false) {
6666
onOfflineReady?.()
6767
}
6868
});
6969
}
7070
else {
7171
let onNeedRefreshCalled = false
72-
const showSkipWaitingPrompt = () => {
72+
const showSkipWaitingPrompt = (event?: import('workbox-window').WorkboxLifecycleWaitingEvent) => {
73+
/*
74+
FIX:
75+
- open page in a new tab and navigate to home page
76+
- add a new sw version
77+
- open a new second tab and navigate to home page
78+
- click reload on the first tab
79+
- second tab refreshed, but the first tab doesn't (still with prompt)
80+
*/
81+
if (event && onNeedRefreshCalled && event.isExternal)
82+
window.location.reload()
83+
7384
onNeedRefreshCalled = true
7485
// \`event.wasWaitingBeforeRegister\` will be false if this is
7586
// the first time the updated service worker is waiting.
@@ -83,7 +94,7 @@ export function registerSW(options: RegisterSWOptions = {}) {
8394
// that will reload the page as soon as the previously waiting
8495
// service worker has taken control.
8596
wb?.addEventListener('controlling', (event) => {
86-
if (event.isUpdate)
97+
if (event.isUpdate === true || event.isExternal === true)
8798
window.location.reload()
8899
})
89100

@@ -111,8 +122,6 @@ export function registerSW(options: RegisterSWOptions = {}) {
111122
// Add an event listener to detect when the registered
112123
// service worker has installed but is waiting to activate.
113124
wb.addEventListener('waiting', showSkipWaitingPrompt)
114-
// @ts-expect-error event listener provided by workbox-window
115-
wb.addEventListener('externalwaiting', showSkipWaitingPrompt)
116125
}
117126
}
118127

0 commit comments

Comments
 (0)