Package Exports
- sw-precache-webpack-plugin
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (sw-precache-webpack-plugin) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
sw-precache-webpack-plugin
Webpack plugin for using service workers. Will generate a service worker file using sw-precache and add it to your build directory.
Install
npm install --save-dev sw-precache-webpack-plugin
Usage
var path = require('path');
var SWPrecacheWebpackPlugin = require('sw-precache-webpack-plugin')
module.exports = {
context: __dirname,
entry: {
main: path.resolve(__dirname, 'src/index'),
},
output: {
path: path.resolve(__dirname, 'src/bundles/'),
filename: '[name]-[hash].js',
},
plugins: [
new SWPrecacheWebpackPlugin(
{
cacheId: "my-project-name",
maximumFileSizeToCacheInBytes: 4194304,
runtimeCaching: [{
handler: "cacheFirst",
urlPattern: /[.]mp3$/,
}],
verbose: true,
}
),
]
}
This will generate a new service worker at src/bundles/my-service-worker.js
.
Then you would just register it in your application:
(function() {
if('serviceWorker' in navigator) {
navigator.serviceWorker
.register('/service-worker.js')
.then(function() {
console.log('Service worker registered');
})
.catch(function(error) {
console.error('Error registering service worker: ', error);
});
}
})();
Options
All options as specified by sw-precache are able to be passed in to the plugin.
I recommend omitting the staticFileGlobs
argument and letting the plugin automatically determine the files to cache based on your bundles being built. If you omit the staticFileGlobs
argument will add [name]
and [hash]
of each bundle to staticFileGlobs
.
- // sw-precache options:
- @param {options: cacheId [String]},
- @param {options: directoryIndex [String]},
- @param {options: dynamicUrlToDependencies [Object<String,Array
]}, - @param {options: handleFetch [boolean]},
- @param {options: ignoreUrlParametersMatching [Array
]}, - @param {options: importScripts [Array
]}, - @param {options: logger [function]},
- @param {options: maximumFileSizeToCacheInBytes [Number]},
- @param {options: navigateFallbackWhitelist [Array
]}, - @param {options: replacePrefix [String]},
- @param {options: runtimeCaching [Array
- @param {options: staticFileGlobs [Array
]}, - @param {options: stripPrefix [String}]
- @param {options: templateFilePath [String]},
- @param {options: verbose [boolean]},
You can configure the plugin with these options (passible in the same options object as sw-precache
options)
- // plugin options:
- @param {string} [{options: filename}] - Service worker filename, default is 'service-worker.js'
- @param {string} [{options: filepath}] - Service worker path and name, default is to use webpack.output.path + options.filename
Example:
plugins: [
new SWPrecacheWebpackPlugin(
{
filename: "my-project-service-worker.js",
cacheId: "my-project-name",
}
),
]