Last active
July 3, 2017 05:28
-
-
Save whilelucky/fb2b569631a5c6d5585951ae516302ea to your computer and use it in GitHub Desktop.
service-worker
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
// register the service worker after the onload event to prevent | |
// bandwidth resource contention during the main and vendor js downloads | |
export const scripts = { | |
serviceWorker: | |
`"serviceWorker" in window.navigator && window.addEventListener("load", function() { | |
window.navigator.serviceWorker.register("/serviceWorker.js") | |
.then(function(r) { | |
console.log("ServiceWorker registration successful with scope: ", r.scope) | |
}).catch(function(e) { | |
console.error("ServiceWorker registration failed: ", 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
<script src="${assets.webpackManifest.js}"></script> | |
<script src="${assets.vendor.js}"></script> | |
<script src="${assets.main.js}"></script> | |
<script>${scripts.loadRemainingCSS(route)}</script> | |
//add the serviceWorker script to your html template | |
<script>${scripts.serviceWorker}</script> |
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
//serve it at the root level scope | |
app.use('/serviceWorker.js', express.static('build/client/serviceWorker.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
new SWPrecacheWebpackPlugin({ | |
cacheId: 'app-name', | |
filename: 'serviceWorker.js', | |
staticFileGlobsIgnorePatterns: [/\.map$/, /manifest/i], | |
dontCacheBustUrlsMatching: /./, | |
minify: true, | |
}), |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment