service worker register by webpack service worker manifest service-worker-precache-webpack-plugin
npm i service-worker-register --save
const serviceWorkerRegister = require('service-worker-register');
// The service-worker.js name will get really url address by sw-mapping.json file
serviceWorkerRegister.register('service-worker.js');
serviceWorkerRegister.register(filename, options)
:
options.scope
: {String} default:/
- service worker scopeoptions.manifestUrl
: {String} default:/public/sw-mapping.json
- service worker filename mapping fileoptions.report
: {Function} optional - service worker register error report interface
The plugin depend on service-worker-precache-webpack-plugin
webpack-manifest-resource-plugin
injectsw-mapping.json
file content to global varSERVICE_WORKER_MANIFEST
// sw-manifest.js
{
"config": {
"localPublicPath": "/public/",
"publicPath": "/public/"
},
"service-worker.js": "service-worker.4434ddf3js"
}
- if
SERVICE_WORKER_MANIFEST
is be inject, use this configuration directly, otherwise launch the network based onoptions.manifestUrl
URL