Skip to content

Instantly share code, notes, and snippets.

@edm00se
Last active November 8, 2018 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edm00se/c4defbf1b29ec3f6e10cdb7fc1afcbec to your computer and use it in GitHub Desktop.
Save edm00se/c4defbf1b29ec3f6e10cdb7fc1afcbec to your computer and use it in GitHub Desktop.
Supporting snippets for the workbox blog post.
// advanced config for injectManifest approach
importScripts('https://storage.googleapis.com/workbox-cdn/releases/3.6.3/workbox-sw.js');
workbox.precaching.precacheAndRoute([]);
workbox.routing.registerRoute(
new RegExp('https://some-other.com/routed-response/i-want-cached'),
workbox.strategies.cacheFirst()
);
// script at bottom of index.html or main.js
if ('serviceWorker' in navigator && process.env.NODE_ENV === 'production') {
window.addEventListener('load', () => {
navigator.serviceWorker
.register('sw.js')
.then(r => {
console.log('service worker registered in scope: ', r.scope);
})
.catch(e => console.log('SW error: ', e));
});
}
// config/workbox-config.js, with source sw defined
module.exports = {
"globDirectory": "dist/",
"globPatterns": [
"**/*.{png,ico,html,css,js}"
],
"swDest": "dist/sw.js",
"swSrc": "src/sw.js"
};
// config/workbox-config.js
module.exports = {
"globDirectory": "dist/",
"globPatterns": [
"**/*.{png,ico,html,css,js}"
],
"swDest": "dist/sw.js"
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment