@serwist/broadcast-update 9.0.0-preview.1 → 9.0.0-preview.11

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/README.md CHANGED
@@ -1 +1 @@
1
- This module's documentation can be found at https://developers.google.com/web/tools/workbox/modules/workbox-broadcast-update
1
+ This module's documentation can be found at https://serwist.pages.dev/docs/broadcast-update
@@ -1,36 +1,13 @@
1
1
  import type { CacheDidUpdateCallbackParam } from "@serwist/core";
2
- export interface BroadcastCacheUpdateOptions {
3
- /**
4
- * A list of headers that will be used to determine whether the responses
5
- * differ.
6
- *
7
- * @default ['content-length', 'etag', 'last-modified']
8
- */
9
- headersToCheck?: string[];
10
- /**
11
- * A function whose return value
12
- * will be used as the `payload` field in any cache update messages sent
13
- * to the window clients.
14
- * @param options
15
- * @returns
16
- */
17
- generatePayload?: (options: CacheDidUpdateCallbackParam) => Record<string, any>;
18
- /**
19
- * If true (the default) then all open clients will receive a message. If false,
20
- * then only the client that make the original request will be notified of the update.
21
- *
22
- * @default true
23
- */
24
- notifyAllClients?: boolean;
25
- }
2
+ import type { BroadcastCacheUpdateOptions } from "./types.js";
26
3
  /**
27
- * Uses the `postMessage()` API to inform any open windows/tabs when a cached
28
- * response has been updated.
4
+ * A class that uses the `postMessage()` API to inform any open windows/tabs when
5
+ * a cached response has been updated.
29
6
  *
30
7
  * For efficiency's sake, the underlying response bodies are not compared;
31
8
  * only specific response headers are checked.
32
9
  */
