Have a voice in guiding the future of Google's developer brands. Share your thoughts in our survey.

Class: GenerateSW

Constructor

GenerateSW

new GenerateSW(config)

Creates an instance of GenerateSW.

Parameter

config

Object

The configuration to use.

Values in config have the following properties:

Parameter

additionalManifestEntries

Optional

Array of module:workbox-build.ManifestEntry

A list of entries to be precached, in addition to any entries that are generated as part of the build configuration.

babelPresetEnvTargets

Optional

Array of string

The targets to pass to babel-preset-env when transpiling the service worker bundle.

cacheId

Optional

string

An optional ID to be prepended to cache names. This is primarily useful for local development where multiple sites may be served from the same http://localhost:port origin.

cleanupOutdatedCaches

Optional

boolean

Whether or not Workbox should attempt to identify an delete any precaches created by older, incompatible versions.

clientsClaim

Optional

boolean

Whether or not the service worker should start controlling any existing clients as soon as it activates.

chunks

Optional

Array of string

One or more chunk names whose corresponding output files should be included in the precache manifest.

directoryIndex

Optional

string

If a navigation request for a URL ending in / fails to match a precached URL, this value will be appended to the URL and that will be checked for a precache match. This should be set to what your web server is using for its directory index.

dontCacheBustURLsMatching

Optional

RegExp

Assets that match this will be assumed to 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

Array of (string, RegExp, or function())

One or more specifiers used to exclude assets from the precache manifest. This is interpreted following the same rules as webpack's standard exclude option.

excludeChunks

Optional

Array of string

One or more chunk names whose corresponding output files should be excluded from the precache manifest.

ignoreURLParametersMatching

Optional

Array of RegExp

Any search parameter names that match against one of the RegExp in this array will be removed before looking for a precache match. This is useful if your users might request URLs that contain, for example, URL parameters used to track the source of the traffic.

importScripts

Optional

Array of string

A list of JavaScript files that should be passed to importScripts() inside the generated service worker file. This is useful when you want to let Workbox create your top-level service worker file, but want to include some additional code, such as a push event listener.

importScriptsViaChunks

Optional

Array of string

One or more names of webpack chunks. The content of those chunks will be included in the generated service worker, via a call to importScripts().

include

Optional

Array of (string, RegExp, or function())

One or more specifiers used to include assets in the precache manifest. This is interpreted following the same rules as webpack's standard include option.

inlineWorkboxRuntime

Optional

boolean

Whether the runtime code for the Workbox library should be included in the top-level service worker, or split into a separate file that needs to be deployed alongside the service worker. Keeping the runtime separate means that users will not have to re-download the Workbox code each time your top-level service worker changes.

manifestTransforms

Optional

Array of module:workbox-build.ManifestTransform

One or more functions which will be applied sequentially against the generated manifest. If modifyURLPrefix or dontCacheBustURLsMatching are also specified, their corresponding transformations will be applied first.

maximumFileSizeToCacheInBytes

Optional

number

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.

mode

Optional

string

If set to 'production', then an optimized service worker bundle that excludes debugging info will be produced. If not explicitly configured here, the mode value configured in the current webpack compiltion will be used.

modifyURLPrefix

Optional

Object with string properties

A mapping of prefixes that, if present in an entry in the precache manifest, will be replaced with the corresponding value. This can be used to, for example, 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.

navigateFallback

Optional

string

If specified, all navigation requests for URLs that aren't precached will be fulfilled with the HTML at the URL provided. You must pass in the URL of an HTML document that is listed in your precache manifest. This is meant to be used in a Single Page App scenario, in which you want all navigations to use common App Shell HTML.

navigateFallbackDenylist

Optional

Array of RegExp

An optional array of regular expressions that restricts which URLs the configured navigateFallback behavior applies to. This is useful if only a subset of your site's URLs should be treated as being part of a Single Page App. If both navigateFallbackDenylist and navigateFallbackAllowlist are configured, the denylist takes precedent.

navigateFallbackAllowlist

Optional

Array of RegExp

An optional array of regular expressions that restricts which URLs the configured navigateFallback behavior applies to. This is useful if only a subset of your site's URLs should be treated as being part of a Single Page App. If both navigateFallbackDenylist and navigateFallbackAllowlist are configured, the denylist takes precedent.

navigationPreload

Optional

boolean

Whether or not to enable navigation preload in the generated service worker. When set to true, you must also use runtimeCaching to set up an appropriate response strategy that will match navigation requests, and make use of the preloaded response.

offlineGoogleAnalytics

Optional

(boolean or Object)

Controls whether or not to include support for offline Google Analytics. When true, the call to workbox-google-analytics's initialize() will be added to your generated service worker. When set to an Object, that object will be passed in to the initialize() call, allowing you to customize the behavior.

runtimeCaching

Optional

Array of module:workbox-build.RuntimeCachingEntry

skipWaiting

Optional

boolean

Whether to add an unconditional call to skipWaiting() to the generated service worker. If false, then a message listener will be added instead, allowing you to conditionally call skipWaiting().

sourcemap

Optional

boolean

Whether to create a sourcemap for the generated service worker files.

swDest

Optional

string

The asset name of the service worker file that will be created by this plugin.