@serwist/broadcast-update 9.0.0-preview.0 → 9.0.0-preview.10
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/BroadcastCacheUpdate.d.ts +2 -26
- package/dist/BroadcastCacheUpdate.d.ts.map +1 -1
- package/dist/BroadcastUpdatePlugin.d.ts +4 -5
- package/dist/BroadcastUpdatePlugin.d.ts.map +1 -1
- package/dist/constants.d.ts +5 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/index.d.ts +3 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +24 -112
- package/dist/types.d.ts +36 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +5 -5
- package/src/BroadcastCacheUpdate.ts +9 -36
- package/src/BroadcastUpdatePlugin.ts +6 -9
- package/src/{utils/constants.ts → constants.ts} +2 -2
- package/src/index.ts +10 -7
- package/src/types.ts +39 -0
- package/dist/utils/constants.d.ts +0 -5
- package/dist/utils/constants.d.ts.map +0 -1
|
@@ -1,28 +1,5 @@
|
|
|
1
1
|
import type { CacheDidUpdateCallbackParam } from "@serwist/core";
|
|
2
|
-
|
|
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
4
|
* Uses the `postMessage()` API to inform any open windows/tabs when a cached
|
|
28
5
|
* response has been updated.
|
|
@@ -30,7 +7,7 @@ export interface BroadcastCacheUpdateOptions {
|
|
|
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;
|
|
@@ -67,5 +44,4 @@ declare class BroadcastCacheUpdate {
|
|
|
67
44
|
*/
|
|
68
45
|
notifyIfUpdated(options: CacheDidUpdateCallbackParam): Promise<void>;
|
|
69
46
|
}
|
|
70
|
-
export { BroadcastCacheUpdate };
|
|
71
47
|
//# 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;
|
|
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;;;;;;;;;;;;;;;;;;;;;;;OAuBG;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 "./
|
|
1
|
+
import type { CacheDidUpdateCallbackParam, SerwistPlugin } from "@serwist/core";
|
|
2
|
+
import type { BroadcastCacheUpdateOptions } from "./types.js";
|
|
3
3
|
/**
|
|
4
4
|
* This plugin will automatically broadcast a message whenever a cached response
|
|
5
5
|
* 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:
|
|
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;
|
|
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
|
-
*
|
|
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
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,
|
|
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,14 +1,15 @@
|
|
|
1
|
-
import { SerwistError, logger, assert, resultingClientExists, timeout
|
|
1
|
+
import { SerwistError, logger, assert, resultingClientExists, timeout } from '@serwist/core/internal';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
+
];
|
|
11
|
+
|
|
12
|
+
const responsesAreSame = (firstResponse, secondResponse, headersToCheck)=>{
|
|
12
13
|
if (process.env.NODE_ENV !== "production") {
|
|
13
14
|
if (!(firstResponse instanceof Response && secondResponse instanceof Response)) {
|
|
14
15
|
throw new SerwistError("invalid-responses-are-same-args");
|
|
@@ -22,8 +23,6 @@ import { SerwistError, logger, assert, resultingClientExists, timeout, dontWaitF
|
|
|
22
23
|
logger.warn("Unable to determine where the response has been updated " + "because none of the headers that would be checked are present.");
|
|
23
24
|
logger.debug("Attempting to compare the following: ", firstResponse, secondResponse, headersToCheck);
|
|
24
25
|
}
|
|
25
|
-
// Just return true, indicating the that responses are the same, since we
|
|
26
|
-
// can't determine otherwise.
|
|
27
26
|
return true;
|
|
28
27
|
}
|
|
29
28
|
return headersToCheck.every((header)=>{
|
|
@@ -33,81 +32,23 @@ import { SerwistError, logger, assert, resultingClientExists, timeout, dontWaitF
|
|
|
33
32
|
});
|
|
34
33
|
};
|
|
35
34
|
|
|
36
|
-
/*
|
|
37
|
-
Copyright 2018 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
|
-
*/ const CACHE_UPDATED_MESSAGE_TYPE = "CACHE_UPDATED";
|
|
43
|
-
const CACHE_UPDATED_MESSAGE_META = "serwist-broadcast-update";
|
|
44
|
-
const NOTIFY_ALL_CLIENTS = true;
|
|
45
|
-
const DEFAULT_HEADERS_TO_CHECK = [
|
|
46
|
-
"content-length",
|
|
47
|
-
"etag",
|
|
48
|
-
"last-modified"
|
|
49
|
-
];
|
|
50
|
-
|
|
51
|
-
// UA-sniff Safari: https://stackoverflow.com/questions/7944460/detect-safari-browser
|
|
52
|
-
// TODO(philipwalton): remove once this Safari bug fix has been released.
|
|
53
|
-
// https://bugs.webkit.org/show_bug.cgi?id=201169
|
|
54
35
|
const isSafari = typeof navigator !== "undefined" && /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
|
|
55
|
-
|
|
56
|
-
* Generates the default payload used in update messages. By default the
|
|
57
|
-
* payload includes the `cacheName` and `updatedURL` fields.
|
|
58
|
-
*
|
|
59
|
-
* @returns
|
|
60
|
-
* @private
|
|
61
|
-
*/ function defaultPayloadGenerator(data) {
|
|
36
|
+
const defaultPayloadGenerator = (data)=>{
|
|
62
37
|
return {
|
|
63
38
|
cacheName: data.cacheName,
|
|
64
39
|
updatedURL: data.request.url
|
|
65
40
|
};
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
* Uses the `postMessage()` API to inform any open windows/tabs when a cached
|
|
69
|
-
* response has been updated.
|
|
70
|
-
*
|
|
71
|
-
* For efficiency's sake, the underlying response bodies are not compared;
|
|
72
|
-
* only specific response headers are checked.
|
|
73
|
-
*/ class BroadcastCacheUpdate {
|
|
41
|
+
};
|
|
42
|
+
class BroadcastCacheUpdate {
|
|
74
43
|
_headersToCheck;
|
|
75
44
|
_generatePayload;
|
|
76
45
|
_notifyAllClients;
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
* broadcast messages on
|
|
80
|
-
*
|
|
81
|
-
* @param options
|
|
82
|
-
*/ constructor({ generatePayload, headersToCheck, notifyAllClients } = {}){
|
|
83
|
-
this._headersToCheck = headersToCheck || DEFAULT_HEADERS_TO_CHECK;
|
|
46
|
+
constructor({ generatePayload, headersToCheck, notifyAllClients } = {}){
|
|
47
|
+
this._headersToCheck = headersToCheck || defaultHeadersToCheck;
|
|
84
48
|
this._generatePayload = generatePayload || defaultPayloadGenerator;
|
|
85
|
-
this._notifyAllClients = notifyAllClients ??
|
|
49
|
+
this._notifyAllClients = notifyAllClients ?? defaultNotifyAllClients;
|
|
86
50
|
}
|
|
87
|
-
|
|
88
|
-
* Compares two [Responses](https://developer.mozilla.org/en-US/docs/Web/API/Response)
|
|
89
|
-
* and sends a message (via `postMessage()`) to all window clients if the
|
|
90
|
-
* responses differ. Neither of the Responses can be
|
|
91
|
-
* [opaque](https://developer.chrome.com/docs/workbox/caching-resources-during-runtime/#opaque-responses).
|
|
92
|
-
*
|
|
93
|
-
* The message that's posted has the following format (where `payload` can
|
|
94
|
-
* be customized via the `generatePayload` option the instance is created
|
|
95
|
-
* with):
|
|
96
|
-
*
|
|
97
|
-
* ```
|
|
98
|
-
* {
|
|
99
|
-
* type: 'CACHE_UPDATED',
|
|
100
|
-
* meta: 'workbox-broadcast-update',
|
|
101
|
-
* payload: {
|
|
102
|
-
* cacheName: 'the-cache-name',
|
|
103
|
-
* updatedURL: 'https://example.com/'
|
|
104
|
-
* }
|
|
105
|
-
* }
|
|
106
|
-
* ```
|
|
107
|
-
*
|
|
108
|
-
* @param options
|
|
109
|
-
* @returns Resolves once the update is sent.
|
|
110
|
-
*/ async notifyIfUpdated(options) {
|
|
51
|
+
async notifyIfUpdated(options) {
|
|
111
52
|
if (process.env.NODE_ENV !== "production") {
|
|
112
53
|
assert.isType(options.cacheName, "string", {
|
|
113
54
|
moduleName: "@serwist/broadcast-update",
|
|
@@ -128,7 +69,6 @@ const isSafari = typeof navigator !== "undefined" && /^((?!chrome|android).)*saf
|
|
|
128
69
|
paramName: "request"
|
|
129
70
|
});
|
|
130
71
|
}
|
|
131
|
-
// Without two responses there is nothing to compare.
|
|
132
72
|
if (!options.oldResponse) {
|
|
133
73
|
return;
|
|
134
74
|
}
|
|
@@ -141,24 +81,13 @@ const isSafari = typeof navigator !== "undefined" && /^((?!chrome|android).)*saf
|
|
|
141
81
|
meta: CACHE_UPDATED_MESSAGE_META,
|
|
142
82
|
payload: this._generatePayload(options)
|
|
143
83
|
};
|
|
144
|
-
// For navigation requests, wait until the new window client exists
|
|
145
|
-
// before sending the message
|
|
146
84
|
if (options.request.mode === "navigate") {
|
|
147
85
|
let resultingClientId;
|
|
148
86
|
if (options.event instanceof FetchEvent) {
|
|
149
87
|
resultingClientId = options.event.resultingClientId;
|
|
150
88
|
}
|
|
151
89
|
const resultingWin = await resultingClientExists(resultingClientId);
|
|
152
|
-
// Safari does not currently implement postMessage buffering and
|
|
153
|
-
// there's no good way to feature detect that, so to increase the
|
|
154
|
-
// chances of the message being delivered in Safari, we add a timeout.
|
|
155
|
-
// We also do this if `resultingClientExists()` didn't return a client,
|
|
156
|
-
// which means it timed out, so it's worth waiting a bit longer.
|
|
157
90
|
if (!resultingWin || isSafari) {
|
|
158
|
-
// 3500 is chosen because (according to CrUX data) 80% of mobile
|
|
159
|
-
// websites hit the DOMContentLoaded event in less than 3.5 seconds.
|
|
160
|
-
// And presumably sites implementing service worker are on the
|
|
161
|
-
// higher end of the performance spectrum.
|
|
162
91
|
await timeout(3500);
|
|
163
92
|
}
|
|
164
93
|
}
|
|
@@ -170,7 +99,6 @@ const isSafari = typeof navigator !== "undefined" && /^((?!chrome|android).)*saf
|
|
|
170
99
|
win.postMessage(messageData);
|
|
171
100
|
}
|
|
172
101
|
} else {
|
|
173
|
-
// See https://github.com/GoogleChrome/workbox/issues/2895
|
|
174
102
|
if (options.event instanceof FetchEvent) {
|
|
175
103
|
const client = await self.clients.get(options.event.clientId);
|
|
176
104
|
client?.postMessage(messageData);
|
|
@@ -180,30 +108,14 @@ const isSafari = typeof navigator !== "undefined" && /^((?!chrome|android).)*saf
|
|
|
180
108
|
}
|
|
181
109
|
}
|
|
182
110
|
|
|
183
|
-
|
|
184
|
-
* This plugin will automatically broadcast a message whenever a cached response
|
|
185
|
-
* is updated.
|
|
186
|
-
*/ class BroadcastUpdatePlugin {
|
|
111
|
+
class BroadcastUpdatePlugin {
|
|
187
112
|
_broadcastUpdate;
|
|
188
|
-
|
|
189
|
-
* Construct a `@serwist/broadcast-update.BroadcastCacheUpdate` instance with
|
|
190
|
-
* the passed options and calls its `notifyIfUpdated` method whenever the
|
|
191
|
-
* plugin's `cacheDidUpdate` callback is invoked.
|
|
192
|
-
*
|
|
193
|
-
* @param options
|
|
194
|
-
*/ constructor(options){
|
|
113
|
+
constructor(options){
|
|
195
114
|
this._broadcastUpdate = new BroadcastCacheUpdate(options);
|
|
196
115
|
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
* added to a cache.
|
|
201
|
-
*
|
|
202
|
-
* @private
|
|
203
|
-
* @param options The input object to this function.
|
|
204
|
-
*/ cacheDidUpdate = async (options)=>{
|
|
205
|
-
dontWaitFor(this._broadcastUpdate.notifyIfUpdated(options));
|
|
206
|
-
};
|
|
116
|
+
cacheDidUpdate(options) {
|
|
117
|
+
void this._broadcastUpdate.notifyIfUpdated(options);
|
|
118
|
+
}
|
|
207
119
|
}
|
|
208
120
|
|
|
209
|
-
export { BroadcastCacheUpdate, BroadcastUpdatePlugin, responsesAreSame };
|
|
121
|
+
export { BroadcastCacheUpdate, BroadcastUpdatePlugin, CACHE_UPDATED_MESSAGE_META, CACHE_UPDATED_MESSAGE_TYPE, defaultHeadersToCheck, responsesAreSame };
|
package/dist/types.d.ts
ADDED
|
@@ -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.
|
|
3
|
+
"version": "9.0.0-preview.10",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"description": "A
|
|
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.
|
|
32
|
+
"@serwist/core": "9.0.0-preview.10"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"rollup": "4.9.6",
|
|
36
|
-
"typescript": "5.4.0-dev.
|
|
37
|
-
"@serwist/constants": "9.0.0-preview.
|
|
36
|
+
"typescript": "5.4.0-dev.20240206",
|
|
37
|
+
"@serwist/constants": "9.0.0-preview.10"
|
|
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 {
|
|
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,12 +27,12 @@ export interface BroadcastCacheUpdateOptions {
|
|
|
52
27
|
* @returns
|
|
53
28
|
* @private
|
|
54
29
|
*/
|
|
55
|
-
|
|
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
38
|
* Uses the `postMessage()` API to inform any open windows/tabs when a cached
|
|
@@ -66,9 +41,9 @@ function defaultPayloadGenerator(data: CacheDidUpdateCallbackParam): Record<stri
|
|
|
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:
|
|
46
|
+
private readonly _generatePayload: BroadcastPayloadGenerator;
|
|
72
47
|
private readonly _notifyAllClients: boolean;
|
|
73
48
|
|
|
74
49
|
/**
|
|
@@ -78,9 +53,9 @@ class BroadcastCacheUpdate {
|
|
|
78
53
|
* @param options
|
|
79
54
|
*/
|
|
80
55
|
constructor({ generatePayload, headersToCheck, notifyAllClients }: BroadcastCacheUpdateOptions = {}) {
|
|
81
|
-
this._headersToCheck = headersToCheck ||
|
|
56
|
+
this._headersToCheck = headersToCheck || defaultHeadersToCheck;
|
|
82
57
|
this._generatePayload = generatePayload || defaultPayloadGenerator;
|
|
83
|
-
this._notifyAllClients = notifyAllClients ??
|
|
58
|
+
this._notifyAllClients = notifyAllClients ?? defaultNotifyAllClients;
|
|
84
59
|
}
|
|
85
60
|
|
|
86
61
|
/**
|
|
@@ -143,7 +118,7 @@ class BroadcastCacheUpdate {
|
|
|
143
118
|
type: CACHE_UPDATED_MESSAGE_TYPE,
|
|
144
119
|
meta: CACHE_UPDATED_MESSAGE_META,
|
|
145
120
|
payload: this._generatePayload(options),
|
|
146
|
-
};
|
|
121
|
+
} satisfies BroadcastMessage;
|
|
147
122
|
|
|
148
123
|
// For navigation requests, wait until the new window client exists
|
|
149
124
|
// before sending the message
|
|
@@ -184,5 +159,3 @@ class BroadcastCacheUpdate {
|
|
|
184
159
|
}
|
|
185
160
|
}
|
|
186
161
|
}
|
|
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
15
|
* This plugin will automatically broadcast a message whenever a cached response
|
|
17
16
|
* 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:
|
|
42
|
-
|
|
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
|
|
12
|
-
export const
|
|
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
|
-
|
|
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
|
|
19
|
-
|
|
20
|
-
export type { BroadcastCacheUpdateOptions };
|
|
23
|
+
export type * from "./types.js";
|
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"}
|