Skip to content

Commit

Permalink
Force update page if new service worker is installed (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwojcik authored Aug 13, 2023
1 parent 99de7d5 commit 54af854
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions content/_includes/partials/head.njk
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,25 @@
<meta name="generator" content="{{ siteConfig.generator.name }} v{{ siteConfig.generator.version }}">
{% favicons 'assets/favicon.svg', appleIconBgColor='#000' %}
{%- if siteConfig.enablePWA -%}
<script>if ('serviceWorker' in navigator) navigator.serviceWorker.register('/service-worker.js');</script>
{%- endif -%}
<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>
{%- endif -%}

0 comments on commit 54af854

Please sign in to comment.