Skip to content

Commit

Permalink
fix: register client module and prompt (#633)
Browse files Browse the repository at this point in the history
* fix: register client module and prompt

* chore: update register module
  • Loading branch information
userquin committed Feb 10, 2024
1 parent 865fdad commit b347432
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/client/build/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,25 @@ export function registerSW(options: RegisterSWOptions = {}) {
window.location.reload()
})
wb.addEventListener('installed', (event) => {
if (!event.isUpdate) {
if (event.isUpdate === false) {
onOfflineReady?.()
}
});
}
else {
let onNeedRefreshCalled = false
const showSkipWaitingPrompt = () => {
const showSkipWaitingPrompt = (event?: import('workbox-window').WorkboxLifecycleWaitingEvent) => {
/*
FIX:
- open page in a new tab and navigate to home page
- add a new sw version
- open a new second tab and navigate to home page
- click reload on the first tab
- second tab refreshed, but the first tab doesn't (still with prompt)
*/
if (event && onNeedRefreshCalled && event.isExternal)
window.location.reload()

onNeedRefreshCalled = true
// \`event.wasWaitingBeforeRegister\` will be false if this is
// the first time the updated service worker is waiting.
Expand All @@ -83,7 +94,7 @@ export function registerSW(options: RegisterSWOptions = {}) {
// that will reload the page as soon as the previously waiting
// service worker has taken control.
wb?.addEventListener('controlling', (event) => {
if (event.isUpdate)
if (event.isUpdate === true || event.isExternal === true)
window.location.reload()
})

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

Expand Down

0 comments on commit b347432

Please sign in to comment.