Skip to content

Commit

Permalink
Update Service Worker to new version immediately
Browse files Browse the repository at this point in the history
  • Loading branch information
amirnd51 committed Jul 16, 2024
1 parent 0049f98 commit c3c4775
Showing 1 changed file with 35 additions and 27 deletions.
62 changes: 35 additions & 27 deletions src/registerServiceWorker.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
// In production, we register a service worker to serve assets from local cache.

// This lets the app load faster on subsequent visits in production, and gives
// it offline capabilities. However, it also means that developers (and users)
// will only see deployed updates on the "N+1" visit to a page, since previously
// cached resources are updated in the background.

// To learn more about the benefits of this model, read https://goo.gl/KwvDNy.
// This link also includes instructions on opting out of this behavior.

const isLocalhost = Boolean(
window.location.hostname === "localhost" ||
// [::1] is the IPv6 localhost address.
window.location.hostname === "[::1]" ||
// 127.0.0.1/8 is considered localhost for IPv4.
window.location.hostname.match(
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
)
// [::1] is the IPv6 localhost address.
window.location.hostname === "[::1]" ||
// 127.0.0.1/8 is considered localhost for IPv4.
window.location.hostname.match(
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
)
);

export default function register() {
Expand All @@ -41,7 +31,7 @@ export default function register() {
navigator.serviceWorker.ready.then(() => {
console.log(
"This web app is being served cache-first by a service " +
"worker. To learn more, visit https://goo.gl/SC7cgQ"
"worker. To learn more, visit https://goo.gl/SC7cgQ"
);
});
} else {
Expand All @@ -56,19 +46,36 @@ function registerValidSW(swUrl) {
navigator.serviceWorker
.register(swUrl)
.then(registration => {
registration.onupdatefound = () => {
const installingWorker = registration.installing;
if (installingWorker == null) {
return;
}
installingWorker.onstatechange = () => {
if (installingWorker.state === 'installed') {
if (navigator.serviceWorker.controller) {
// New content is available and will be used when all tabs for this page are closed.
console.log('New content is available; please refresh.');

registration.addEventListener('updatefound', () => {
const newWorker = registration.installing;
newWorker.addEventListener('statechange', () => {
if(newWorker.state==="installed"){
newWorker.postMessage({type: 'SKIP_WAITING'});
}
});
});
// Optionally, you can force the new content to be active immediately:
// Uncomment the line below to apply the new version immediately
window.location.reload();

// Notify the user of the update.
// You can customize this notification as needed.
// if (confirm('New version available. Refresh now?')) {
// window.location.reload();
// }
} else {
// Content is cached for offline use.
console.log('Content is cached for offline use.');
}
}
};
};
})
.catch(error => {
console.error('Error during service worker registration:', error);
console.error('Error during service worker registration:', error);
});
}

Expand All @@ -77,9 +84,10 @@ function checkValidServiceWorker(swUrl) {
fetch(swUrl)
.then(response => {
// Ensure service worker exists, and that we really are getting a JS file.
const contentType = response.headers.get("content-type");
if (
response.status === 404 ||
response.headers.get("content-type").indexOf("javascript") === -1
(contentType && contentType.indexOf("javascript") === -1)
) {
// No service worker found. Probably a different app. Reload the page.
navigator.serviceWorker.ready.then(registration => {
Expand Down

0 comments on commit c3c4775

Please sign in to comment.