Supporting snippets for the workbox blog post.
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
// 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() | |
); |
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
// 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)); | |
}); | |
} |
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
// 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" | |
}; |
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
// 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