Skip to content

Commit aea0fee

Browse files
committed
Add service worker invalidation
1 parent 7a0fb4c commit aea0fee

File tree

1 file changed

+49
-24
lines changed

1 file changed

+49
-24
lines changed

packages/react-scripts/template/src/registerServiceWorker.js

+49-24
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,61 @@ export default function register() {
2121

2222
window.addEventListener('load', () => {
2323
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
24-
navigator.serviceWorker
25-
.register(swUrl)
26-
.then(registration => {
27-
registration.onupdatefound = () => {
28-
const installingWorker = registration.installing;
29-
installingWorker.onstatechange = () => {
30-
if (installingWorker.state === 'installed') {
31-
if (navigator.serviceWorker.controller) {
32-
// At this point, the old content will have been purged and
33-
// the fresh content will have been added to the cache.
34-
// It's the perfect time to display a "New content is
35-
// available; please refresh." message in your web app.
36-
console.log('New content is available; please refresh.');
37-
} else {
38-
// At this point, everything has been precached.
39-
// It's the perfect time to display a
40-
// "Content is cached for offline use." message.
41-
console.log('Content is cached for offline use.');
42-
}
43-
}
44-
};
45-
};
24+
25+
fetch(swUrl)
26+
.then(response => {
27+
// Ensure service worker exists, and that we really are getting a JS file.
28+
if (
29+
response.status === 404 ||
30+
response.headers.get('content-type').indexOf('javascript') === -1
31+
) {
32+
navigator.serviceWorker.ready.then(registration => {
33+
registration.unregister().then(() => {
34+
window.location.reload();
35+
});
36+
});
37+
} else {
38+
registerValidSW(swUrl);
39+
}
4640
})
47-
.catch(error => {
48-
console.error('Error during service worker registration:', error);
41+
.catch(() => {
42+
console.log(
43+
'No internet connection found. App is running in offline mode.'
44+
);
4945
});
5046
});
5147
}
5248
}
5349

50+
function registerValidSW(swUrl) {
51+
navigator.serviceWorker
52+
.register(swUrl)
53+
.then(registration => {
54+
registration.onupdatefound = () => {
55+
const installingWorker = registration.installing;
56+
installingWorker.onstatechange = () => {
57+
if (installingWorker.state === 'installed') {
58+
if (navigator.serviceWorker.controller) {
59+
// At this point, the old content will have been purged and
60+
// the fresh content will have been added to the cache.
61+
// It's the perfect time to display a "New content is
62+
// available; please refresh." message in your web app.
63+
console.log('New content is available; please refresh.');
64+
} else {
65+
// At this point, everything has been precached.
66+
// It's the perfect time to display a
67+
// "Content is cached for offline use." message.
68+
console.log('Content is cached for offline use.');
69+
}
70+
}
71+
};
72+
};
73+
})
74+
.catch(error => {
75+
console.error('Error during service worker registration:', error);
76+
});
77+
}
78+
5479
export function unregister() {
5580
if ('serviceWorker' in navigator) {
5681
navigator.serviceWorker.ready.then(registration => {

0 commit comments

Comments
 (0)