Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix service worker onUpdate not triggering if page load was interrupted #23

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,26 @@ function registerValidSW(swUrl: string, config?: Config) {
}
};
};

// in the case a page is loaded an a service worker is already waiting
// we should trigger the onUpdate callback
const waitingWorker = registration.waiting;
if (waitingWorker) {
if (navigator.serviceWorker.controller && waitingWorker.state === 'installed') {
// At this point, the updated precached content has been fetched,
// but the previous service worker will still serve the older
// content until all client tabs are closed.
console.log(
'New content is available and will be used when all ' +
'tabs for this page are closed. See https://cra.link/PWA.'
);

// Execute callback
if (config && config.onUpdate) {
config.onUpdate(registration);
}
}
}
})
.catch((error) => {
console.error('Error during service worker registration:', error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,26 @@ function registerValidSW(swUrl, config) {
}
};
};

// in the case a page is loaded an a service worker is already waiting
// we should trigger the onUpdate callback
const waitingWorker = registration.waiting;
if (waitingWorker) {
if (navigator.serviceWorker.controller && waitingWorker.state === 'installed') {
// At this point, the updated precached content has been fetched,
// but the previous service worker will still serve the older
// content until all client tabs are closed.
console.log(
'New content is available and will be used when all ' +
'tabs for this page are closed. See https://cra.link/PWA.'
);

// Execute callback
if (config && config.onUpdate) {
config.onUpdate(registration);
}
}
}
})
.catch((error) => {
console.error('Error during service worker registration:', error);
Expand Down