Registry / web-framework / serviceworker-webpack-plugin

serviceworker-webpack-plugin

JSON →
library1.0.2jsnpmunverified

A webpack plugin (v1.0.2, latest) that simplifies creating a service worker to cache all webpack bundle assets. It automatically resolves asset names (including hashed names for long-term caching) and provides them to a custom service worker script via a global `serviceWorkerOption` variable. Supports webpack 4 only (peer dependency). Key differentiator: it handles non-deterministic asset names and integrates with webpack's dev server. Alternatives like `workbox-webpack-plugin` are more full-featured but heavier; this plugin is minimal and focused.

npm install serviceworker-webpack-plugin
INSTALL
IMPORT
SIG · SERVICEWORKER-WEBP
S
serviceworker-webpack-plugin
web-frameworkjavascriptv1.0.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ServiceWorkerWebpackPlugin
import ServiceWorkerWebpackPlugin from 'serviceworker-webpack-plugin';
const ServiceWorkerWebpackPlugin = require('serviceworker-webpack-plugin').default;
Default export is the constructor. In CommonJS, use require('serviceworker-webpack-plugin').default; plain require('serviceworker-webpack-plugin') returns the module object.
runtime
import runtime from 'serviceworker-webpack-plugin/lib/runtime';
import { runtime } from 'serviceworker-webpack-plugin';
The runtime module is a separate file in the lib folder, not part of the main export. Must import with relative path.
serviceWorkerOption
// Global variable injected into the service worker script at build time
import { serviceWorkerOption } from 'serviceworker-webpack-plugin';
This is a global variable injected into the service worker file by the plugin. It is not importable.

Demonstrates setup: plugin in webpack config, custom service worker that caches all assets, and runtime registration in main bundle.

// webpack.config.js import path from 'path'; import ServiceWorkerWebpackPlugin from 'serviceworker-webpack-plugin'; export default { entry: './src/index.js', output: { path: path.resolve(__dirname, 'dist'), filename: 'bundle.[chunkhash].js', publicPath: '/', }, plugins: [ new ServiceWorkerWebpackPlugin({ entry: path.join(__dirname, 'src/sw.js'), }), ], }; // src/sw.js self.addEventListener('install', function(event) { event.waitUntil( caches.open('my-cache').then(function(cache) { return cache.addAll(serviceWorkerOption.assets); }) ); }); self.addEventListener('fetch', function(event) { event.respondWith( caches.match(event.request).then(function(response) { return response || fetch(event.request); }) ); }); // src/index.js import runtime from 'serviceworker-webpack-plugin/lib/runtime'; if ('serviceWorker' in navigator) { runtime.register(); }
Debug
Known issues
breakingv1.0.0 dropped webpack 3 support; requires webpack ^4
fix
Upgrade to webpack 4 or stay on v0.2.3 for webpack 3
affects: >=1.0.0
deprecatedv0.2.0 changed default publicPath from '' to '/'; can cause double slashes in asset paths
fix
Set publicPath explicitly to '' if using a relative path, or upgrade to latest
affects: >=0.2.0 <0.3.0
gotchaRuntime import must be from serviceworker-webpack-plugin/lib/runtime, not the main package
fix
Use `import runtime from 'serviceworker-webpack-plugin/lib/runtime'`
affects: >=1.0.0
gotchaThe default export in CommonJS requires `.default` access
fix
Use `const ServiceWorkerWebpackPlugin = require('serviceworker-webpack-plugin').default`
affects: >=1.0.0
gotchaPlugin does not support webpack 5; unmaintained for newer webpack versions
fix
Consider using workbox-webpack-plugin for webpack 5 projects
affects: >=1.0.2
Errors
Common errors & fixes
TypeError: ServiceWorkerWebpackPlugin is not a constructor
CommonJS require returns the module object, not the constructor directly
fix
Use `const ServiceWorkerWebpackPlugin = require('serviceworker-webpack-plugin').default`
Module not found: Can't resolve 'serviceworker-webpack-plugin/lib/runtime'
Incorrect import path or missing dependency
fix
Ensure correct import: `import runtime from 'serviceworker-webpack-plugin/lib/runtime'`
Error: serviceWorkerOption is not defined
The global variable is only available inside the service worker file processed by the plugin
fix
Reference `serviceWorkerOption` only in the file specified as `entry` in the plugin options
Upgrade
Version history
1.0.2latest on npm
Audit
Dependencies
webpackrequiredpeer dependency; plugin designed specifically for webpack 4.x
Agent activity
6 hits · last 30 days
node
6
Resources
serviceworker-webpack-plugin — npm install serviceworker-webpack-plugin · libregistry