@serwist/window 8.4.3 → 9.0.0-preview.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/dist/index.cjs DELETED
@@ -1,710 +0,0 @@
1
- 'use strict';
2
-
3
- var internal = require('@serwist/core/internal');
4
-
5
- /*
6
- Copyright 2019 Google LLC
7
-
8
- Use of this source code is governed by an MIT-style
9
- license that can be found in the LICENSE file or at
10
- https://opensource.org/licenses/MIT.
11
- */ /**
12
- * Sends a data object to a service worker via `postMessage` and resolves with
13
- * a response (if any).
14
- *
15
- * A response can be set in a message handler in the service worker by
16
- * calling `event.ports[0].postMessage(...)`, which will resolve the promise
17
- * returned by `messageSW()`. If no response is set, the promise will not
18
- * resolve.
19
- *
20
- * @param sw The service worker to send the message to.
21
- * @param data An object to send to the service worker.
22
- * @returns
23
- */ // biome-ignore lint/complexity/noBannedTypes: Better not change type of data.
24
- function messageSW(sw, data) {
25
- return new Promise((resolve)=>{
26
- const messageChannel = new MessageChannel();
27
- messageChannel.port1.onmessage = (event)=>{
28
- resolve(event.data);
29
- };
30
- sw.postMessage(data, [
31
- messageChannel.port2
32
- ]);
33
- });
34
- }
35
-
36
- /*
37
- Copyright 2019 Google LLC
38
-
39
- Use of this source code is governed by an MIT-style
40
- license that can be found in the LICENSE file or at
41
- https://opensource.org/licenses/MIT.
42
- */ /**
43
- * A minimal `Event` subclass shim.
44
- * This doesn't *actually* subclass `Event` because not all browsers support
45
- * constructable `EventTarget`, and using a real `Event` will error.
46
- * @private
47
- */ class SerwistEvent {
48
- type;
49
- target;
50
- sw;
51
- originalEvent;
52
- isExternal;
53
- constructor(type, props){
54
- this.type = type;
55
- Object.assign(this, props);
56
- }
57
- }
58
-
59
- /*
60
- Copyright 2019 Google LLC
61
-
62
- Use of this source code is governed by an MIT-style
63
- license that can be found in the LICENSE file or at
64
- https://opensource.org/licenses/MIT.
65
- */ /**
66
- * A minimal `EventTarget` shim.
67
- * This is necessary because not all browsers support constructable
68
- * `EventTarget`, so using a real `EventTarget` will error.
69
- * @private
70
- */ class SerwistEventTarget {
71
- _eventListenerRegistry = new Map();
72
- /**
73
- * @param type
74
- * @param listener
75
- * @private
76
- */ addEventListener(type, listener) {
77
- const foo = this._getEventListenersByType(type);
78
- foo.add(listener);
79
- }
80
- /**
81
- * @param type
82
- * @param listener
83
- * @private
84
- */ removeEventListener(type, listener) {
85
- this._getEventListenersByType(type).delete(listener);
86
- }
87
- /**
88
- * @param event
89
- * @private
90
- */ dispatchEvent(event) {
91
- event.target = this;
92
- const listeners = this._getEventListenersByType(event.type);
93
- for (const listener of listeners){
94
- listener(event);
95
- }
96
- }
97
- /**
98
- * Returns a Set of listeners associated with the passed event type.
99
- * If no handlers have been registered, an empty Set is returned.
100
- *
101
- * @param type The event type.
102
- * @returns An array of handler functions.
103
- * @private
104
- */ _getEventListenersByType(type) {
105
- if (!this._eventListenerRegistry.has(type)) {
106
- this._eventListenerRegistry.set(type, new Set());
107
- }
108
- return this._eventListenerRegistry.get(type);
109
- }
110
- }
111
-
112
- /*
113
- Copyright 2019 Google LLC
114
-
115
- Use of this source code is governed by an MIT-style
116
- license that can be found in the LICENSE file or at
117
- https://opensource.org/licenses/MIT.
118
- */ /**
119
- * Returns true if two URLs have the same `.href` property. The URLS can be
120
- * relative, and if they are the current location href is used to resolve URLs.
121
- *
122
- * @private
123
- * @param url1
124
- * @param url2
125
- * @returns
126
- */ function urlsMatch(url1, url2) {
127
- const { href } = location;
128
- return new URL(url1, href).href === new URL(url2, href).href;
129
- }
130
-
131
- // The time a SW must be in the waiting phase before we can conclude
132
- // `skipWaiting()` wasn't called. This 200 amount wasn't scientifically
133
- // chosen, but it seems to avoid false positives in my testing.
134
- const WAITING_TIMEOUT_DURATION = 200;
135
- // The amount of time after a registration that we can reasonably conclude
136
- // that the registration didn't trigger an update.
137
- const REGISTRATION_TIMEOUT_DURATION = 60000;
138
- // The de facto standard message that a service worker should be listening for
139
- // to trigger a call to skipWaiting().
140
- const SKIP_WAITING_MESSAGE = {
141
- type: "SKIP_WAITING"
142
- };
143
- /**
144
- * A class to aid in handling service worker registration, updates, and
145
- * reacting to service worker lifecycle events.
146
- *
147
- * @fires `@serwist/window.Serwist.message`
148
- * @fires `@serwist/window.Serwist.installed`
149
- * @fires `@serwist/window.Serwist.waiting`
150
- * @fires `@serwist/window.Serwist.controlling`
151
- * @fires `@serwist/window.Serwist.activated`
152
- * @fires `@serwist/window.Serwist.redundant`
153
- */ class Serwist extends SerwistEventTarget {
154
- _scriptURL;
155
- _registerOptions = {};
156
- _updateFoundCount = 0;
157
- // Deferreds we can resolve later.
158
- _swDeferred = new internal.Deferred();
159
- _activeDeferred = new internal.Deferred();
160
- _controllingDeferred = new internal.Deferred();
161
- _registrationTime = 0;
162
- _isUpdate;
163
- _compatibleControllingSW;
164
- _registration;
165
- _sw;
166
- _ownSWs = new Set();
167
- _externalSW;
168
- _waitingTimeout;
169
- /**
170
- * Creates a new Serwist instance with a script URL and service worker
171
- * options. The script URL and options are the same as those used when
172
- * calling [navigator.serviceWorker.register(scriptURL, options)](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register).
173
- *
174
- * @param scriptURL The service worker script
175
- * associated with this instance. Using a
176
- * [`TrustedScriptURL`](https://web.dev/trusted-types/) is supported.
177
- * @param registerOptions The service worker options associated
178
- * with this instance.
179
- */ // biome-ignore lint/complexity/noBannedTypes: Unknown reason
180
- constructor(scriptURL, registerOptions = {}){
181
- super();
182
- this._scriptURL = scriptURL;
183
- this._registerOptions = registerOptions;
184
- // Add a message listener immediately since messages received during
185
- // page load are buffered only until the DOMContentLoaded event:
186
- // https://github.com/GoogleChrome/workbox/issues/2202
187
- navigator.serviceWorker.addEventListener("message", this._onMessage);
188
- }
189
- /**
190
- * Registers a service worker for this instances script URL and service
191
- * worker options. By default this method delays registration until after
192
- * the window has loaded.
193
- *
194
- * @param options
195
- */ async register({ immediate = false } = {}) {
196
- if (process.env.NODE_ENV !== "production") {
197
- if (this._registrationTime) {
198
- internal.logger.error("Cannot re-register a Serwist instance after it has " + "been registered. Create a new instance instead.");
199
- return;
200
- }
201
- }
202
- if (!immediate && document.readyState !== "complete") {
203
- await new Promise((res)=>window.addEventListener("load", res));
204
- }
205
- // Set this flag to true if any service worker was controlling the page
206
- // at registration time.
207
- this._isUpdate = Boolean(navigator.serviceWorker.controller);
208
- // Before registering, attempt to determine if a SW is already controlling
209
- // the page, and if that SW script (and version, if specified) matches this
210
- // instance's script.
211
- this._compatibleControllingSW = this._getControllingSWIfCompatible();
212
- this._registration = await this._registerScript();
213
- // If we have a compatible controller, store the controller as the "own"
214
- // SW, resolve active/controlling deferreds and add necessary listeners.
215
- if (this._compatibleControllingSW) {
216
- this._sw = this._compatibleControllingSW;
217
- this._activeDeferred.resolve(this._compatibleControllingSW);
218
- this._controllingDeferred.resolve(this._compatibleControllingSW);
219
- this._compatibleControllingSW.addEventListener("statechange", this._onStateChange, {
220
- once: true
221
- });
222
- }
223
- // If there's a waiting service worker with a matching URL before the
224
- // `updatefound` event fires, it likely means that this site is open
225
- // in another tab, or the user refreshed the page (and thus the previous
226
- // page wasn't fully unloaded before this page started loading).
227
- // https://developers.google.com/web/fundamentals/primers/service-workers/lifecycle#waiting
228
- const waitingSW = this._registration.waiting;
229
- if (waitingSW && urlsMatch(waitingSW.scriptURL, this._scriptURL.toString())) {
230
- // Store the waiting SW as the "own" Sw, even if it means overwriting
231
- // a compatible controller.
232
- this._sw = waitingSW;
233
- // Run this in the next microtask, so any code that adds an event
234
- // listener after awaiting `register()` will get this event.
235
- internal.dontWaitFor(Promise.resolve().then(()=>{
236
- this.dispatchEvent(new SerwistEvent("waiting", {
237
- sw: waitingSW,
238
- wasWaitingBeforeRegister: true
239
- }));
240
- if (process.env.NODE_ENV !== "production") {
241
- internal.logger.warn("A service worker was already waiting to activate " + "before this script was registered...");
242
- }
243
- }));
244
- }
245
- // If an "own" SW is already set, resolve the deferred.
246
- if (this._sw) {
247
- this._swDeferred.resolve(this._sw);
248
- this._ownSWs.add(this._sw);
249
- }
250
- if (process.env.NODE_ENV !== "production") {
251
- internal.logger.log("Successfully registered service worker.", this._scriptURL.toString());
252
- if (navigator.serviceWorker.controller) {
253
- if (this._compatibleControllingSW) {
254
- internal.logger.debug("A service worker with the same script URL " + "is already controlling this page.");
255
- } else {
256
- internal.logger.debug("A service worker with a different script URL is " + "currently controlling the page. The browser is now fetching " + "the new script now...");
257
- }
258
- }
259
- const currentPageIsOutOfScope = ()=>{
260
- const scopeURL = new URL(this._registerOptions.scope || this._scriptURL.toString(), document.baseURI);
261
- const scopeURLBasePath = new URL("./", scopeURL.href).pathname;
262
- return !location.pathname.startsWith(scopeURLBasePath);
263
- };
264
- if (currentPageIsOutOfScope()) {
265
- internal.logger.warn("The current page is not in scope for the registered " + "service worker. Was this a mistake?");
266
- }
267
- }
268
- this._registration.addEventListener("updatefound", this._onUpdateFound);
269
- navigator.serviceWorker.addEventListener("controllerchange", this._onControllerChange);
270
- return this._registration;
271
- }
272
- /**
273
- * Checks for updates of the registered service worker.
274
- */ async update() {
275
- if (!this._registration) {
276
- if (process.env.NODE_ENV !== "production") {
277
- internal.logger.error("Cannot update a Serwist instance without " + "being registered. Register the Serwist instance first.");
278
- }
279
- return;
280
- }
281
- // Try to update registration
282
- await this._registration.update();
283
- }
284
- /**
285
- * Resolves to the service worker registered by this instance as soon as it
286
- * is active. If a service worker was already controlling at registration
287
- * time then it will resolve to that if the script URLs (and optionally
288
- * script versions) match, otherwise it will wait until an update is found
289
- * and activates.
290
- *
291
- * @returns
292
- */ get active() {
293
- return this._activeDeferred.promise;
294
- }
295
- /**
296
- * Resolves to the service worker registered by this instance as soon as it
297
- * is controlling the page. If a service worker was already controlling at
298
- * registration time then it will resolve to that if the script URLs (and
299
- * optionally script versions) match, otherwise it will wait until an update
300
- * is found and starts controlling the page.
301
- * Note: the first time a service worker is installed it will active but
302
- * not start controlling the page unless `clients.claim()` is called in the
303
- * service worker.
304
- *
305
- * @returns
306
- */ get controlling() {
307
- return this._controllingDeferred.promise;
308
- }
309
- /**
310
- * Resolves with a reference to a service worker that matches the script URL
311
- * of this instance, as soon as it's available.
312
- *
313
- * If, at registration time, there's already an active or waiting service
314
- * worker with a matching script URL, it will be used (with the waiting
315
- * service worker taking precedence over the active service worker if both
316
- * match, since the waiting service worker would have been registered more
317
- * recently).
318
- * If there's no matching active or waiting service worker at registration
319
- * time then the promise will not resolve until an update is found and starts
320
- * installing, at which point the installing service worker is used.
321
- *
322
- * @returns
323
- */ getSW() {
324
- // If `this._sw` is set, resolve with that as we want `getSW()` to
325
- // return the correct (new) service worker if an update is found.
326
- return this._sw !== undefined ? Promise.resolve(this._sw) : this._swDeferred.promise;
327
- }
328
- /**
329
- * Sends the passed data object to the service worker registered by this
330
- * instance (via `@serwist/window.Serwist.getSW`) and resolves
331
- * with a response (if any).
332
- *
333
- * A response can be set in a message handler in the service worker by
334
- * calling `event.ports[0].postMessage(...)`, which will resolve the promise
335
- * returned by `messageSW()`. If no response is set, the promise will never
336
- * resolve.
337
- *
338
- * @param data An object to send to the service worker
339
- * @returns
340
- */ // We might be able to change the 'data' type to Record<string, unknown> in the future.
341
- // eslint-disable-next-line @typescript-eslint/ban-types
342
- async messageSW(data) {
343
- const sw = await this.getSW();
344
- return messageSW(sw, data);
345
- }
346
- /**
347
- * Sends a `{type: 'SKIP_WAITING'}` message to the service worker that's
348
- * currently in the `waiting` state associated with the current registration.
349
- *
350
- * If there is no current registration or no service worker is `waiting`,
351
- * calling this will have no effect.
352
- */ messageSkipWaiting() {
353
- if (this._registration?.waiting) {
354
- void messageSW(this._registration.waiting, SKIP_WAITING_MESSAGE);
355
- }
356
- }
357
- /**
358
- * Checks for a service worker already controlling the page and returns
359
- * it if its script URL matches.
360
- *
361
- * @private
362
- * @returns
363
- */ _getControllingSWIfCompatible() {
364
- const controller = navigator.serviceWorker.controller;
365
- if (controller && urlsMatch(controller.scriptURL, this._scriptURL.toString())) {
366
- return controller;
367
- }
368
- return undefined;
369
- }
370
- /**
371
- * Registers a service worker for this instances script URL and register
372
- * options and tracks the time registration was complete.
373
- *
374
- * @private
375
- */ async _registerScript() {
376
- try {
377
- // this._scriptURL may be a TrustedScriptURL, but there's no support for
378
- // passing that to register() in lib.dom right now.
379
- // https://github.com/GoogleChrome/workbox/issues/2855
380
- const reg = await navigator.serviceWorker.register(this._scriptURL, this._registerOptions);
381
- // Keep track of when registration happened, so it can be used in the
382
- // `this._onUpdateFound` heuristic. Also use the presence of this
383
- // property as a way to see if `.register()` has been called.
384
- this._registrationTime = performance.now();
385
- return reg;
386
- } catch (error) {
387
- if (process.env.NODE_ENV !== "production") {
388
- internal.logger.error(error);
389
- }
390
- // Re-throw the error.
391
- throw error;
392
- }
393
- }
394
- /**
395
- * @private
396
- * @param originalEvent
397
- */ _onUpdateFound = (originalEvent)=>{
398
- // `this._registration` will never be `undefined` after an update is found.
399
- const registration = this._registration;
400
- const installingSW = registration.installing;
401
- // If the script URL passed to `navigator.serviceWorker.register()` is
402
- // different from the current controlling SW's script URL, we know any
403
- // successful registration calls will trigger an `updatefound` event.
404
- // But if the registered script URL is the same as the current controlling
405
- // SW's script URL, we'll only get an `updatefound` event if the file
406
- // changed since it was last registered. This can be a problem if the user
407
- // opens up the same page in a different tab, and that page registers
408
- // a SW that triggers an update. It's a problem because this page has no
409
- // good way of knowing whether the `updatefound` event came from the SW
410
- // script it registered or from a registration attempt made by a newer
411
- // version of the page running in another tab.
412
- // To minimize the possibility of a false positive, we use the logic here:
413
- const updateLikelyTriggeredExternally = // Since we enforce only calling `register()` once, and since we don't
414
- // add the `updatefound` event listener until the `register()` call, if
415
- // `_updateFoundCount` is > 0 then it means this method has already
416
- // been called, thus this SW must be external
417
- this._updateFoundCount > 0 || // If the script URL of the installing SW is different from this
418
- // instance's script URL, we know it's definitely not from our
419
- // registration.
420
- !urlsMatch(installingSW.scriptURL, this._scriptURL.toString()) || // If all of the above are false, then we use a time-based heuristic:
421
- // Any `updatefound` event that occurs long after our registration is
422
- // assumed to be external.
423
- performance.now() > this._registrationTime + REGISTRATION_TIMEOUT_DURATION ? // triggered by this instance.
424
- true : false;
425
- if (updateLikelyTriggeredExternally) {
426
- this._externalSW = installingSW;
427
- registration.removeEventListener("updatefound", this._onUpdateFound);
428
- } else {
429
- // If the update was not triggered externally we know the installing
430
- // SW is the one we registered, so we set it.
431
- this._sw = installingSW;
432
- this._ownSWs.add(installingSW);
433
- this._swDeferred.resolve(installingSW);
434
- if (process.env.NODE_ENV !== "production") {
435
- if (this._isUpdate) {
436
- internal.logger.log("Updated service worker found. Installing now...");
437
- } else {
438
- internal.logger.log("Service worker is installing...");
439
- }
440
- }
441
- }
442
- // Dispatch the `installing` event when the SW is installing.
443
- this.dispatchEvent(new SerwistEvent("installing", {
444
- sw: installingSW,
445
- originalEvent,
446
- isExternal: updateLikelyTriggeredExternally,
447
- isUpdate: this._isUpdate
448
- }));
449
- // Increment the `updatefound` count, so future invocations of this
450
- // method can be sure they were triggered externally.
451
- ++this._updateFoundCount;
452
- // Add a `statechange` listener regardless of whether this update was
453
- // triggered externally, since we have callbacks for both.
454
- installingSW.addEventListener("statechange", this._onStateChange);
455
- };
456
- /**
457
- * @private
458
- * @param originalEvent
459
- */ _onStateChange = (originalEvent)=>{
460
- // `this._registration` will never be `undefined` after an update is found.
461
- const registration = this._registration;
462
- const sw = originalEvent.target;
463
- const { state } = sw;
464
- const isExternal = sw === this._externalSW;
465
- const eventProps = {
466
- sw,
467
- isExternal,
468
- originalEvent
469
- };
470
- if (!isExternal && this._isUpdate) {
471
- eventProps.isUpdate = true;
472
- }
473
- this.dispatchEvent(new SerwistEvent(state, eventProps));
474
- if (state === "installed") {
475
- // This timeout is used to ignore cases where the service worker calls
476
- // `skipWaiting()` in the install event, thus moving it directly in the
477
- // activating state. (Since all service workers *must* go through the
478
- // waiting phase, the only way to detect `skipWaiting()` called in the
479
- // install event is to observe that the time spent in the waiting phase
480
- // is very short.)
481
- // NOTE: we don't need separate timeouts for the own and external SWs
482
- // since they can't go through these phases at the same time.
483
- this._waitingTimeout = self.setTimeout(()=>{
484
- // Ensure the SW is still waiting (it may now be redundant).
485
- if (state === "installed" && registration.waiting === sw) {
486
- this.dispatchEvent(new SerwistEvent("waiting", eventProps));
487
- if (process.env.NODE_ENV !== "production") {
488
- if (isExternal) {
489
- internal.logger.warn("An external service worker has installed but is " + "waiting for this client to close before activating...");
490
- } else {
491
- internal.logger.warn("The service worker has installed but is waiting " + "for existing clients to close before activating...");
492
- }
493
- }
494
- }
495
- }, WAITING_TIMEOUT_DURATION);
496
- } else if (state === "activating") {
497
- clearTimeout(this._waitingTimeout);
498
- if (!isExternal) {
499
- this._activeDeferred.resolve(sw);
500
- }
501
- }
502
- if (process.env.NODE_ENV !== "production") {
503
- switch(state){
504
- case "installed":
505
- if (isExternal) {
506
- internal.logger.warn("An external service worker has installed. " + "You may want to suggest users reload this page.");
507
- } else {
508
- internal.logger.log("Registered service worker installed.");
509
- }
510
- break;
511
- case "activated":
512
- if (isExternal) {
513
- internal.logger.warn("An external service worker has activated.");
514
- } else {
515
- internal.logger.log("Registered service worker activated.");
516
- if (sw !== navigator.serviceWorker.controller) {
517
- internal.logger.warn("The registered service worker is active but " + "not yet controlling the page. Reload or run " + "`clients.claim()` in the service worker.");
518
- }
519
- }
520
- break;
521
- case "redundant":
522
- if (sw === this._compatibleControllingSW) {
523
- internal.logger.log("Previously controlling service worker now redundant!");
524
- } else if (!isExternal) {
525
- internal.logger.log("Registered service worker now redundant!");
526
- }
527
- break;
528
- }
529
- }
530
- };
531
- /**
532
- * @private
533
- * @param originalEvent
534
- */ _onControllerChange = (originalEvent)=>{
535
- const sw = this._sw;
536
- const isExternal = sw !== navigator.serviceWorker.controller;
537
- // Unconditionally dispatch the controlling event, with isExternal set
538
- // to distinguish between controller changes due to the initial registration
539
- // vs. an update-check or other tab's registration.
540
- // See https://github.com/GoogleChrome/workbox/issues/2786
541
- this.dispatchEvent(new SerwistEvent("controlling", {
542
- isExternal,
543
- originalEvent,
544
- sw,
545
- isUpdate: this._isUpdate
546
- }));
547
- if (!isExternal) {
548
- if (process.env.NODE_ENV !== "production") {
549
- internal.logger.log("Registered service worker now controlling this page.");
550
- }
551
- this._controllingDeferred.resolve(sw);
552
- }
553
- };
554
- /**
555
- * @private
556
- * @param originalEvent
557
- */ _onMessage = async (originalEvent)=>{
558
- // Can't change type 'any' of data.
559
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
560
- const { data, ports, source } = originalEvent;
561
- // Wait until there's an "own" service worker. This is used to buffer
562
- // `message` events that may be received prior to calling `register()`.
563
- await this.getSW();
564
- // If the service worker that sent the message is in the list of own
565
- // service workers for this instance, dispatch a `message` event.
566
- // NOTE: we check for all previously owned service workers rather than
567
- // just the current one because some messages (e.g. cache updates) use
568
- // a timeout when sent and may be delayed long enough for a service worker
569
- // update to be found.
570
- if (this._ownSWs.has(source)) {
571
- this.dispatchEvent(new SerwistEvent("message", {
572
- // Can't change type 'any' of data.
573
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
574
- data,
575
- originalEvent,
576
- ports,
577
- sw: source
578
- }));
579
- }
580
- };
581
- } // The jsdoc comments below outline the events this instance may dispatch:
582
- // -----------------------------------------------------------------------
583
- /**
584
- * The `message` event is dispatched any time a `postMessage` is received.
585
- *
586
- * @event workbox-window.Workbox#message
587
- * @type {SerwistEvent}
588
- * @property {*} data The `data` property from the original `message` event.
589
- * @property {Event} originalEvent The original [`message`]{@link https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent}
590
- * event.
591
- * @property {string} type `message`.
592
- * @property {MessagePort[]} ports The `ports` value from `originalEvent`.
593
- * @property {Workbox} target The `Workbox` instance.
594
- */ /**
595
- * The `installed` event is dispatched if the state of a
596
- * {@link workbox-window.Workbox} instance's
597
- * {@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw|registered service worker}
598
- * changes to `installed`.
599
- *
600
- * Then can happen either the very first time a service worker is installed,
601
- * or after an update to the current service worker is found. In the case
602
- * of an update being found, the event's `isUpdate` property will be `true`.
603
- *
604
- * @event workbox-window.Workbox#installed
605
- * @type {SerwistEvent}
606
- * @property {ServiceWorker} sw The service worker instance.
607
- * @property {Event} originalEvent The original [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
608
- * event.
609
- * @property {boolean|undefined} isUpdate True if a service worker was already
610
- * controlling when this `Workbox` instance called `register()`.
611
- * @property {boolean|undefined} isExternal True if this event is associated
612
- * 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}.
613
- * @property {string} type `installed`.
614
- * @property {Workbox} target The `Workbox` instance.
615
- */ /**
616
- * The `waiting` event is dispatched if the state of a
617
- * {@link workbox-window.Workbox} instance's
618
- * [registered service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw}
619
- * changes to `installed` and then doesn't immediately change to `activating`.
620
- * It may also be dispatched if a service worker with the same
621
- * [`scriptURL`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/scriptURL}
622
- * was already waiting when the {@link workbox-window.Workbox#register}
623
- * method was called.
624
- *
625
- * @event workbox-window.Workbox#waiting
626
- * @type {SerwistEvent}
627
- * @property {ServiceWorker} sw The service worker instance.
628
- * @property {Event|undefined} originalEvent The original
629
- * [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
630
- * event, or `undefined` in the case where the service worker was waiting
631
- * to before `.register()` was called.
632
- * @property {boolean|undefined} isUpdate True if a service worker was already
633
- * controlling when this `Workbox` instance called `register()`.
634
- * @property {boolean|undefined} isExternal True if this event is associated
635
- * 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}.
636
- * @property {boolean|undefined} wasWaitingBeforeRegister True if a service worker with
637
- * a matching `scriptURL` was already waiting when this `Workbox`
638
- * instance called `register()`.
639
- * @property {string} type `waiting`.
640
- * @property {Workbox} target The `Workbox` instance.
641
- */ /**
642
- * The `controlling` event is dispatched if a
643
- * [`controllerchange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/oncontrollerchange}
644
- * fires on the service worker [container]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer}
645
- * and the [`scriptURL`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/scriptURL}
646
- * of the new [controller]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/controller}
647
- * matches the `scriptURL` of the `Workbox` instance's
648
- * [registered service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw}.
649
- *
650
- * @event workbox-window.Workbox#controlling
651
- * @type {SerwistEvent}
652
- * @property {ServiceWorker} sw The service worker instance.
653
- * @property {Event} originalEvent The original [`controllerchange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/oncontrollerchange}
654
- * event.
655
- * @property {boolean|undefined} isUpdate True if a service worker was already
656
- * controlling when this service worker was registered.
657
- * @property {boolean|undefined} isExternal True if this event is associated
658
- * 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}.
659
- * @property {string} type `controlling`.
660
- * @property {Workbox} target The `Workbox` instance.
661
- */ /**
662
- * The `activated` event is dispatched if the state of a
663
- * {@link workbox-window.Workbox} instance's
664
- * {@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw|registered service worker}
665
- * changes to `activated`.
666
- *
667
- * @event workbox-window.Workbox#activated
668
- * @type {SerwistEvent}
669
- * @property {ServiceWorker} sw The service worker instance.
670
- * @property {Event} originalEvent The original [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
671
- * event.
672
- * @property {boolean|undefined} isUpdate True if a service worker was already
673
- * controlling when this `Workbox` instance called `register()`.
674
- * @property {boolean|undefined} isExternal True if this event is associated
675
- * 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}.
676
- * @property {string} type `activated`.
677
- * @property {Workbox} target The `Workbox` instance.
678
- */ /**
679
- * The `redundant` event is dispatched if the state of a
680
- * {@link workbox-window.Workbox} instance's
681
- * [registered service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw}
682
- * changes to `redundant`.
683
- *
684
- * @event workbox-window.Workbox#redundant
685
- * @type {SerwistEvent}
686
- * @property {ServiceWorker} sw The service worker instance.
687
- * @property {Event} originalEvent The original [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
688
- * event.
689
- * @property {boolean|undefined} isUpdate True if a service worker was already
690
- * controlling when this `Workbox` instance called `register()`.
691
- * @property {string} type `redundant`.
692
- * @property {Workbox} target The `Workbox` instance.
693
- */ /**
694
- * The `installing` event is dispatched if the service worker
695
- * finds the new version and starts installing.
696
- *
697
- * @event workbox-window.Workbox#installing
698
- * @type {SerwistEvent}
699
- * @property {ServiceWorker} sw The installing service worker instance.
700
- * @property {Event} originalEvent The original [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
701
- * event.
702
- * @property {boolean|undefined} isUpdate True if a service worker was already
703
- * controlling when this `Workbox` instance called `register()`.
704
- * @property {string} type `installing`.
705
- * @property {Workbox} target The `Workbox` instance.
706
- */
707
-
708
- exports.Serwist = Serwist;
709
- exports.SerwistEvent = SerwistEvent;
710
- exports.messageSW = messageSW;