workbox-window
Summary
- Types
- Methods
Types
Workbox
A class to aid in handling service worker registration, updates, and reacting to service worker lifecycle events.
Properties
- constructor
function
Creates a new Workbox instance with a script URL and service worker options. The script URL and options are the same as those used when calling navigator.serviceWorker.register(scriptURL, options).
The
constructor
function looks like:(scriptURL: string | TrustedScriptURL, registerOptions?: object) => {...}
- scriptURL
string | TrustedScriptURL
The service worker script associated with this instance. Using a
TrustedScriptURL
is supported. - registerOptions
object optional
- returns
- active
Promise<ServiceWorker>
- controlling
Promise<ServiceWorker>
- getSW
function
Resolves with a reference to a service worker that matches the script URL of this instance, as soon as it's available.
If, at registration time, there's already an active or waiting service worker with a matching script URL, it will be used (with the waiting service worker taking precedence over the active service worker if both match, since the waiting service worker would have been registered more recently). If there's no matching active or waiting service worker at registration time then the promise will not resolve until an update is found and starts installing, at which point the installing service worker is used.
The
getSW
function looks like:() => {...}
- returns
Promise<ServiceWorker>
- messageSW
function
Sends the passed data object to the service worker registered by this instance (via
workbox-window.Workbox#getSW
) and resolves with a response (if any).A response can be set in a message handler in the service worker by calling
event.ports[0].postMessage(...)
, which will resolve the promise returned bymessageSW()
. If no response is set, the promise will never resolve.The
messageSW
function looks like:(data: object) => {...}
- data
object
An object to send to the service worker
- returns
Promise<any>
- messageSkipWaiting
function
Sends a
{type: 'SKIP_WAITING'}
message to the service worker that's currently in thewaiting
state associated with the current registration.If there is no current registration or no service worker is
waiting
, calling this will have no effect.The
messageSkipWaiting
function looks like:() => {...}
- register
function
Registers a service worker for this instances script URL and service worker options. By default this method delays registration until after the window has loaded.
The
register
function looks like:(options?: object) => {...}
- options
object optional
- immediate
boolean optional
- returns
Promise<ServiceWorkerRegistration>
- update
function
Checks for updates of the registered service worker.
The
update
function looks like:() => {...}
- returns
Promise<void>
WorkboxEventMap
Properties
- activated
- activating
- controlling
- installed
- installing
- message
- redundant
- waiting
WorkboxLifecycleEvent
Properties
- isExternal
boolean optional
- isUpdate
boolean optional
- originalEvent
Event optional
- sw
ServiceWorker optional
- target
WorkboxEventTarget optional
- type
typeOperator
WorkboxLifecycleEventMap
Properties
- activated
- activating
- controlling
- installed
- installing
- redundant
- waiting
WorkboxLifecycleWaitingEvent
Properties
- isExternal
boolean optional
- isUpdate
boolean optional
- originalEvent
Event optional
- sw
ServiceWorker optional
- target
WorkboxEventTarget optional
- type
typeOperator
- wasWaitingBeforeRegister
boolean optional
WorkboxMessageEvent
Properties
- data
any
- isExternal
boolean optional
- originalEvent
Event
- ports
typeOperator
- sw
ServiceWorker optional
- target
WorkboxEventTarget optional
- type
"message"
Methods
messageSW
workbox-window.messageSW(
sw:
ServiceWorker,
data:
object,
)
Sends a data object to a service worker via postMessage
and resolves with a response (if any).
A response can be set in a message handler in the service worker by calling event.ports[0].postMessage(...)
, which will resolve the promise returned by messageSW()
. If no response is set, the promise will not resolve.
Parameters
- sw
ServiceWorker
The service worker to send the message to.
- data
object
An object to send to the service worker.
Returns
Promise<any>