Skip to content

Make onUpdate callback fire when a new service worker is intalled and waiting #5491

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

Closed
Closed
29 changes: 17 additions & 12 deletions packages/react-scripts/template/src/serviceWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,16 @@ function registerValidSW(swUrl, config) {
navigator.serviceWorker
.register(swUrl)
.then(registration => {
//a new service worker has previously finished installing, and is now waiting
if (registration.waiting && registration.active) {
newerSwAvailable();
}
registration.onupdatefound = () => {
const installingWorker = registration.installing;
installingWorker.onstatechange = () => {
if (installingWorker.state === 'installed') {
if (navigator.serviceWorker.controller) {
// 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 http://bit.ly/CRA-PWA.'
);

// Execute callback
if (config && config.onUpdate) {
config.onUpdate(registration);
}
newerSwAvailable();
} else {
// At this point, everything has been precached.
// It's the perfect time to display a
Expand All @@ -89,6 +82,18 @@ function registerValidSW(swUrl, config) {
}
};
};
function newerSwAvailable(){
// 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 http://bit.ly/CRA-PWA.'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat PR!

This bit.ly link needs to be pointed to https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app now. I created http://bit.ly/CRA2-PWA but not sure if @gaearon wants to do it from his own account or something similar :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some issues with updating these links as these files (with the original links) are present in existing projects. More details here: #5536

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I see. Thanks.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

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