33
- declare class BroadcastCacheUpdate {
10
+ export declare class BroadcastCacheUpdate {
34
11
  private readonly _headersToCheck;
35
12
  private readonly _generatePayload;
36
13
  private readonly _notifyAllClients;
@@ -42,10 +19,8 @@ declare class BroadcastCacheUpdate {
42
19
  */
43
20
  constructor({ generatePayload, headersToCheck, notifyAllClients }?: BroadcastCacheUpdateOptions);
44
21
  /**
45
- * Compares two [Responses](https://developer.mozilla.org/en-US/docs/Web/API/Response)
46
- * and sends a message (via `postMessage()`) to all window clients if the
47
- * responses differ. Neither of the Responses can be
48
- * [opaque](https://developer.chrome.com/docs/workbox/caching-resources-during-runtime/#opaque-responses).
22
+ * Compares two responses and sends a message (via `postMessage()`) to all window clients if the
23
+ * responses differ. Neither of the Responses can be opaque.
49
24
  *
50
25
  * The message that's posted has the following format (where `payload` can
51
26
  * be customized via the `generatePayload` option the instance is created
@@ -67,5 +42,4 @@ declare class BroadcastCacheUpdate {
67
42
  */
68
43
  notifyIfUpdated(options: CacheDidUpdateCallbackParam): Promise<void>;
69
44
  }
70
- export { BroadcastCacheUpdate };
71
45
  //# sourceMappingURL=BroadcastCacheUpdate.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"BroadcastCacheUpdate.d.ts","sourceRoot":"","sources":["../src/BroadcastCacheUpdate.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,eAAe,CAAC;AAcjE,MAAM,WAAW,2BAA2B;IAC1C;;;;;OAKG;IACH,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B;;;;;;OAMG;IACH,eAAe,CAAC,EAAE,CAAC,OAAO,EAAE,2BAA2B,KAAK,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAChF;;;;;OAKG;IACH,gBAAgB,CAAC,EAAE,OAAO,CAAC;CAC5B;AAgBD;;;;;;GAMG;AACH,cAAM,oBAAoB;IACxB,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAW;IAC3C,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAgE;IACjG,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAU;IAE5C;;;;;OAKG;gBACS,EAAE,eAAe,EAAE,cAAc,EAAE,gBAAgB,EAAE,GAAE,2BAAgC;IAMnG;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACG,eAAe,CAAC,OAAO,EAAE,2BAA2B,GAAG,OAAO,CAAC,IAAI,CAAC;CA4E3E;AAED,OAAO,EAAE,oBAAoB,EAAE,CAAC"}
1
+ {"version":3,"file":"BroadcastCacheUpdate.d.ts","sourceRoot":"","sources":["../src/BroadcastCacheUpdate.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,eAAe,CAAC;AAKjE,OAAO,KAAK,EAAE,2BAA2B,EAAiE,MAAM,YAAY,CAAC;AAuB7H;;;;;;GAMG;AACH,qBAAa,oBAAoB;IAC/B,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAW;IAC3C,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAA4B;IAC7D,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAU;IAE5C;;;;;OAKG;gBACS,EAAE,eAAe,EAAE,cAAc,EAAE,gBAAgB,EAAE,GAAE,2BAAgC;IAMnG;;;;;;;;;;;;;;;;;;;;;OAqBG;IACG,eAAe,CAAC,OAAO,EAAE,2BAA2B,GAAG,OAAO,CAAC,IAAI,CAAC;CA4E3E"}
@@ -1,10 +1,10 @@
1
- import type { SerwistPlugin } from "@serwist/core";
2
- import type { BroadcastCacheUpdateOptions } from "./BroadcastCacheUpdate.js";
1
+ import type { CacheDidUpdateCallbackParam, SerwistPlugin } from "@serwist/core";
2
+ import type { BroadcastCacheUpdateOptions } from "./types.js";
3
3
  /**
4
- * This plugin will automatically broadcast a message whenever a cached response
5
- * is updated.
4
+ * A class implementing the `cacheDidUpdate` lifecycle callback. It will automatically
5
+ * broadcast a message whenever a cached response is updated.
6
6
  */
7
- declare class BroadcastUpdatePlugin implements SerwistPlugin {
7
+ export declare class BroadcastUpdatePlugin implements SerwistPlugin {
8
8
  private readonly _broadcastUpdate;
9
9
  /**
10
10
  * Construct a `@serwist/broadcast-update.BroadcastCacheUpdate` instance with
@@ -22,7 +22,6 @@ declare class BroadcastUpdatePlugin implements SerwistPlugin {
22
22
  * @private
23
23
  * @param options The input object to this function.
24
24
  */
25
- cacheDidUpdate: SerwistPlugin["cacheDidUpdate"];
25
+ cacheDidUpdate(options: CacheDidUpdateCallbackParam): void;
26
26
  }
27
- export { BroadcastUpdatePlugin };
28
27
  //# sourceMappingURL=BroadcastUpdatePlugin.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"BroadcastUpdatePlugin.d.ts","sourceRoot":"","sources":["../src/BroadcastUpdatePlugin.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAGnD,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,2BAA2B,CAAC;AAG7E;;;GAGG;AACH,cAAM,qBAAsB,YAAW,aAAa;IAClD,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAuB;IAExD;;;;;;OAMG;gBACS,OAAO,CAAC,EAAE,2BAA2B;IAIjD;;;;;;;OAOG;IACH,cAAc,EAAE,aAAa,CAAC,gBAAgB,CAAC,CAE7C;CACH;AAED,OAAO,EAAE,qBAAqB,EAAE,CAAC"}
1
+ {"version":3,"file":"BroadcastUpdatePlugin.d.ts","sourceRoot":"","sources":["../src/BroadcastUpdatePlugin.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,2BAA2B,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAGhF,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,YAAY,CAAC;AAE9D;;;GAGG;AACH,qBAAa,qBAAsB,YAAW,aAAa;IACzD,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAuB;IAExD;;;;;;OAMG;gBACS,OAAO,CAAC,EAAE,2BAA2B;IAIjD;;;;;;;OAOG;IACH,cAAc,CAAC,OAAO,EAAE,2BAA2B;CAGpD"}
@@ -0,0 +1,5 @@
1
+ export declare const CACHE_UPDATED_MESSAGE_TYPE = "CACHE_UPDATED";
2
+ export declare const CACHE_UPDATED_MESSAGE_META = "serwist-broadcast-update";
3
+ export declare const defaultNotifyAllClients = true;
4
+ export declare const defaultHeadersToCheck: string[];
5
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAQA,eAAO,MAAM,0BAA0B,kBAAkB,CAAC;AAC1D,eAAO,MAAM,0BAA0B,6BAA6B,CAAC;AACrE,eAAO,MAAM,uBAAuB,OAAO,CAAC;AAC5C,eAAO,MAAM,qBAAqB,UAA8C,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,10 +1,7 @@
1
- import type { BroadcastCacheUpdateOptions } from "./BroadcastCacheUpdate.js";
2
1
  import { BroadcastCacheUpdate } from "./BroadcastCacheUpdate.js";
3
2
  import { BroadcastUpdatePlugin } from "./BroadcastUpdatePlugin.js";
3
+ import { CACHE_UPDATED_MESSAGE_META, CACHE_UPDATED_MESSAGE_TYPE, defaultHeadersToCheck } from "./constants.js";
4
4
  import { responsesAreSame } from "./responsesAreSame.js";
5
- /**
6
- * @module workbox-broadcast-update
7
- */
8
- export { BroadcastCacheUpdate, BroadcastUpdatePlugin, responsesAreSame };
9
- export type { BroadcastCacheUpdateOptions };
5
+ export { BroadcastCacheUpdate, BroadcastUpdatePlugin, responsesAreSame, CACHE_UPDATED_MESSAGE_META, CACHE_UPDATED_MESSAGE_TYPE, defaultHeadersToCheck, };
6
+ export type * from "./types.js";
10
7
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,2BAA2B,CAAC;AAC7E,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AACjE,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AACnE,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAEzD;;GAEG;AAEH,OAAO,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,CAAC;AAEzE,YAAY,EAAE,2BAA2B,EAAE,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AACjE,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AACnE,OAAO,EAAE,0BAA0B,EAAE,0BAA0B,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAC/G,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAEzD,OAAO,EACL,oBAAoB,EACpB,qBAAqB,EACrB,gBAAgB,EAChB,0BAA0B,EAC1B,0BAA0B,EAC1B,qBAAqB,GACtB,CAAC;AAEF,mBAAmB,YAAY,CAAC"}
package/dist/index.js CHANGED
@@ -1,4 +1,13 @@
1
- import { SerwistError, logger, assert, resultingClientExists, timeout, dontWaitFor } from '@serwist/core/internal';
1
+ import { SerwistError, logger, assert, resultingClientExists, timeout } from '@serwist/core/internal';
2
+
3
+ const CACHE_UPDATED_MESSAGE_TYPE = "CACHE_UPDATED";
4
+ const CACHE_UPDATED_MESSAGE_META = "serwist-broadcast-update";
5
+ const defaultNotifyAllClients = true;
6
+ const defaultHeadersToCheck = [
7
+ "content-length",
8
+ "etag",
9
+ "last-modified"
10
+ ];
2
11
 
3
12
  const responsesAreSame = (firstResponse, secondResponse, headersToCheck)=>{
4
13
  if (process.env.NODE_ENV !== "production") {
@@ -11,7 +20,7 @@ const responsesAreSame = (firstResponse, secondResponse, headersToCheck)=>{
11
20
  });
12
21
  if (!atLeastOneHeaderAvailable) {
13
22
  if (process.env.NODE_ENV !== "production") {
14
- logger.warn("Unable to determine where the response has been updated " + "because none of the headers that would be checked are present.");
23
+ logger.warn("Unable to determine where the response has been updated because none of the headers that would be checked are present.");
15
24
  logger.debug("Attempting to compare the following: ", firstResponse, secondResponse, headersToCheck);
16
25
  }
17
26
  return true;
@@ -23,30 +32,21 @@ const responsesAreSame = (firstResponse, secondResponse, headersToCheck)=>{
23
32
  });
24
33
  };
25
34
 
26
- const CACHE_UPDATED_MESSAGE_TYPE = "CACHE_UPDATED";
27
- const CACHE_UPDATED_MESSAGE_META = "serwist-broadcast-update";
28
- const NOTIFY_ALL_CLIENTS = true;
29
- const DEFAULT_HEADERS_TO_CHECK = [
30
- "content-length",
31
- "etag",
32
- "last-modified"
33
- ];
34
-
35
35
  const isSafari = typeof navigator !== "undefined" && /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
36
- function defaultPayloadGenerator(data) {
36
+ const defaultPayloadGenerator = (data)=>{
37
37
  return {
38
38
  cacheName: data.cacheName,
39
39
  updatedURL: data.request.url
40
40
  };
41
- }
41
+ };
42
42
  class BroadcastCacheUpdate {
43
43
  _headersToCheck;
44
44
  _generatePayload;
45
45
  _notifyAllClients;
46
46
  constructor({ generatePayload, headersToCheck, notifyAllClients } = {}){
47
- this._headersToCheck = headersToCheck || DEFAULT_HEADERS_TO_CHECK;
47
+ this._headersToCheck = headersToCheck || defaultHeadersToCheck;
48
48
  this._generatePayload = generatePayload || defaultPayloadGenerator;
49
- this._notifyAllClients = notifyAllClients ?? NOTIFY_ALL_CLIENTS;
49
+ this._notifyAllClients = notifyAllClients ?? defaultNotifyAllClients;
50
50
  }
51
51
  async notifyIfUpdated(options) {
52
52
  if (process.env.NODE_ENV !== "production") {
@@ -113,9 +113,9 @@ class BroadcastUpdatePlugin {
113
113
  constructor(options){
114
114
  this._broadcastUpdate = new BroadcastCacheUpdate(options);
115
115
  }
116
- cacheDidUpdate = async (options)=>{
117
- dontWaitFor(this._broadcastUpdate.notifyIfUpdated(options));
118
- };
116
+ cacheDidUpdate(options) {
117
+ void this._broadcastUpdate.notifyIfUpdated(options);
118
+ }
119
119
  }
120
120
 
121
- export { BroadcastCacheUpdate, BroadcastUpdatePlugin, responsesAreSame };
121
+ export { BroadcastCacheUpdate, BroadcastUpdatePlugin, CACHE_UPDATED_MESSAGE_META, CACHE_UPDATED_MESSAGE_TYPE, defaultHeadersToCheck, responsesAreSame };
@@ -2,11 +2,10 @@
2
2
  * Given two `Response's`, compares several header values to see if they are
3
3
  * the same or not.
4
4
  *
5
- * @param firstResponse
6
- * @param secondResponse
7
- * @param headersToCheck
5
+ * @param firstResponse The first response.
6
+ * @param secondResponse The second response.
7
+ * @param headersToCheck A list of headers to check.
8
8
  * @returns
9
9
  */
10
- declare const responsesAreSame: (firstResponse: Response, secondResponse: Response, headersToCheck: string[]) => boolean;
11
- export { responsesAreSame };
10
+ export declare const responsesAreSame: (firstResponse: Response, secondResponse: Response, headersToCheck: string[]) => boolean;
12
11
  //# sourceMappingURL=responsesAreSame.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"responsesAreSame.d.ts","sourceRoot":"","sources":["../src/responsesAreSame.ts"],"names":[],"mappings":"AAUA;;;;;;;;GAQG;AACH,QAAA,MAAM,gBAAgB,kBAAmB,QAAQ,kBAAkB,QAAQ,kBAAkB,MAAM,EAAE,KAAG,OA4BvG,CAAC;AAEF,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
1
+ {"version":3,"file":"responsesAreSame.d.ts","sourceRoot":"","sources":["../src/responsesAreSame.ts"],"names":[],"mappings":"AAUA;;;;;;;;GAQG;AACH,eAAO,MAAM,gBAAgB,kBAAmB,QAAQ,kBAAkB,QAAQ,kBAAkB,MAAM,EAAE,KAAG,OA4B9G,CAAC"}
@@ -0,0 +1,36 @@
1
+ import type { CacheDidUpdateCallbackParam } from "@serwist/core";
2
+ import type { CACHE_UPDATED_MESSAGE_META, CACHE_UPDATED_MESSAGE_TYPE } from "./constants.js";
3
+ export interface BroadcastCacheUpdateOptions {
4
+ /**
5
+ * A list of headers that will be used to determine whether the responses
6
+ * differ.
7
+ *
8
+ * @default ['content-length', 'etag', 'last-modified']
9
+ */
10
+ headersToCheck?: string[];
11
+ /**
12
+ * A function whose return value
13
+ * will be used as the `payload` field in any cache update messages sent
14
+ * to the window clients.
15
+ * @param options
16
+ * @returns
17
+ */
18
+ generatePayload?: (options: CacheDidUpdateCallbackParam) => Record<string, any>;
19
+ /**
20
+ * If `true` (the default) then all open clients will receive a message. If `false`,
21
+ * then only the client that make the original request will be notified of the update.
22
+ *
23
+ * @default true
24
+ */
25
+ notifyAllClients?: boolean;
26
+ }
27
+ export type BroadcastPayload = Record<string, any>;
28
+ export interface BroadcastPayloadGenerator {
29
+ (options: CacheDidUpdateCallbackParam): BroadcastPayload;
30
+ }
31
+ export interface BroadcastMessage {
32
+ type: typeof CACHE_UPDATED_MESSAGE_TYPE;
33
+ meta: typeof CACHE_UPDATED_MESSAGE_META;
34
+ payload: BroadcastPayload;
35
+ }
36
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,eAAe,CAAC;AACjE,OAAO,KAAK,EAAE,0BAA0B,EAAE,0BAA0B,EAAE,MAAM,gBAAgB,CAAC;AAE7F,MAAM,WAAW,2BAA2B;IAC1C;;;;;OAKG;IACH,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B;;;;;;OAMG;IACH,eAAe,CAAC,EAAE,CAAC,OAAO,EAAE,2BAA2B,KAAK,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAChF;;;;;OAKG;IACH,gBAAgB,CAAC,EAAE,OAAO,CAAC;CAC5B;AAED,MAAM,MAAM,gBAAgB,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AAEnD,MAAM,WAAW,yBAAyB;IACxC,CAAC,OAAO,EAAE,2BAA2B,GAAG,gBAAgB,CAAC;CAC1D;AAED,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,OAAO,0BAA0B,CAAC;IACxC,IAAI,EAAE,OAAO,0BAA0B,CAAC;IACxC,OAAO,EAAE,gBAAgB,CAAC;CAC3B"}
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@serwist/broadcast-update",
3
- "version": "9.0.0-preview.1",
3
+ "version": "9.0.0-preview.11",
4
4
  "type": "module",
5
- "description": "A service worker helper library that uses the Broadcast Channel API to announce when a cached response has updated",
5
+ "description": "A module that uses the Broadcast Channel API to announce when a cached response has updated",
6
6
  "files": [
7
7
  "src",
8
8
  "dist"
@@ -29,12 +29,12 @@
29
29
  }
30
30
  },
31
31
  "dependencies": {
32
- "@serwist/core": "9.0.0-preview.1"
32
+ "@serwist/core": "9.0.0-preview.11"
33
33
  },
34
34
  "devDependencies": {
35
35
  "rollup": "4.9.6",
36
- "typescript": "5.4.0-dev.20240203",
37
- "@serwist/constants": "9.0.0-preview.1"
36
+ "typescript": "5.4.0-dev.20240206",
37
+ "@serwist/constants": "9.0.0-preview.11"
38
38
  },
39
39
  "peerDependencies": {
40
40
  "typescript": ">=5.0.0"
@@ -9,8 +9,9 @@
9
9
  import type { CacheDidUpdateCallbackParam } from "@serwist/core";
10
10
  import { assert, logger, resultingClientExists, timeout } from "@serwist/core/internal";
11
11
 
12
+ import { CACHE_UPDATED_MESSAGE_META, CACHE_UPDATED_MESSAGE_TYPE, defaultHeadersToCheck, defaultNotifyAllClients } from "./constants.js";
12
13
  import { responsesAreSame } from "./responsesAreSame.js";
13
- import { CACHE_UPDATED_MESSAGE_META, CACHE_UPDATED_MESSAGE_TYPE, DEFAULT_HEADERS_TO_CHECK, NOTIFY_ALL_CLIENTS } from "./utils/constants.js";
14
+ import type { BroadcastCacheUpdateOptions, BroadcastMessage, BroadcastPayload, BroadcastPayloadGenerator } from "./types.js";
14
15
 
15
16
  // UA-sniff Safari: https://stackoverflow.com/questions/7944460/detect-safari-browser
16
17
  // TODO(philipwalton): remove once this Safari bug fix has been released.
@@ -19,32 +20,6 @@ const isSafari = typeof navigator !== "undefined" && /^((?!chrome|android).)*saf
19
20
 
20
21
  // Give TypeScript the correct global.
21
22
  declare let self: ServiceWorkerGlobalScope;
22
-
23
- export interface BroadcastCacheUpdateOptions {
24
- /**
25
- * A list of headers that will be used to determine whether the responses
26
- * differ.
27
- *
28
- * @default ['content-length', 'etag', 'last-modified']
29
- */
30
- headersToCheck?: string[];
31
- /**
32
- * A function whose return value
33
- * will be used as the `payload` field in any cache update messages sent
34
- * to the window clients.
35
- * @param options
36
- * @returns
37
- */
38
- generatePayload?: (options: CacheDidUpdateCallbackParam) => Record<string, any>;
39
- /**
40
- * If true (the default) then all open clients will receive a message. If false,
41
- * then only the client that make the original request will be notified of the update.
42
- *
43
- * @default true
44
- */
45
- notifyAllClients?: boolean;
46
- }
47
-
48
23
  /**
49
24
  * Generates the default payload used in update messages. By default the
50
25
  * payload includes the `cacheName` and `updatedURL` fields.
@@ -52,23 +27,23 @@ export interface BroadcastCacheUpdateOptions {
52
27
  * @returns
53
28
  * @private
54
29
  */
55
- function defaultPayloadGenerator(data: CacheDidUpdateCallbackParam): Record<string, any> {
30
+ const defaultPayloadGenerator = (data: CacheDidUpdateCallbackParam): BroadcastPayload => {
56
31
  return {
57
32
  cacheName: data.cacheName,
58
33
  updatedURL: data.request.url,
59
34
  };
60
- }
35
+ };
61
36
 
62
37
  /**
63
- * Uses the `postMessage()` API to inform any open windows/tabs when a cached
64
- * response has been updated.
38
+ * A class that uses the `postMessage()` API to inform any open windows/tabs when
39
+ * a cached response has been updated.
65
40
  *
66
41
  * For efficiency's sake, the underlying response bodies are not compared;
67
42
  * only specific response headers are checked.
68
43
  */
69
- class BroadcastCacheUpdate {
44
+ export class BroadcastCacheUpdate {
70
45
  private readonly _headersToCheck: string[];
71
- private readonly _generatePayload: (options: CacheDidUpdateCallbackParam) => Record<string, any>;
46
+ private readonly _generatePayload: BroadcastPayloadGenerator;
72
47
  private readonly _notifyAllClients: boolean;
73
48
 
74
49
  /**
@@ -78,16 +53,14 @@ class BroadcastCacheUpdate {
78
53
  * @param options
79
54
  */
80
55
  constructor({ generatePayload, headersToCheck, notifyAllClients }: BroadcastCacheUpdateOptions = {}) {
81
- this._headersToCheck = headersToCheck || DEFAULT_HEADERS_TO_CHECK;
56
+ this._headersToCheck = headersToCheck || defaultHeadersToCheck;
82
57
  this._generatePayload = generatePayload || defaultPayloadGenerator;
83
- this._notifyAllClients = notifyAllClients ?? NOTIFY_ALL_CLIENTS;
58
+ this._notifyAllClients = notifyAllClients ?? defaultNotifyAllClients;
84
59
  }
85
60
 
86
61
  /**
87
- * Compares two [Responses](https://developer.mozilla.org/en-US/docs/Web/API/Response)
88
- * and sends a message (via `postMessage()`) to all window clients if the
89
- * responses differ. Neither of the Responses can be
90
- * [opaque](https://developer.chrome.com/docs/workbox/caching-resources-during-runtime/#opaque-responses).
62
+ * Compares two responses and sends a message (via `postMessage()`) to all window clients if the
63
+ * responses differ. Neither of the Responses can be opaque.
91
64
  *
92
65
  * The message that's posted has the following format (where `payload` can
93
66
  * be customized via the `generatePayload` option the instance is created
@@ -143,7 +116,7 @@ class BroadcastCacheUpdate {
143
116
  type: CACHE_UPDATED_MESSAGE_TYPE,
144
117
  meta: CACHE_UPDATED_MESSAGE_META,
145
118
  payload: this._generatePayload(options),
146
- };
119
+ } satisfies BroadcastMessage;
147
120
 
148
121
  // For navigation requests, wait until the new window client exists
149
122
  // before sending the message
@@ -184,5 +157,3 @@ class BroadcastCacheUpdate {
184
157
  }
185
158
  }
186
159
  }
187
-
188
- export { BroadcastCacheUpdate };
@@ -6,17 +6,16 @@
6
6
  https://opensource.org/licenses/MIT.
7
7
  */
8
8
 
9
- import type { SerwistPlugin } from "@serwist/core";
10
- import { dontWaitFor } from "@serwist/core/internal";
9
+ import type { CacheDidUpdateCallbackParam, SerwistPlugin } from "@serwist/core";
11
10
 
12
- import type { BroadcastCacheUpdateOptions } from "./BroadcastCacheUpdate.js";
13
11
  import { BroadcastCacheUpdate } from "./BroadcastCacheUpdate.js";
12
+ import type { BroadcastCacheUpdateOptions } from "./types.js";
14
13
 
15
14
  /**
16
- * This plugin will automatically broadcast a message whenever a cached response
17
- * is updated.
15
+ * A class implementing the `cacheDidUpdate` lifecycle callback. It will automatically
16
+ * broadcast a message whenever a cached response is updated.
18
17
  */
19
- class BroadcastUpdatePlugin implements SerwistPlugin {
18
+ export class BroadcastUpdatePlugin implements SerwistPlugin {
20
19
  private readonly _broadcastUpdate: BroadcastCacheUpdate;
21
20
 
22
21
  /**
@@ -38,9 +37,7 @@ class BroadcastUpdatePlugin implements SerwistPlugin {
38
37
  * @private
39
38
  * @param options The input object to this function.
40
39
  */
41
- cacheDidUpdate: SerwistPlugin["cacheDidUpdate"] = async (options) => {
42
- dontWaitFor(this._broadcastUpdate.notifyIfUpdated(options));
43
- };
40
+ cacheDidUpdate(options: CacheDidUpdateCallbackParam) {
41
+ void this._broadcastUpdate.notifyIfUpdated(options);
42
+ }
44
43
  }
45
-
46
- export { BroadcastUpdatePlugin };
@@ -8,5 +8,5 @@
8
8
 
9
9
  export const CACHE_UPDATED_MESSAGE_TYPE = "CACHE_UPDATED";
10
10
  export const CACHE_UPDATED_MESSAGE_META = "serwist-broadcast-update";
11
- export const NOTIFY_ALL_CLIENTS = true;
12
- export const DEFAULT_HEADERS_TO_CHECK: string[] = ["content-length", "etag", "last-modified"];
11
+ export const defaultNotifyAllClients = true;
12
+ export const defaultHeadersToCheck = ["content-length", "etag", "last-modified"];
package/src/index.ts CHANGED
@@ -6,15 +6,18 @@
6
6
  https://opensource.org/licenses/MIT.
7
7
  */
8
8
 
9
- import type { BroadcastCacheUpdateOptions } from "./BroadcastCacheUpdate.js";
10
9
  import { BroadcastCacheUpdate } from "./BroadcastCacheUpdate.js";
11
10
  import { BroadcastUpdatePlugin } from "./BroadcastUpdatePlugin.js";
11
+ import { CACHE_UPDATED_MESSAGE_META, CACHE_UPDATED_MESSAGE_TYPE, defaultHeadersToCheck } from "./constants.js";
12
12
  import { responsesAreSame } from "./responsesAreSame.js";
13
13
 
14
- /**
15
- * @module workbox-broadcast-update
16
- */
14
+ export {
15
+ BroadcastCacheUpdate,
16
+ BroadcastUpdatePlugin,
17
+ responsesAreSame,
18
+ CACHE_UPDATED_MESSAGE_META,
19
+ CACHE_UPDATED_MESSAGE_TYPE,
20
+ defaultHeadersToCheck,
21
+ };
17
22
 
18
- export { BroadcastCacheUpdate, BroadcastUpdatePlugin, responsesAreSame };
19
-
20
- export type { BroadcastCacheUpdateOptions };
23
+ export type * from "./types.js";
@@ -12,12 +12,12 @@ import { SerwistError, logger } from "@serwist/core/internal";
12
12
  * Given two `Response's`, compares several header values to see if they are
13
13
  * the same or not.
14
14
  *
15
- * @param firstResponse
16
- * @param secondResponse
17
- * @param headersToCheck
15
+ * @param firstResponse The first response.
16
+ * @param secondResponse The second response.
17
+ * @param headersToCheck A list of headers to check.
18
18
  * @returns
19
19
  */
20
- const responsesAreSame = (firstResponse: Response, secondResponse: Response, headersToCheck: string[]): boolean => {
20
+ export const responsesAreSame = (firstResponse: Response, secondResponse: Response, headersToCheck: string[]): boolean => {
21
21
  if (process.env.NODE_ENV !== "production") {
22
22
  if (!(firstResponse instanceof Response && secondResponse instanceof Response)) {
23
23
  throw new SerwistError("invalid-responses-are-same-args");
@@ -30,7 +30,7 @@ const responsesAreSame = (firstResponse: Response, secondResponse: Response, hea
30
30
 
31
31
  if (!atLeastOneHeaderAvailable) {
32
32
  if (process.env.NODE_ENV !== "production") {
33
- logger.warn("Unable to determine where the response has been updated " + "because none of the headers that would be checked are present.");
33
+ logger.warn("Unable to determine where the response has been updated because none of the headers that would be checked are present.");
34
34
  logger.debug("Attempting to compare the following: ", firstResponse, secondResponse, headersToCheck);
35
35
  }
36
36
 
@@ -46,5 +46,3 @@ const responsesAreSame = (firstResponse: Response, secondResponse: Response, hea
46
46
  return headerStateComparison && headerValueComparison;
47
47
  });
48
48
  };
49
-
50
- export { responsesAreSame };
package/src/types.ts ADDED
@@ -0,0 +1,39 @@
1
+ import type { CacheDidUpdateCallbackParam } from "@serwist/core";
2
+ import type { CACHE_UPDATED_MESSAGE_META, CACHE_UPDATED_MESSAGE_TYPE } from "./constants.js";
3
+
4
+ export interface BroadcastCacheUpdateOptions {
5
+ /**
6
+ * A list of headers that will be used to determine whether the responses
7
+ * differ.
8
+ *
9
+ * @default ['content-length', 'etag', 'last-modified']
10
+ */
11
+ headersToCheck?: string[];
12
+ /**
13
+ * A function whose return value
14
+ * will be used as the `payload` field in any cache update messages sent
15
+ * to the window clients.
16
+ * @param options
17
+ * @returns
18
+ */
19
+ generatePayload?: (options: CacheDidUpdateCallbackParam) => Record<string, any>;
20
+ /**
21
+ * If `true` (the default) then all open clients will receive a message. If `false`,
22
+ * then only the client that make the original request will be notified of the update.
23
+ *
24
+ * @default true
25
+ */
26
+ notifyAllClients?: boolean;
27
+ }
28
+
29
+ export type BroadcastPayload = Record<string, any>;
30
+
31
+ export interface BroadcastPayloadGenerator {
32
+ (options: CacheDidUpdateCallbackParam): BroadcastPayload;
33
+ }
34
+
35
+ export interface BroadcastMessage {
36
+ type: typeof CACHE_UPDATED_MESSAGE_TYPE;
37
+ meta: typeof CACHE_UPDATED_MESSAGE_META;
38
+ payload: BroadcastPayload;
39
+ }
@@ -1,5 +0,0 @@
1
- export declare const CACHE_UPDATED_MESSAGE_TYPE = "CACHE_UPDATED";
2
- export declare const CACHE_UPDATED_MESSAGE_META = "serwist-broadcast-update";
3
- export declare const NOTIFY_ALL_CLIENTS = true;
4
- export declare const DEFAULT_HEADERS_TO_CHECK: string[];
5
- //# sourceMappingURL=constants.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/utils/constants.ts"],"names":[],"mappings":"AAQA,eAAO,MAAM,0BAA0B,kBAAkB,CAAC;AAC1D,eAAO,MAAM,0BAA0B,6BAA6B,CAAC;AACrE,eAAO,MAAM,kBAAkB,OAAO,CAAC;AACvC,eAAO,MAAM,wBAAwB,EAAE,MAAM,EAAgD,CAAC"}