Skip to content
Closed
Changes from 1 commit
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
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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