Simplifies creation of a service worker to serve your webpack bundles
npm install serviceworker-webpack-plugin> Simplifies creation of a service worker to serve your webpack bundles.





``sh`
npm install serviceworker-webpack-plugin
When building a service worker, you probably want to cache all
your assets during the install phase.
But in order to do so, you need their names.
That's not simple when you are using Webpack:
- The assets names are non-deterministic when taking advantage of the long term caching.
- The assets list can even evolve over time as you add splitting points or more resources.
- You want to be able to use your service worker with the dev-server mode of Webpack.
- You want to keep the build process as simple as possible.
`js
import ServiceWorkerWebpackPlugin from 'serviceworker-webpack-plugin';
...
plugins: [
new ServiceWorkerWebpackPlugin({
entry: path.join(__dirname, 'src/sw.js'),
}),
],
`
`js
import runtime from 'serviceworker-webpack-plugin/lib/runtime';
if ('serviceWorker' in navigator) {
const registration = runtime.register();
}
`
You can now use the global serviceWorkerOption variable in your sw.js.`
E.g. In our example this object looks like:js`
{
assets: [
'./main.256334452761ef349e91.js',
],
}
You can have a look at the /docs
folder if you need a full working example.
- optionsentry
- , required, string:filename
Path to the actual service worker implementation.
- , string, default 'sw.js':output.path
Relative (from the webpack's config ) output path for emitted script.excludes
- , array, default ['/.', '/.map']:serviceWorkerOption.assets
Exclude matched assets from being added to the variable. (Blacklist)includes
- , array, default ['*/']:serviceWorkerOption.assets
Include matched assets added to the variable. (Whitelist)publicPath
- , string, default '/':template
Specifies the public URL address of the output files when referenced in a browser.
We use this value to load the service worker over the network.
- , function, default noop:serviceWorkerOption
This callback function can be used to inject statically generated service worker.
It's taking a argument and must return a promise.transformOptions
- , function:serviceWorkerOption
This callback function receives a raw argument.jsonStats
The key contains all the webpack build information.minimize
- :process.env.NODE_ENV === 'production'
Whether to minimize output. Defaults to
- options: That's forwarded to the options argument of theServiceWorkerContainer.register() function.
- The offline-plugin package
was a great source of inspiration.
- The html-webpack-plugin
package was also really helpful.
?I wouldn't have been able to write this plugin without the offline-plugin project.
Thanks @NekR for sharing it!
Still, soon after using it, I realized that it wasn't what I was looking for.
- The abstraction provided was too high.
(I needed to build some custom fetch logic.)
- It was making me, even more, dependent on Webpack.
(What if later, I want to switch to another build system?)
Hence, I decided to change the approach and created this thin layer on
top of Webpack to solve the assets name issue. Nothing more.
If you don't care about my two issues with offline-pluginoffline-plugin` is great.
then you don't need to use this package,
MIT