Package Exports
- service-worker-webpack
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 (service-worker-webpack) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
service-worker-webpack
A Webpack Plugin to generate a Service Worker. Powered by Workbox.
What is this? 🧐
A minimal wrapper around Workbox to quickly add a service worker via your webpack build process.
Motivation
Workbox is great -- it's well documented and straightforward to customize your service worker. workbox-webpack-plugin makes caching your webpack assets simple, but I found myself reimplementing the same patterns across projects. Specifically:
- Wiring up service worker registration boilerplate
- Toggling service worker development on/off in development
- Toggling Workbox logging on/off when debugging
Installation & Usage 📦
Add this package to your project:
yarn add -D service-worker-webpackUpdate your
webpack.config.js:const { ServiceWorkerPlugin } = require("service-worker-webpack"); module.exports = { plugins: [new ServiceWorkerPlugin()], };
API Overview 🛠
| Name | Description | Type |
|---|---|---|
| enableInDevelopment |
Enable the service worker in local development. Depending on your service worker configuration, this can be problematic for developer workflows where you end up serving outdated cached files. If Defaults to |
boolean | undefined |
| enableWorkboxLogging |
Enable workbox logging. Workbox logging is both very helpful and very chatty. By default, workbox will use the webpack mode to determine whether or not to enable workbox logging. When the mode is Setting this to Note: This option is only relevant when using the service worker generated by workbox. It does not apply to the development service worker generated when Defaults to |
boolean | undefined |
| registration.autoRegister |
Autoregister the service worker. If import { Workbox } from "workbox-window";
if ("serviceWorker" in navigator) {
const wb = new Workbox("/path-to-your-service-worker.js");
wb.register();
}Defaults to |
boolean | undefined |
| registration.entry |
The webpack entry to inject the auto registration code into. The resulting bundle must be present on all pages that expect to register the service worker. Defaults to |
string | undefined |
| registration.path |
The registration path tells the browser where your service worker is located. Defaults to |
string | undefined |
| registration.scope |
The scope of the service worker determines which files the service worker controls, in other words, from which path the service worker will intercept requests. The default scope is the location of the service worker file, and extends to all directories below. So if service-worker.js is located in the root directory, the service worker will control requests from all files at this domain. Defaults to |
string | undefined |
| workbox |
Options passed to Defaults to |
InjectManifestOptions | GenerateSWOptions |
Examples 🚀
Check out the service-worker-webpack-example.
Warning ⚠️
You must serve your application over HTTPS in production environments. Service Workers must be served from the site's origin over HTTPS. A special case is made for localhost, so this is generally not necessary during local development. HTTPS is not handled by this library.
The origin constraint means that the service worker can not control mysite.com if it was served from something like mycdn.mysite.com.
Contributing 👫
PR's and issues welcomed! For more guidance check out CONTRIBUTING.md
Licensing 📃
See the project's MIT License.