A tool to generate robust Service Worker for your application. Refactored and improved from sw-precache
npm i -S create-sw
or you can install it globally:
npm i -g create-sw
This tool provides a cli tool to help you build your own service-worker.js
create-sw --config=sw-config.js
sw-config.js
has the same options in sw-precache and some unique options:
An array contains the external resources url that you want to precache.
// sw-config.js
module.exports = {
// ...
externals: [
'https://cdn.bootcss.com/jquery/3.2.1/jquery.js',
'https://cdn.bootcss.com/bootstrap/4.0.0-beta/css/bootstrap.css'
]
};
An option to deside if the output sw.js is minified. Defaults to false
// sw-config.js
module.exports = {
// ...
minify: true
};
This tool also provides a runtime for you to install Service Worker and set a callback to the Service Worker lifecycle:
// In your Javascript code
var runtime = require('create-sw/runtime');
runtime.install('/path/to/sw.js', {
onInstalled() {
console.log('onInstalled')
},
onUpdated() {
console.log('onUpdated')
},
onUpdateFailed() {
console.log('onUpdateFailed')
}
})