The latest beta release of Workbox v4 includes the following developer-visible changes, in addition to all the changes from the previous pre-releases.
🎉 What's New?
workbox-window
- The
Workbox
constructor function signature has changed to accept an optionalscriptVersion
option. To support this new option (and other future options) the constructor now accepts a single object argument [#1861].
// Old way
new Workbox(scriptURL, registerOptions);
// New way
new Workbox({
scriptUrl,
scriptVersion,
registerOptions,
});
-
The properties
.active
and.controlling
have been added to theWorkbox
class. These are promises which will resolve as soon asWorkbox
has a reference to a service working in the corresponding state (active or controlling) with a matchingscriptURL
(and optionallyscriptVersion
, if used). These are similar to thenavigator.serviceWorker.ready
promise, but when usingscriptVersion
you can be sure they won't resolve until the correct version of the script is active/controlling [#1861]. -
Workbox
event listeners are now always called with anEvent
-like object (i.e. they have a.type
,.target
properties), to more closely match how native event listeners work. In the future when all browsers support contructableEventTarget
, these will be nativeEvent
objects [#1861].
// Old way
myWorkbox.addEventListener('activated', (sw) => {
// Do something with `sw`.
});
// New way
myWorkbox.addEventListener('activated', (event) => {
// The activated service worker is at `event.sw`.
// And the underlying event that triggered this is at `event.originalEvent`.
});
- The
controlling
event is now dispatched prior to theactivated
event, which correctly matches the ordering of when thecontrollerchange
andstatechange
events fire in the service worker lifecycle. [#1861]
workbox-routing
- The
workbox.routing.Router#addCacheListener()
method has updated the format off messages it can receive from the window to cache. Previously it would accept an array of URL strings, now it can also accept an array of URL strings or arrays in the form of[url, requestInit]
. This is useful if you need to override the default request mode [#1851]
Build tools
- Support for a new
boolean
configuration option,cleanupOutdatedCaches
, has been added to theGenerateSW
mode of all of the build tools. It defaults tofalse
. When set totrue
, a call toworkbox.precaching.cleanupOutdatedCaches()
will automatically be added to your generated service worker, which will in turn delete any out-of-date precaches no longer used by Workbox. Workbox v4 introduced a breaking change to the precache format, so developers upgrading from Workbox v3 or earlier might find this useful. [#1863]
🐛 What's Fixed?
workbox-webpack-plugin
- In addition to
swSrc
being a file on file system, it can now be a webpack generated asset as well. This allows users to compile their service worker with webpack and then give it as a source toinject-manifest
plugin. [#1763]
workbox-core
- Don't set fetchOptions when request.mode is 'navigate', to work around an issue that could lead to failed navigations. [#1862]