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

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

Closed
Closed
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
31 changes: 18 additions & 13 deletions packages/react-scripts/template-typescript/src/serviceWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const isLocalhost = Boolean(

type Config = {
onSuccess?: (registration: ServiceWorkerRegistration) => void;
onUpdate?: (registration: ServiceWorkerRegistration) => void;
onUpdate?: (registration: ServiceWorkerRegistration, sw: ServiceWorker) => void;
};

export function register(config?: Config) {
Expand Down Expand Up @@ -66,6 +66,10 @@ function registerValidSW(swUrl: string, config?: 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.waiting);
}
registration.onupdatefound = () => {
const installingWorker = registration.installing;
if (installingWorker == null) {
Expand All @@ -74,18 +78,7 @@ function registerValidSW(swUrl: string, config?: Config) {
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(installingWorker);
} else {
// At this point, everything has been precached.
// It's the perfect time to display a
Expand All @@ -100,6 +93,18 @@ function registerValidSW(swUrl: string, config?: Config) {
}
};
};
function newerSwAvailable(sw: ServiceWorker){
// 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/CRA2-PWA.'
);
if (config && config.onUpdate) {
config.onUpdate(registration, sw);
}
}
})
.catch(error => {
console.error('Error during service worker registration:', error);
Expand Down
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,6 +58,10 @@ 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.waiting);
}
registration.onupdatefound = () => {
const installingWorker = registration.installing;
if (installingWorker == null) {
Expand All @@ -66,18 +70,7 @@ function registerValidSW(swUrl, config) {
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(installingWorker);
} else {
// At this point, everything has been precached.
// It's the perfect time to display a
Expand All @@ -92,6 +85,18 @@ function registerValidSW(swUrl, config) {
}
};
};
function newerSwAvailable(sw){
// 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/CRA2-PWA.'
);
if (config && config.onUpdate) {
config.onUpdate(registration, sw);
}
}
})
.catch(error => {
console.error('Error during service worker registration:', error);
Expand Down