Skip to content

Commit d64a7b9

Browse files
committed
Add cache invalidation based on service work url
1 parent 67f6163 commit d64a7b9

File tree

1 file changed

+56
-24
lines changed

1 file changed

+56
-24
lines changed

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

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,69 @@
1111
export default function register() {
1212
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
1313
window.addEventListener('load', () => {
14+
// this would become service-work-hash.js
1415
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
15-
navigator.serviceWorker
16-
.register(swUrl)
17-
.then(registration => {
18-
registration.onupdatefound = () => {
19-
const installingWorker = registration.installing;
20-
installingWorker.onstatechange = () => {
21-
if (installingWorker.state === 'installed') {
22-
if (navigator.serviceWorker.controller) {
23-
// At this point, the old content will have been purged and
24-
// the fresh content will have been added to the cache.
25-
// It's the perfect time to display a "New content is
26-
// available; please refresh." message in your web app.
27-
console.log('New content is available; please refresh.');
28-
} else {
29-
// At this point, everything has been precached.
30-
// It's the perfect time to display a
31-
// "Content is cached for offline use." message.
32-
console.log('Content is cached for offline use.');
33-
}
16+
if (!navigator.serviceWorker.controller) {
17+
// No service worker yet
18+
registerServiceWorker(swUrl);
19+
} else {
20+
fetch(swUrl).then(res => {
21+
// Check to see if the SW URL is valid
22+
if (res.ok) {
23+
// Matches. All good. Continue with registering SW
24+
registerServiceWorker(swUrl);
25+
} else {
26+
// SW URL was invalid.
27+
fetch(
28+
`${window.location.protocol}//${window.location.host}`
29+
).then(res2 => {
30+
// Just check if online
31+
if (res2.ok) {
32+
// Unregister and refresh page
33+
unregister();
34+
window.location.reload(true);
35+
} else {
36+
console.log('Offline. Using cached copy');
3437
}
35-
};
36-
};
37-
})
38-
.catch(error => {
39-
console.error('Error during service worker registration:', error);
38+
});
39+
}
4040
});
41+
}
4142
});
4243
}
4344
}
4445

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

0 commit comments

Comments
 (0)