An rspack plugin that generates a manifest of assets and injects it into a service worker asset.
This is a fork of the InjectManifest
part of the
workbox-webpack-plugin
package
but adapted to rspack's native Service Worker support.
npm install -D @birchill/inject-manifest-plugin
-
The first step is to define your service worker in a way that rspack will detect it. That means passing a
URL
object tonavigator.serviceWorker.register()
. For example:const registrationPromise = navigator.serviceWorker.register( new URL( /* webpackChunkName: "serviceworker" */ './sw.ts', import.meta.url ) );
Note that there are various limitations to rspack's handling of URL arguments such as not being able to pass a URL variable. See rspack's usage notes for Web Workers.
-
The next step is ensuring that your generated service worker asset has a fixed name (e.g.
serviceworker.js
) rather than a name with a cache-busting hash in it.// rspack.config.js const config = { // ... output: { chunkFilename: (assetInfo) => { // The service worker entrypoint needs to be fixed (i.e. not have a hash // appended). if (assetInfo.chunk?.name === 'serviceworker') { return '[name].js'; } return '[name].[contenthash].js'; }, }, };
-
Finally, add the plugin to your rspack configuration.
The only required parameter is
swDest
, which is the path to the generated service worker asset. This is not necessarily the same as the path of the source file.For example, if your service worker source file is
src/sw.ts
but you registered it as follows:navigator.serviceWorker.register( new URL( /* webpackChunkName: "serviceworker" */ './sw.ts', import.meta.url ) );
swDest
should be'serviceworker.js'
, NOTsrc/sw.ts
.For example:
// rspack.config.js import { InjectManifest } from '@birchill/inject-manifest-plugin'; const config = { // ... plugins: [ new InjectManifest({ swDest: 'serviceworker.js', }), ], };
The options are the same as the InjectManifest
options in the Workbox plugin
which are documented
here.
-
additionalManifestProperties
(optional) - A list of entries to be precached, in addition to any entries that are generated as part of the build configuration.Type: Array of strings or
ManifestEntry
objects whose shape is as follows:interface ManifestEntry { integrity?: string; revision: string | null; url: string; }
-
chunks
(optional) - One or more chunk names whose corresponding output files should be included in the precache manifest.Type: Array of strings.
-
dontCacheBustURLsMatching
(optional) - Assets that match this will be uniquely versioned via their URL, and exempted from the normal HTTP cache-busting that's done when populating the precache.While not required, it's recommended that if your existing build process already inserts a
[hash]
value into each filename, you provide a RegExp that will detect that, as it will reduce the bandwidth consumed when precaching. -
exclude
(optional) - One or more specifiers used to exclude assets from the precache manifest.This is interpreted following the same rules as
rspack
's standardexclude
option.If not provided, the default value is
[/\\.map$/, /^manifest.*\\.js$/]
.",Type: Array of
Condition
s whose type istype Condition = RegExp | string | ((any: unknown) => boolean);
-
excludeChunks
(optional) - One or more chunk names whose corresponding output files should be excluded from the precache manifest.Type: Array of strings.
-
include
(optional) - One or more specifiers used to include assets in the precache manifest.This is interpreted following the same rules as
rspack
's standardinclude
option.Type: Array of
Condition
s. -
injectionPoint
(optional) - The string to find inside of theswDest
asset. Once found, it will be replaced by the generated precache manifest.Defaults to
'self.__WB_MANIFEST'
.Type: string.
-
manifestTransforms
(optional) - One or more functions which will be applied sequentially against the generated manifest.If
modifyURLPrefix
ordontCacheBustURLsMatching
are also specified, their corresponding transformations will be applied first.Type: Array of functions whose type is
ManifestTransform
:type ManifestTransform = ( manifestEntries: Array<ManifestEntry & { size: number }>, compilation?: Compilation ) => Promise<ManifestTransformResult> | ManifestTransformResult; type ManifestTransformResult = { manifest: Array<ManifestEntry & { size: number }>; warnings?: Array<string>; }; type ManifestEntry = { integrity?: string; revision: string | null; url: string; };
-
maximumFileSizeToCacheInBytes
(optional) - This value can be used to determine the maximum size of files that will be precached.This prevents you from inadvertently precaching very large files that might have accidentally matched one of your patterns.
Defaults to
2097152
(2MB).Type: number.
-
modifyURLPrefix
(optional) - An object mapping string prefixes to replacement string values.This can be used to, e.g., remove or add a path prefix from a manifest entry if your web hosting setup doesn't match your local filesystem setup.
As an alternative with more flexibility, you can use the
manifestTransforms
option and provide a function that modifies the entries in the manifest using whatever logic you provide.Type: object whose keys are strings and values are strings.
-
swDest
(required) - The name of the service worker file that will be The name of the service worker file that will be generated, e.g."serviceworker.js"
.This file is generated by rspack but needs to be specified in order to inject the manifest into it.
Note that this is name of the file generated by rspack, not the original service worker file.
Since this plugin relies on rspack's native handling of service workers, it does not support any of the features related to compiling service workers in Workbox.
Specifically the following options are not supported:
compileSrc
importScriptsViaChunks
mode
swSrc
webpackCompilationPlugins
Furthermore, swDest
is a required option in this plugin as we need to know
which asset to inject the manifest into (and which assets to automatically
exclude from the manifest).
The following projects all recreate Workbox's InjectManifest
functionality in
some form.
At the time of writing, however, I couldn't get any of them to behave correctly
with rspack's native service worker generation.
- workbox-rspack-plugin. Port of Workbox to rspack.
- @aaron/workbox-rspack-plugin. More up-to-date port.
inject-manifest-plugin
. A much leaner version of what Workbox'sInjectManifest
does (with different but similar options), adapted to work with both webpack and rspack.