-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve service worker implementation + make it optional (#63)
- Loading branch information
Showing
3 changed files
with
62 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,33 @@ | ||
{% if siteConfig.enablePWA %} | ||
<script> | ||
if ('serviceWorker' in navigator) navigator.serviceWorker.register('/service-worker.js'); | ||
</script> | ||
if ('serviceWorker' in navigator) { | ||
navigator.serviceWorker.register('/service-worker.js') | ||
.then(registration => { | ||
registration.addEventListener('updatefound', () => { | ||
const newWorker = registration.installing; | ||
newWorker.addEventListener('statechange', () => { | ||
if (newWorker.state === 'installed') { | ||
if (navigator.serviceWorker.controller) { | ||
window.location.reload(); | ||
} | ||
} | ||
}); | ||
}); | ||
}) | ||
.catch(error => { | ||
console.error('Service Worker registration failed:', error); | ||
}); | ||
} | ||
</script> | ||
{% else %} | ||
<script> | ||
if ('serviceWorker' in navigator) { | ||
navigator.serviceWorker.getRegistrations().then(function(registrations) { | ||
for(let registration of registrations) { | ||
registration.unregister(); | ||
} | ||
}); | ||
} | ||
</script> | ||
{% endif %} |
bc3de99
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
eleventy-bliss – ./
eleventy-bliss.vercel.app
11ty-bliss.lkmt.us
eleventy-bliss-lwojcik.vercel.app
eleventy-bliss.lkmt.us
eleventy-bliss-git-main-lwojcik.vercel.app