@vite-pwa/workbox-window 8.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +19 -0
- package/README.md +80 -0
- package/dist/index.cjs +583 -0
- package/dist/index.d.cts +373 -0
- package/dist/index.d.ts +373 -0
- package/dist/index.js +554 -0
- package/package.json +57 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
import { TrustedScriptURL } from 'trusted-types/lib';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Sends a data object to a service worker via `postMessage` and resolves with
|
|
5
|
+
* a response (if any).
|
|
6
|
+
*
|
|
7
|
+
* A response can be set in a message handler in the service worker by
|
|
8
|
+
* calling `event.ports[0].postMessage(...)`, which will resolve the promise
|
|
9
|
+
* returned by `messageSW()`. If no response is set, the promise will not
|
|
10
|
+
* resolve.
|
|
11
|
+
*
|
|
12
|
+
* @param {ServiceWorker} sw The service worker to send the message to.
|
|
13
|
+
* @param {object} data An object to send to the service worker.
|
|
14
|
+
* @return {Promise<object | undefined>}
|
|
15
|
+
*/
|
|
16
|
+
declare function messageSW(sw: ServiceWorker, data: any): Promise<unknown>;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A minimal `Event` subclass shim.
|
|
20
|
+
* This doesn't *actually* subclass `Event` because not all browsers support
|
|
21
|
+
* constructable `EventTarget`, and using a real `Event` will error.
|
|
22
|
+
* @private
|
|
23
|
+
*/
|
|
24
|
+
declare class WorkboxEvent<K extends keyof WorkboxEventMap> {
|
|
25
|
+
type: K;
|
|
26
|
+
target?: WorkboxEventTarget;
|
|
27
|
+
sw?: ServiceWorker;
|
|
28
|
+
originalEvent?: Event;
|
|
29
|
+
isExternal?: boolean;
|
|
30
|
+
constructor(type: K, props: Omit<WorkboxEventMap[K], 'target' | 'type'>);
|
|
31
|
+
}
|
|
32
|
+
interface WorkboxMessageEvent extends WorkboxEvent<'message'> {
|
|
33
|
+
data: any;
|
|
34
|
+
originalEvent: Event;
|
|
35
|
+
ports: readonly MessagePort[];
|
|
36
|
+
}
|
|
37
|
+
interface WorkboxLifecycleEvent extends WorkboxEvent<keyof WorkboxLifecycleEventMap> {
|
|
38
|
+
isUpdate?: boolean;
|
|
39
|
+
}
|
|
40
|
+
interface WorkboxLifecycleWaitingEvent extends WorkboxLifecycleEvent {
|
|
41
|
+
wasWaitingBeforeRegister?: boolean;
|
|
42
|
+
}
|
|
43
|
+
interface WorkboxLifecycleEventMap {
|
|
44
|
+
/**
|
|
45
|
+
* The `installing` event is dispatched if the service-worker
|
|
46
|
+
* find the new version and start installing.
|
|
47
|
+
*
|
|
48
|
+
* @event workbox-window.Workbox#installing
|
|
49
|
+
* @type {WorkboxEvent}
|
|
50
|
+
* @property {ServiceWorker} sw The installing service worker instance.
|
|
51
|
+
* @property {Event} originalEvent The original [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
|
|
52
|
+
* event.
|
|
53
|
+
* @property {string} type `installing`.
|
|
54
|
+
* @property {Workbox} target The `Workbox` instance.
|
|
55
|
+
*/
|
|
56
|
+
installing: WorkboxLifecycleEvent;
|
|
57
|
+
/**
|
|
58
|
+
* The `installed` event is dispatched if the state of a
|
|
59
|
+
* {@link workbox-window.Workbox} instance's
|
|
60
|
+
* {@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw|registered service worker}
|
|
61
|
+
* changes to `installed`.
|
|
62
|
+
*
|
|
63
|
+
* Then can happen either the very first time a service worker is installed,
|
|
64
|
+
* or after an update to the current service worker is found. In the case
|
|
65
|
+
* of an update being found, the event's `isUpdate` property will be `true`.
|
|
66
|
+
*
|
|
67
|
+
* @event workbox-window.Workbox#installed
|
|
68
|
+
* @type {WorkboxEvent}
|
|
69
|
+
* @property {ServiceWorker} sw The service worker instance.
|
|
70
|
+
* @property {Event} originalEvent The original [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
|
|
71
|
+
* event.
|
|
72
|
+
* @property {boolean|undefined} isUpdate True if a service worker was already
|
|
73
|
+
* controlling when this `Workbox` instance called `register()`.
|
|
74
|
+
* @property {boolean|undefined} isExternal True if this event is associated
|
|
75
|
+
* 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}.
|
|
76
|
+
* @property {string} type `installed`.
|
|
77
|
+
* @property {Workbox} target The `Workbox` instance.
|
|
78
|
+
*/
|
|
79
|
+
installed: WorkboxLifecycleEvent;
|
|
80
|
+
/**
|
|
81
|
+
* The `waiting` event is dispatched if the state of a
|
|
82
|
+
* {@link workbox-window.Workbox} instance's
|
|
83
|
+
* [registered service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw}
|
|
84
|
+
* changes to `installed` and then doesn't immediately change to `activating`.
|
|
85
|
+
* It may also be dispatched if a service worker with the same
|
|
86
|
+
* [`scriptURL`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/scriptURL}
|
|
87
|
+
* was already waiting when the {@link workbox-window.Workbox#register}
|
|
88
|
+
* method was called.
|
|
89
|
+
*
|
|
90
|
+
* @event workbox-window.Workbox#waiting
|
|
91
|
+
* @type {WorkboxEvent}
|
|
92
|
+
* @property {ServiceWorker} sw The service worker instance.
|
|
93
|
+
* @property {Event|undefined} originalEvent The original
|
|
94
|
+
* [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
|
|
95
|
+
* event, or `undefined` in the case where the service worker was waiting
|
|
96
|
+
* to before `.register()` was called.
|
|
97
|
+
* @property {boolean|undefined} isUpdate True if a service worker was already
|
|
98
|
+
* controlling when this `Workbox` instance called `register()`.
|
|
99
|
+
* @property {boolean|undefined} isExternal True if this event is associated
|
|
100
|
+
* 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}.
|
|
101
|
+
* @property {boolean|undefined} wasWaitingBeforeRegister True if a service worker with
|
|
102
|
+
* a matching `scriptURL` was already waiting when this `Workbox`
|
|
103
|
+
* instance called `register()`.
|
|
104
|
+
* @property {string} type `waiting`.
|
|
105
|
+
* @property {Workbox} target The `Workbox` instance.
|
|
106
|
+
*/
|
|
107
|
+
waiting: WorkboxLifecycleWaitingEvent;
|
|
108
|
+
activating: WorkboxLifecycleEvent;
|
|
109
|
+
/**
|
|
110
|
+
* The `activated` event is dispatched if the state of a
|
|
111
|
+
* {@link workbox-window.Workbox} instance's
|
|
112
|
+
* {@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw|registered service worker}
|
|
113
|
+
* changes to `activated`.
|
|
114
|
+
*
|
|
115
|
+
* @event workbox-window.Workbox#activated
|
|
116
|
+
* @type {WorkboxEvent}
|
|
117
|
+
* @property {ServiceWorker} sw The service worker instance.
|
|
118
|
+
* @property {Event} originalEvent The original [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
|
|
119
|
+
* event.
|
|
120
|
+
* @property {boolean|undefined} isUpdate True if a service worker was already
|
|
121
|
+
* controlling when this `Workbox` instance called `register()`.
|
|
122
|
+
* @property {boolean|undefined} isExternal True if this event is associated
|
|
123
|
+
* 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}.
|
|
124
|
+
* @property {string} type `activated`.
|
|
125
|
+
* @property {Workbox} target The `Workbox` instance.
|
|
126
|
+
*/
|
|
127
|
+
activated: WorkboxLifecycleEvent;
|
|
128
|
+
/**
|
|
129
|
+
* The `controlling` event is dispatched if a
|
|
130
|
+
* [`controllerchange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/oncontrollerchange}
|
|
131
|
+
* fires on the service worker [container]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer}
|
|
132
|
+
* and the [`scriptURL`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/scriptURL}
|
|
133
|
+
* of the new [controller]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/controller}
|
|
134
|
+
* matches the `scriptURL` of the `Workbox` instance's
|
|
135
|
+
* [registered service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw}.
|
|
136
|
+
*
|
|
137
|
+
* @event workbox-window.Workbox#controlling
|
|
138
|
+
* @type {WorkboxEvent}
|
|
139
|
+
* @property {ServiceWorker} sw The service worker instance.
|
|
140
|
+
* @property {Event} originalEvent The original [`controllerchange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/oncontrollerchange}
|
|
141
|
+
* event.
|
|
142
|
+
* @property {boolean|undefined} isUpdate True if a service worker was already
|
|
143
|
+
* controlling when this service worker was registered.
|
|
144
|
+
* @property {boolean|undefined} isExternal True if this event is associated
|
|
145
|
+
* 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}.
|
|
146
|
+
* @property {string} type `controlling`.
|
|
147
|
+
* @property {Workbox} target The `Workbox` instance.
|
|
148
|
+
*/
|
|
149
|
+
controlling: WorkboxLifecycleEvent;
|
|
150
|
+
/**
|
|
151
|
+
* The `redundant` event is dispatched if the state of a
|
|
152
|
+
* {@link workbox-window.Workbox} instance's
|
|
153
|
+
* [registered service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw}
|
|
154
|
+
* changes to `redundant`.
|
|
155
|
+
*
|
|
156
|
+
* @event workbox-window.Workbox#redundant
|
|
157
|
+
* @type {WorkboxEvent}
|
|
158
|
+
* @property {ServiceWorker} sw The service worker instance.
|
|
159
|
+
* @property {Event} originalEvent The original [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
|
|
160
|
+
* event.
|
|
161
|
+
* @property {boolean|undefined} isUpdate True if a service worker was already
|
|
162
|
+
* controlling when this `Workbox` instance called `register()`.
|
|
163
|
+
* @property {string} type `redundant`.
|
|
164
|
+
* @property {Workbox} target The `Workbox` instance.
|
|
165
|
+
*/
|
|
166
|
+
redundant: WorkboxLifecycleEvent;
|
|
167
|
+
}
|
|
168
|
+
interface WorkboxEventMap extends WorkboxLifecycleEventMap {
|
|
169
|
+
/**
|
|
170
|
+
* The `message` event is dispatched any time a `postMessage` is received.
|
|
171
|
+
*
|
|
172
|
+
* @event workbox-window.Workbox#message
|
|
173
|
+
* @type {WorkboxEvent}
|
|
174
|
+
* @property {*} data The `data` property from the original `message` event.
|
|
175
|
+
* @property {Event} originalEvent The original [`message`]{@link https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent}
|
|
176
|
+
* event.
|
|
177
|
+
* @property {string} type `message`.
|
|
178
|
+
* @property {MessagePort[]} ports The `ports` value from `originalEvent`.
|
|
179
|
+
* @property {Workbox} target The `Workbox` instance.
|
|
180
|
+
*/
|
|
181
|
+
message: WorkboxMessageEvent;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* A minimal `EventTarget` shim.
|
|
186
|
+
* This is necessary because not all browsers support constructable
|
|
187
|
+
* `EventTarget`, so using a real `EventTarget` will error.
|
|
188
|
+
* @private
|
|
189
|
+
*/
|
|
190
|
+
declare class WorkboxEventTarget {
|
|
191
|
+
private readonly _eventListenerRegistry;
|
|
192
|
+
/**
|
|
193
|
+
* @param {string} type
|
|
194
|
+
* @param {Function} listener
|
|
195
|
+
* @private
|
|
196
|
+
*/
|
|
197
|
+
addEventListener<K extends keyof WorkboxEventMap>(type: K, listener: (event: WorkboxEventMap[K]) => any): void;
|
|
198
|
+
/**
|
|
199
|
+
* @param {string} type
|
|
200
|
+
* @param {Function} listener
|
|
201
|
+
* @private
|
|
202
|
+
*/
|
|
203
|
+
removeEventListener<K extends keyof WorkboxEventMap>(type: K, listener: (event: WorkboxEventMap[K]) => any): void;
|
|
204
|
+
/**
|
|
205
|
+
* @param {object} event
|
|
206
|
+
* @private
|
|
207
|
+
*/
|
|
208
|
+
dispatchEvent(event: WorkboxEvent<any>): void;
|
|
209
|
+
/**
|
|
210
|
+
* Returns a Set of listeners associated with the passed event type.
|
|
211
|
+
* If no handlers have been registered, an empty Set is returned.
|
|
212
|
+
*
|
|
213
|
+
* @param {string} type The event type.
|
|
214
|
+
* @return {Set<ListenerCallback>} An array of handler functions.
|
|
215
|
+
* @private
|
|
216
|
+
*/
|
|
217
|
+
private _getEventListenersByType;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* A class to aid in handling service worker registration, updates, and
|
|
222
|
+
* reacting to service worker lifecycle events.
|
|
223
|
+
*
|
|
224
|
+
* @fires WorkboxEventMap#message
|
|
225
|
+
* @fires WorkboxLifecycleEventMap#installing
|
|
226
|
+
* @fires WorkboxLifecycleEventMap#installed
|
|
227
|
+
* @fires WorkboxLifecycleEventMap#waiting
|
|
228
|
+
* @fires WorkboxLifecycleEventMap#controlling
|
|
229
|
+
* @fires WorkboxLifecycleEventMap#activated
|
|
230
|
+
* @fires WorkboxLifecycleEventMap#redundant
|
|
231
|
+
*/
|
|
232
|
+
declare class Workbox extends WorkboxEventTarget {
|
|
233
|
+
private readonly _scriptURL;
|
|
234
|
+
private readonly _registerOptions;
|
|
235
|
+
private _updateFoundCount;
|
|
236
|
+
private readonly _swDeferred;
|
|
237
|
+
private readonly _activeDeferred;
|
|
238
|
+
private readonly _controllingDeferred;
|
|
239
|
+
private _registrationTime;
|
|
240
|
+
private _isUpdate?;
|
|
241
|
+
private _compatibleControllingSW?;
|
|
242
|
+
private _registration?;
|
|
243
|
+
private _sw?;
|
|
244
|
+
private readonly _ownSWs;
|
|
245
|
+
private _externalSW?;
|
|
246
|
+
private _waitingTimeout?;
|
|
247
|
+
/**
|
|
248
|
+
* Creates a new Workbox instance with a script URL and service worker
|
|
249
|
+
* options. The script URL and options are the same as those used when
|
|
250
|
+
* calling [navigator.serviceWorker.register(scriptURL, options)](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register).
|
|
251
|
+
*
|
|
252
|
+
* @param {string|TrustedScriptURL} scriptURL The service worker script
|
|
253
|
+
* associated with this instance. Using a
|
|
254
|
+
* [`TrustedScriptURL`](https://web.dev/trusted-types/) is supported.
|
|
255
|
+
* @param {object} [registerOptions] The service worker options associated
|
|
256
|
+
* with this instance.
|
|
257
|
+
*/
|
|
258
|
+
constructor(scriptURL: string | TrustedScriptURL, registerOptions?: {});
|
|
259
|
+
/**
|
|
260
|
+
* Registers a service worker for this instances script URL and service
|
|
261
|
+
* worker options. By default this method delays registration until after
|
|
262
|
+
* the window has loaded.
|
|
263
|
+
*
|
|
264
|
+
* @param {object} [options]
|
|
265
|
+
* @param {Function} [options.immediate] Setting this to true will
|
|
266
|
+
* register the service worker immediately, even if the window has
|
|
267
|
+
* not loaded (not recommended).
|
|
268
|
+
*/
|
|
269
|
+
register({ immediate }?: {
|
|
270
|
+
immediate?: boolean | undefined;
|
|
271
|
+
}): Promise<ServiceWorkerRegistration | undefined>;
|
|
272
|
+
/**
|
|
273
|
+
* Checks for updates of the registered service worker.
|
|
274
|
+
*/
|
|
275
|
+
update(): Promise<void>;
|
|
276
|
+
/**
|
|
277
|
+
* Resolves to the service worker registered by this instance as soon as it
|
|
278
|
+
* is active. If a service worker was already controlling at registration
|
|
279
|
+
* time then it will resolve to that if the script URLs (and optionally
|
|
280
|
+
* script versions) match, otherwise it will wait until an update is found
|
|
281
|
+
* and activates.
|
|
282
|
+
*
|
|
283
|
+
* @return {Promise<ServiceWorker>}
|
|
284
|
+
*/
|
|
285
|
+
get active(): Promise<ServiceWorker>;
|
|
286
|
+
/**
|
|
287
|
+
* Resolves to the service worker registered by this instance as soon as it
|
|
288
|
+
* is controlling the page. If a service worker was already controlling at
|
|
289
|
+
* registration time then it will resolve to that if the script URLs (and
|
|
290
|
+
* optionally script versions) match, otherwise it will wait until an update
|
|
291
|
+
* is found and starts controlling the page.
|
|
292
|
+
* Note: the first time a service worker is installed it will active but
|
|
293
|
+
* not start controlling the page unless `clients.claim()` is called in the
|
|
294
|
+
* service worker.
|
|
295
|
+
*
|
|
296
|
+
* @return {Promise<ServiceWorker>}
|
|
297
|
+
*/
|
|
298
|
+
get controlling(): Promise<ServiceWorker>;
|
|
299
|
+
/**
|
|
300
|
+
* Resolves with a reference to a service worker that matches the script URL
|
|
301
|
+
* of this instance, as soon as it's available.
|
|
302
|
+
*
|
|
303
|
+
* If, at registration time, there's already an active or waiting service
|
|
304
|
+
* worker with a matching script URL, it will be used (with the waiting
|
|
305
|
+
* service worker taking precedence over the active service worker if both
|
|
306
|
+
* match, since the waiting service worker would have been registered more
|
|
307
|
+
* recently).
|
|
308
|
+
* If there's no matching active or waiting service worker at registration
|
|
309
|
+
* time then the promise will not resolve until an update is found and starts
|
|
310
|
+
* installing, at which point the installing service worker is used.
|
|
311
|
+
*
|
|
312
|
+
* @return {Promise<ServiceWorker>}
|
|
313
|
+
*/
|
|
314
|
+
getSW(): Promise<ServiceWorker>;
|
|
315
|
+
/**
|
|
316
|
+
* Sends the passed data object to the service worker registered by this
|
|
317
|
+
* instance (via {@link workbox-window.Workbox#getSW}) and resolves
|
|
318
|
+
* with a response (if any).
|
|
319
|
+
*
|
|
320
|
+
* A response can be set in a message handler in the service worker by
|
|
321
|
+
* calling `event.ports[0].postMessage(...)`, which will resolve the promise
|
|
322
|
+
* returned by `messageSW()`. If no response is set, the promise will never
|
|
323
|
+
* resolve.
|
|
324
|
+
*
|
|
325
|
+
* @param {object} data An object to send to the service worker
|
|
326
|
+
* @return {Promise<object>}
|
|
327
|
+
*/
|
|
328
|
+
messageSW(data: object): Promise<any>;
|
|
329
|
+
/**
|
|
330
|
+
* Sends a `{type: 'SKIP_WAITING'}` message to the service worker that's
|
|
331
|
+
* currently in the `waiting` state associated with the current registration.
|
|
332
|
+
*
|
|
333
|
+
* If there is no current registration or no service worker is `waiting`,
|
|
334
|
+
* calling this will have no effect.
|
|
335
|
+
*/
|
|
336
|
+
messageSkipWaiting(): void;
|
|
337
|
+
/**
|
|
338
|
+
* Checks for a service worker already controlling the page and returns
|
|
339
|
+
* it if its script URL matches.
|
|
340
|
+
*
|
|
341
|
+
* @private
|
|
342
|
+
* @return {ServiceWorker|undefined}
|
|
343
|
+
*/
|
|
344
|
+
private _getControllingSWIfCompatible;
|
|
345
|
+
/**
|
|
346
|
+
* Registers a service worker for this instances script URL and register
|
|
347
|
+
* options and tracks the time registration was complete.
|
|
348
|
+
*
|
|
349
|
+
* @private
|
|
350
|
+
*/
|
|
351
|
+
private _registerScript;
|
|
352
|
+
/**
|
|
353
|
+
* @private
|
|
354
|
+
*/
|
|
355
|
+
private readonly _onUpdateFound;
|
|
356
|
+
/**
|
|
357
|
+
* @private
|
|
358
|
+
* @param {Event} originalEvent
|
|
359
|
+
*/
|
|
360
|
+
private readonly _onStateChange;
|
|
361
|
+
/**
|
|
362
|
+
* @private
|
|
363
|
+
* @param {Event} originalEvent
|
|
364
|
+
*/
|
|
365
|
+
private readonly _onControllerChange;
|
|
366
|
+
/**
|
|
367
|
+
* @private
|
|
368
|
+
* @param {Event} originalEvent
|
|
369
|
+
*/
|
|
370
|
+
private readonly _onMessage;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
export { Workbox, WorkboxEvent, type WorkboxEventMap, type WorkboxLifecycleEvent, type WorkboxLifecycleEventMap, type WorkboxLifecycleWaitingEvent, type WorkboxMessageEvent, messageSW };
|