| 1 | import { TrustedScriptURL } from 'trusted-types/lib';
|
|---|
| 2 | import { WorkboxEventTarget } from './utils/WorkboxEventTarget.js';
|
|---|
| 3 | import './_version.js';
|
|---|
| 4 | /**
|
|---|
| 5 | * A class to aid in handling service worker registration, updates, and
|
|---|
| 6 | * reacting to service worker lifecycle events.
|
|---|
| 7 | *
|
|---|
| 8 | * @fires {@link workbox-window.Workbox#message}
|
|---|
| 9 | * @fires {@link workbox-window.Workbox#installed}
|
|---|
| 10 | * @fires {@link workbox-window.Workbox#waiting}
|
|---|
| 11 | * @fires {@link workbox-window.Workbox#controlling}
|
|---|
| 12 | * @fires {@link workbox-window.Workbox#activated}
|
|---|
| 13 | * @fires {@link workbox-window.Workbox#redundant}
|
|---|
| 14 | * @memberof workbox-window
|
|---|
| 15 | */
|
|---|
| 16 | declare class Workbox extends WorkboxEventTarget {
|
|---|
| 17 | private readonly _scriptURL;
|
|---|
| 18 | private readonly _registerOptions;
|
|---|
| 19 | private _updateFoundCount;
|
|---|
| 20 | private readonly _swDeferred;
|
|---|
| 21 | private readonly _activeDeferred;
|
|---|
| 22 | private readonly _controllingDeferred;
|
|---|
| 23 | private _registrationTime;
|
|---|
| 24 | private _isUpdate?;
|
|---|
| 25 | private _compatibleControllingSW?;
|
|---|
| 26 | private _registration?;
|
|---|
| 27 | private _sw?;
|
|---|
| 28 | private readonly _ownSWs;
|
|---|
| 29 | private _externalSW?;
|
|---|
| 30 | private _waitingTimeout?;
|
|---|
| 31 | /**
|
|---|
| 32 | * Creates a new Workbox instance with a script URL and service worker
|
|---|
| 33 | * options. The script URL and options are the same as those used when
|
|---|
| 34 | * calling [navigator.serviceWorker.register(scriptURL, options)](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register).
|
|---|
| 35 | *
|
|---|
| 36 | * @param {string|TrustedScriptURL} scriptURL The service worker script
|
|---|
| 37 | * associated with this instance. Using a
|
|---|
| 38 | * [`TrustedScriptURL`](https://web.dev/trusted-types/) is supported.
|
|---|
| 39 | * @param {Object} [registerOptions] The service worker options associated
|
|---|
| 40 | * with this instance.
|
|---|
| 41 | */
|
|---|
| 42 | constructor(scriptURL: string | TrustedScriptURL, registerOptions?: {});
|
|---|
| 43 | /**
|
|---|
| 44 | * Registers a service worker for this instances script URL and service
|
|---|
| 45 | * worker options. By default this method delays registration until after
|
|---|
| 46 | * the window has loaded.
|
|---|
| 47 | *
|
|---|
| 48 | * @param {Object} [options]
|
|---|
| 49 | * @param {Function} [options.immediate=false] Setting this to true will
|
|---|
| 50 | * register the service worker immediately, even if the window has
|
|---|
| 51 | * not loaded (not recommended).
|
|---|
| 52 | */
|
|---|
| 53 | register({ immediate }?: {
|
|---|
| 54 | immediate?: boolean | undefined;
|
|---|
| 55 | }): Promise<ServiceWorkerRegistration | undefined>;
|
|---|
| 56 | /**
|
|---|
| 57 | * Checks for updates of the registered service worker.
|
|---|
| 58 | */
|
|---|
| 59 | update(): Promise<void>;
|
|---|
| 60 | /**
|
|---|
| 61 | * Resolves to the service worker registered by this instance as soon as it
|
|---|
| 62 | * is active. If a service worker was already controlling at registration
|
|---|
| 63 | * time then it will resolve to that if the script URLs (and optionally
|
|---|
| 64 | * script versions) match, otherwise it will wait until an update is found
|
|---|
| 65 | * and activates.
|
|---|
| 66 | *
|
|---|
| 67 | * @return {Promise<ServiceWorker>}
|
|---|
| 68 | */
|
|---|
| 69 | get active(): Promise<ServiceWorker>;
|
|---|
| 70 | /**
|
|---|
| 71 | * Resolves to the service worker registered by this instance as soon as it
|
|---|
| 72 | * is controlling the page. If a service worker was already controlling at
|
|---|
| 73 | * registration time then it will resolve to that if the script URLs (and
|
|---|
| 74 | * optionally script versions) match, otherwise it will wait until an update
|
|---|
| 75 | * is found and starts controlling the page.
|
|---|
| 76 | * Note: the first time a service worker is installed it will active but
|
|---|
| 77 | * not start controlling the page unless `clients.claim()` is called in the
|
|---|
| 78 | * service worker.
|
|---|
| 79 | *
|
|---|
| 80 | * @return {Promise<ServiceWorker>}
|
|---|
| 81 | */
|
|---|
| 82 | get controlling(): Promise<ServiceWorker>;
|
|---|
| 83 | /**
|
|---|
| 84 | * Resolves with a reference to a service worker that matches the script URL
|
|---|
| 85 | * of this instance, as soon as it's available.
|
|---|
| 86 | *
|
|---|
| 87 | * If, at registration time, there's already an active or waiting service
|
|---|
| 88 | * worker with a matching script URL, it will be used (with the waiting
|
|---|
| 89 | * service worker taking precedence over the active service worker if both
|
|---|
| 90 | * match, since the waiting service worker would have been registered more
|
|---|
| 91 | * recently).
|
|---|
| 92 | * If there's no matching active or waiting service worker at registration
|
|---|
| 93 | * time then the promise will not resolve until an update is found and starts
|
|---|
| 94 | * installing, at which point the installing service worker is used.
|
|---|
| 95 | *
|
|---|
| 96 | * @return {Promise<ServiceWorker>}
|
|---|
| 97 | */
|
|---|
| 98 | getSW(): Promise<ServiceWorker>;
|
|---|
| 99 | /**
|
|---|
| 100 | * Sends the passed data object to the service worker registered by this
|
|---|
| 101 | * instance (via {@link workbox-window.Workbox#getSW}) and resolves
|
|---|
| 102 | * with a response (if any).
|
|---|
| 103 | *
|
|---|
| 104 | * A response can be set in a message handler in the service worker by
|
|---|
| 105 | * calling `event.ports[0].postMessage(...)`, which will resolve the promise
|
|---|
| 106 | * returned by `messageSW()`. If no response is set, the promise will never
|
|---|
| 107 | * resolve.
|
|---|
| 108 | *
|
|---|
| 109 | * @param {Object} data An object to send to the service worker
|
|---|
| 110 | * @return {Promise<Object>}
|
|---|
| 111 | */
|
|---|
| 112 | messageSW(data: object): Promise<any>;
|
|---|
| 113 | /**
|
|---|
| 114 | * Sends a `{type: 'SKIP_WAITING'}` message to the service worker that's
|
|---|
| 115 | * currently in the `waiting` state associated with the current registration.
|
|---|
| 116 | *
|
|---|
| 117 | * If there is no current registration or no service worker is `waiting`,
|
|---|
| 118 | * calling this will have no effect.
|
|---|
| 119 | */
|
|---|
| 120 | messageSkipWaiting(): void;
|
|---|
| 121 | /**
|
|---|
| 122 | * Checks for a service worker already controlling the page and returns
|
|---|
| 123 | * it if its script URL matches.
|
|---|
| 124 | *
|
|---|
| 125 | * @private
|
|---|
| 126 | * @return {ServiceWorker|undefined}
|
|---|
| 127 | */
|
|---|
| 128 | private _getControllingSWIfCompatible;
|
|---|
| 129 | /**
|
|---|
| 130 | * Registers a service worker for this instances script URL and register
|
|---|
| 131 | * options and tracks the time registration was complete.
|
|---|
| 132 | *
|
|---|
| 133 | * @private
|
|---|
| 134 | */
|
|---|
| 135 | private _registerScript;
|
|---|
| 136 | /**
|
|---|
| 137 | * @private
|
|---|
| 138 | */
|
|---|
| 139 | private readonly _onUpdateFound;
|
|---|
| 140 | /**
|
|---|
| 141 | * @private
|
|---|
| 142 | * @param {Event} originalEvent
|
|---|
| 143 | */
|
|---|
| 144 | private readonly _onStateChange;
|
|---|
| 145 | /**
|
|---|
| 146 | * @private
|
|---|
| 147 | * @param {Event} originalEvent
|
|---|
| 148 | */
|
|---|
| 149 | private readonly _onControllerChange;
|
|---|
| 150 | /**
|
|---|
| 151 | * @private
|
|---|
| 152 | * @param {Event} originalEvent
|
|---|
| 153 | */
|
|---|
| 154 | private readonly _onMessage;
|
|---|
| 155 | }
|
|---|
| 156 | export { Workbox };
|
|---|
| 157 | /**
|
|---|
| 158 | * The `message` event is dispatched any time a `postMessage` is received.
|
|---|
| 159 | *
|
|---|
| 160 | * @event workbox-window.Workbox#message
|
|---|
| 161 | * @type {WorkboxEvent}
|
|---|
| 162 | * @property {*} data The `data` property from the original `message` event.
|
|---|
| 163 | * @property {Event} originalEvent The original [`message`]{@link https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent}
|
|---|
| 164 | * event.
|
|---|
| 165 | * @property {string} type `message`.
|
|---|
| 166 | * @property {MessagePort[]} ports The `ports` value from `originalEvent`.
|
|---|
| 167 | * @property {Workbox} target The `Workbox` instance.
|
|---|
| 168 | */
|
|---|
| 169 | /**
|
|---|
| 170 | * The `installed` event is dispatched if the state of a
|
|---|
| 171 | * {@link workbox-window.Workbox} instance's
|
|---|
| 172 | * {@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw|registered service worker}
|
|---|
| 173 | * changes to `installed`.
|
|---|
| 174 | *
|
|---|
| 175 | * Then can happen either the very first time a service worker is installed,
|
|---|
| 176 | * or after an update to the current service worker is found. In the case
|
|---|
| 177 | * of an update being found, the event's `isUpdate` property will be `true`.
|
|---|
| 178 | *
|
|---|
| 179 | * @event workbox-window.Workbox#installed
|
|---|
| 180 | * @type {WorkboxEvent}
|
|---|
| 181 | * @property {ServiceWorker} sw The service worker instance.
|
|---|
| 182 | * @property {Event} originalEvent The original [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
|
|---|
| 183 | * event.
|
|---|
| 184 | * @property {boolean|undefined} isUpdate True if a service worker was already
|
|---|
| 185 | * controlling when this `Workbox` instance called `register()`.
|
|---|
| 186 | * @property {boolean|undefined} isExternal True if this event is associated
|
|---|
| 187 | * with an [external service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-window#when_an_unexpected_version_of_the_service_worker_is_found}.
|
|---|
| 188 | * @property {string} type `installed`.
|
|---|
| 189 | * @property {Workbox} target The `Workbox` instance.
|
|---|
| 190 | */
|
|---|
| 191 | /**
|
|---|
| 192 | * The `waiting` event is dispatched if the state of a
|
|---|
| 193 | * {@link workbox-window.Workbox} instance's
|
|---|
| 194 | * [registered service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw}
|
|---|
| 195 | * changes to `installed` and then doesn't immediately change to `activating`.
|
|---|
| 196 | * It may also be dispatched if a service worker with the same
|
|---|
| 197 | * [`scriptURL`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/scriptURL}
|
|---|
| 198 | * was already waiting when the {@link workbox-window.Workbox#register}
|
|---|
| 199 | * method was called.
|
|---|
| 200 | *
|
|---|
| 201 | * @event workbox-window.Workbox#waiting
|
|---|
| 202 | * @type {WorkboxEvent}
|
|---|
| 203 | * @property {ServiceWorker} sw The service worker instance.
|
|---|
| 204 | * @property {Event|undefined} originalEvent The original
|
|---|
| 205 | * [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
|
|---|
| 206 | * event, or `undefined` in the case where the service worker was waiting
|
|---|
| 207 | * to before `.register()` was called.
|
|---|
| 208 | * @property {boolean|undefined} isUpdate True if a service worker was already
|
|---|
| 209 | * controlling when this `Workbox` instance called `register()`.
|
|---|
| 210 | * @property {boolean|undefined} isExternal True if this event is associated
|
|---|
| 211 | * with an [external service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-window#when_an_unexpected_version_of_the_service_worker_is_found}.
|
|---|
| 212 | * @property {boolean|undefined} wasWaitingBeforeRegister True if a service worker with
|
|---|
| 213 | * a matching `scriptURL` was already waiting when this `Workbox`
|
|---|
| 214 | * instance called `register()`.
|
|---|
| 215 | * @property {string} type `waiting`.
|
|---|
| 216 | * @property {Workbox} target The `Workbox` instance.
|
|---|
| 217 | */
|
|---|
| 218 | /**
|
|---|
| 219 | * The `controlling` event is dispatched if a
|
|---|
| 220 | * [`controllerchange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/oncontrollerchange}
|
|---|
| 221 | * fires on the service worker [container]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer}
|
|---|
| 222 | * and the [`scriptURL`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/scriptURL}
|
|---|
| 223 | * of the new [controller]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/controller}
|
|---|
| 224 | * matches the `scriptURL` of the `Workbox` instance's
|
|---|
| 225 | * [registered service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw}.
|
|---|
| 226 | *
|
|---|
| 227 | * @event workbox-window.Workbox#controlling
|
|---|
| 228 | * @type {WorkboxEvent}
|
|---|
| 229 | * @property {ServiceWorker} sw The service worker instance.
|
|---|
| 230 | * @property {Event} originalEvent The original [`controllerchange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/oncontrollerchange}
|
|---|
| 231 | * event.
|
|---|
| 232 | * @property {boolean|undefined} isUpdate True if a service worker was already
|
|---|
| 233 | * controlling when this service worker was registered.
|
|---|
| 234 | * @property {boolean|undefined} isExternal True if this event is associated
|
|---|
| 235 | * with an [external service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-window#when_an_unexpected_version_of_the_service_worker_is_found}.
|
|---|
| 236 | * @property {string} type `controlling`.
|
|---|
| 237 | * @property {Workbox} target The `Workbox` instance.
|
|---|
| 238 | */
|
|---|
| 239 | /**
|
|---|
| 240 | * The `activated` event is dispatched if the state of a
|
|---|
| 241 | * {@link workbox-window.Workbox} instance's
|
|---|
| 242 | * {@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw|registered service worker}
|
|---|
| 243 | * changes to `activated`.
|
|---|
| 244 | *
|
|---|
| 245 | * @event workbox-window.Workbox#activated
|
|---|
| 246 | * @type {WorkboxEvent}
|
|---|
| 247 | * @property {ServiceWorker} sw The service worker instance.
|
|---|
| 248 | * @property {Event} originalEvent The original [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
|
|---|
| 249 | * event.
|
|---|
| 250 | * @property {boolean|undefined} isUpdate True if a service worker was already
|
|---|
| 251 | * controlling when this `Workbox` instance called `register()`.
|
|---|
| 252 | * @property {boolean|undefined} isExternal True if this event is associated
|
|---|
| 253 | * with an [external service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-window#when_an_unexpected_version_of_the_service_worker_is_found}.
|
|---|
| 254 | * @property {string} type `activated`.
|
|---|
| 255 | * @property {Workbox} target The `Workbox` instance.
|
|---|
| 256 | */
|
|---|
| 257 | /**
|
|---|
| 258 | * The `redundant` event is dispatched if the state of a
|
|---|
| 259 | * {@link workbox-window.Workbox} instance's
|
|---|
| 260 | * [registered service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw}
|
|---|
| 261 | * changes to `redundant`.
|
|---|
| 262 | *
|
|---|
| 263 | * @event workbox-window.Workbox#redundant
|
|---|
| 264 | * @type {WorkboxEvent}
|
|---|
| 265 | * @property {ServiceWorker} sw The service worker instance.
|
|---|
| 266 | * @property {Event} originalEvent The original [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
|
|---|
| 267 | * event.
|
|---|
| 268 | * @property {boolean|undefined} isUpdate True if a service worker was already
|
|---|
| 269 | * controlling when this `Workbox` instance called `register()`.
|
|---|
| 270 | * @property {string} type `redundant`.
|
|---|
| 271 | * @property {Workbox} target The `Workbox` instance.
|
|---|
| 272 | */
|
|---|