Skip to content

Instantly share code, notes, and snippets.

@edm00se

edm00se/sw-inject-manifest.js Secret

Last active Nov 8, 2018
Embed
What would you like to do?
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
You can’t perform that action at this time.