Skip to content

Commit

Permalink
Improve service worker implementation + make it optional (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwojcik authored Aug 20, 2023
1 parent c49c8e3 commit bc3de99
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
34 changes: 28 additions & 6 deletions _11ty/plugins.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable indent */
const { IS_PRODUCTION } = require('./constants');
const siteConfig = require('../content/_data/siteConfig');
const pluginRss = require('@11ty/eleventy-plugin-rss');
Expand All @@ -18,6 +17,32 @@ const productionPlugins = IS_PRODUCTION
]
: [];

const pwaPluginConfig = siteConfig.enablePWA
? [
{
body: pluginPWA,
options: {
cacheId: siteConfig.site.title,
runtimeCaching: [
{
urlPattern: /\/$/,
handler: 'NetworkFirst',
},
{
urlPattern: /\.html$/,
handler: 'NetworkFirst',
},
{
urlPattern:
/^.*\.(jpg|png|mp4|gif|webp|ico|svg|woff2|woff|eot|ttf|otf|ttc|json)$/,
handler: 'StaleWhileRevalidate',
},
],
},
},
]
: [];

const plugins = [
{
body: EleventyHtmlBasePlugin,
Expand All @@ -31,9 +56,6 @@ const plugins = [
{
body: eleventyNavigationPlugin,
},
{
body: pluginPWA,
},
{
body: syntaxHighlight,
},
Expand All @@ -52,8 +74,8 @@ const plugins = [
background_color: siteConfig.manifestJson.backgroundColor,
orientation: 'any',
},
}
},
},
];

module.exports = [...plugins, ...productionPlugins];
module.exports = [...plugins, ...pwaPluginConfig, ...productionPlugins];
3 changes: 2 additions & 1 deletion content/_data/siteConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,9 @@ module.exports = {
postsPerPage: 10,
},
// ---------------------------------------------------------------------------
// Settings for PWA manifest file
// Settings for PWA
// ---------------------------------------------------------------------------
enablePWA: false, // If enabled, service worker for PWA will be registered
manifestJson: {
// Language of PWA application
language: 'en-US',
Expand Down
34 changes: 32 additions & 2 deletions content/_includes/partials/head/service-worker.njk
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 %}

1 comment on commit bc3de99

@vercel
Copy link

@vercel vercel bot commented on bc3de99 Aug 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.