1 | this.workbox = this.workbox || {};
|
2 | this.workbox.broadcastUpdate = (function (exports, assert_js, timeout_js, resultingClientExists_js, logger_js, WorkboxError_js, dontWaitFor_js) {
|
3 | ;
|
4 |
|
5 | try {
|
6 | self['workbox:broadcast-update:5.1.4'] && _();
|
7 | } catch (e) {}
|
8 |
|
9 | /*
|
10 | Copyright 2018 Google LLC
|
11 |
|
12 | Use of this source code is governed by an MIT-style
|
13 | license that can be found in the LICENSE file or at
|
14 | https://opensource.org/licenses/MIT.
|
15 | */
|
16 | /**
|
17 | * Given two `Response's`, compares several header values to see if they are
|
18 | * the same or not.
|
19 | *
|
20 | * @param {Response} firstResponse
|
21 | * @param {Response} secondResponse
|
22 | * @param {Array<string>} headersToCheck
|
23 | * @return {boolean}
|
24 | *
|
25 | * @memberof module:workbox-broadcast-update
|
26 | */
|
27 |
|
28 | const responsesAreSame = (firstResponse, secondResponse, headersToCheck) => {
|
29 | {
|
30 | if (!(firstResponse instanceof Response && secondResponse instanceof Response)) {
|
31 | throw new WorkboxError_js.WorkboxError('invalid-responses-are-same-args');
|
32 | }
|
33 | }
|
34 |
|
35 | const atLeastOneHeaderAvailable = headersToCheck.some(header => {
|
36 | return firstResponse.headers.has(header) && secondResponse.headers.has(header);
|
37 | });
|
38 |
|
39 | if (!atLeastOneHeaderAvailable) {
|
40 | {
|
41 | logger_js.logger.warn(`Unable to determine where the response has been updated ` + `because none of the headers that would be checked are present.`);
|
42 | logger_js.logger.debug(`Attempting to compare the following: `, firstResponse, secondResponse, headersToCheck);
|
43 | } // Just return true, indicating the that responses are the same, since we
|
44 | // can't determine otherwise.
|
45 |
|
46 |
|
47 | return true;
|
48 | }
|
49 |
|
50 | return headersToCheck.every(header => {
|
51 | const headerStateComparison = firstResponse.headers.has(header) === secondResponse.headers.has(header);
|
52 | const headerValueComparison = firstResponse.headers.get(header) === secondResponse.headers.get(header);
|
53 | return headerStateComparison && headerValueComparison;
|
54 | });
|
55 | };
|
56 |
|
57 | /*
|
58 | Copyright 2018 Google LLC
|
59 |
|
60 | Use of this source code is governed by an MIT-style
|
61 | license that can be found in the LICENSE file or at
|
62 | https://opensource.org/licenses/MIT.
|
63 | */
|
64 | const CACHE_UPDATED_MESSAGE_TYPE = 'CACHE_UPDATED';
|
65 | const CACHE_UPDATED_MESSAGE_META = 'workbox-broadcast-update';
|
66 | const DEFAULT_HEADERS_TO_CHECK = ['content-length', 'etag', 'last-modified'];
|
67 |
|
68 | /*
|
69 | Copyright 2018 Google LLC
|
70 |
|
71 | Use of this source code is governed by an MIT-style
|
72 | license that can be found in the LICENSE file or at
|
73 | https://opensource.org/licenses/MIT.
|
74 | */
|
75 | // TODO(philipwalton): remove once this Safari bug fix has been released.
|
76 | // https://bugs.webkit.org/show_bug.cgi?id=201169
|
77 |
|
78 | const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
|
79 | /**
|
80 | * Generates the default payload used in update messages. By default the
|
81 | * payload includes the `cacheName` and `updatedURL` fields.
|
82 | *
|
83 | * @return Object
|
84 | * @private
|
85 | */
|
86 |
|
87 | function defaultPayloadGenerator(data) {
|
88 | return {
|
89 | cacheName: data.cacheName,
|
90 | updatedURL: data.request.url
|
91 | };
|
92 | }
|
93 | /**
|
94 | * Uses the `postMessage()` API to inform any open windows/tabs when a cached
|
95 | * response has been updated.
|
96 | *
|
97 | * For efficiency's sake, the underlying response bodies are not compared;
|
98 | * only specific response headers are checked.
|
99 | *
|
100 | * @memberof module:workbox-broadcast-update
|
101 | */
|
102 |
|
103 |
|
104 | class BroadcastCacheUpdate {
|
105 | /**
|
106 | * Construct a BroadcastCacheUpdate instance with a specific `channelName` to
|
107 | * broadcast messages on
|
108 | *
|
109 | * @param {Object} options
|
110 | * @param {Array<string>} [options.headersToCheck=['content-length', 'etag', 'last-modified']]
|
111 | * A list of headers that will be used to determine whether the responses
|
112 | * differ.
|
113 | * @param {string} [options.generatePayload] A function whose return value
|
114 | * will be used as the `payload` field in any cache update messages sent
|
115 | * to the window clients.
|
116 | */
|
117 | constructor({
|
118 | headersToCheck,
|
119 | generatePayload
|
120 | } = {}) {
|
121 | this._headersToCheck = headersToCheck || DEFAULT_HEADERS_TO_CHECK;
|
122 | this._generatePayload = generatePayload || defaultPayloadGenerator;
|
123 | }
|
124 | /**
|
125 | * Compares two [Responses](https://developer.mozilla.org/en-US/docs/Web/API/Response)
|
126 | * and sends a message (via `postMessage()`) to all window clients if the
|
127 | * responses differ (note: neither of the Responses can be
|
128 | * {@link http://stackoverflow.com/questions/39109789|opaque}).
|
129 | *
|
130 | * The message that's posted has the following format (where `payload` can
|
131 | * be customized via the `generatePayload` option the instance is created
|
132 | * with):
|
133 | *
|
134 | * ```
|
135 | * {
|
136 | * type: 'CACHE_UPDATED',
|
137 | * meta: 'workbox-broadcast-update',
|
138 | * payload: {
|
139 | * cacheName: 'the-cache-name',
|
140 | * updatedURL: 'https://example.com/'
|
141 | * }
|
142 | * }
|
143 | * ```
|
144 | *
|
145 | * @param {Object} options
|
146 | * @param {Response} [options.oldResponse] Cached response to compare.
|
147 | * @param {Response} options.newResponse Possibly updated response to compare.
|
148 | * @param {Request} options.request The request.
|
149 | * @param {string} options.cacheName Name of the cache the responses belong
|
150 | * to. This is included in the broadcast message.
|
151 | * @param {Event} [options.event] event An optional event that triggered
|
152 | * this possible cache update.
|
153 | * @return {Promise} Resolves once the update is sent.
|
154 | */
|
155 |
|
156 |
|
157 | async notifyIfUpdated(options) {
|
158 | {
|
159 | assert_js.assert.isType(options.cacheName, 'string', {
|
160 | moduleName: 'workbox-broadcast-update',
|
161 | className: 'BroadcastCacheUpdate',
|
162 | funcName: 'notifyIfUpdated',
|
163 | paramName: 'cacheName'
|
164 | });
|
165 | assert_js.assert.isInstance(options.newResponse, Response, {
|
166 | moduleName: 'workbox-broadcast-update',
|
167 | className: 'BroadcastCacheUpdate',
|
168 | funcName: 'notifyIfUpdated',
|
169 | paramName: 'newResponse'
|
170 | });
|
171 | assert_js.assert.isInstance(options.request, Request, {
|
172 | moduleName: 'workbox-broadcast-update',
|
173 | className: 'BroadcastCacheUpdate',
|
174 | funcName: 'notifyIfUpdated',
|
175 | paramName: 'request'
|
176 | });
|
177 | } // Without two responses there is nothing to compare.
|
178 |
|
179 |
|
180 | if (!options.oldResponse) {
|
181 | return;
|
182 | }
|
183 |
|
184 | if (!responsesAreSame(options.oldResponse, options.newResponse, this._headersToCheck)) {
|
185 | {
|
186 | logger_js.logger.log(`Newer response found (and cached) for:`, options.request.url);
|
187 | }
|
188 |
|
189 | const messageData = {
|
190 | type: CACHE_UPDATED_MESSAGE_TYPE,
|
191 | meta: CACHE_UPDATED_MESSAGE_META,
|
192 | payload: this._generatePayload(options)
|
193 | }; // For navigation requests, wait until the new window client exists
|
194 | // before sending the message
|
195 |
|
196 | if (options.request.mode === 'navigate') {
|
197 | let resultingClientId;
|
198 |
|
199 | if (options.event instanceof FetchEvent) {
|
200 | resultingClientId = options.event.resultingClientId;
|
201 | }
|
202 |
|
203 | const resultingWin = await resultingClientExists_js.resultingClientExists(resultingClientId); // Safari does not currently implement postMessage buffering and
|
204 | // there's no good way to feature detect that, so to increase the
|
205 | // chances of the message being delivered in Safari, we add a timeout.
|
206 | // We also do this if `resultingClientExists()` didn't return a client,
|
207 | // which means it timed out, so it's worth waiting a bit longer.
|
208 |
|
209 | if (!resultingWin || isSafari) {
|
210 | // 3500 is chosen because (according to CrUX data) 80% of mobile
|
211 | // websites hit the DOMContentLoaded event in less than 3.5 seconds.
|
212 | // And presumably sites implementing service worker are on the
|
213 | // higher end of the performance spectrum.
|
214 | await timeout_js.timeout(3500);
|
215 | }
|
216 | }
|
217 |
|
218 | const windows = await self.clients.matchAll({
|
219 | type: 'window'
|
220 | });
|
221 |
|
222 | for (const win of windows) {
|
223 | win.postMessage(messageData);
|
224 | }
|
225 | }
|
226 | }
|
227 |
|
228 | }
|
229 |
|
230 | /*
|
231 | Copyright 2018 Google LLC
|
232 |
|
233 | Use of this source code is governed by an MIT-style
|
234 | license that can be found in the LICENSE file or at
|
235 | https://opensource.org/licenses/MIT.
|
236 | */
|
237 | /**
|
238 | * This plugin will automatically broadcast a message whenever a cached response
|
239 | * is updated.
|
240 | *
|
241 | * @memberof module:workbox-broadcast-update
|
242 | */
|
243 |
|
244 | class BroadcastUpdatePlugin {
|
245 | /**
|
246 | * Construct a BroadcastCacheUpdate instance with the passed options and
|
247 | * calls its [`notifyIfUpdated()`]{@link module:workbox-broadcast-update.BroadcastCacheUpdate~notifyIfUpdated}
|
248 | * method whenever the plugin's `cacheDidUpdate` callback is invoked.
|
249 | *
|
250 | * @param {Object} options
|
251 | * @param {Array<string>} [options.headersToCheck=['content-length', 'etag', 'last-modified']]
|
252 | * A list of headers that will be used to determine whether the responses
|
253 | * differ.
|
254 | * @param {string} [options.generatePayload] A function whose return value
|
255 | * will be used as the `payload` field in any cache update messages sent
|
256 | * to the window clients.
|
257 | */
|
258 | constructor(options) {
|
259 | /**
|
260 | * A "lifecycle" callback that will be triggered automatically by the
|
261 | * `workbox-sw` and `workbox-runtime-caching` handlers when an entry is
|
262 | * added to a cache.
|
263 | *
|
264 | * @private
|
265 | * @param {Object} options The input object to this function.
|
266 | * @param {string} options.cacheName Name of the cache being updated.
|
267 | * @param {Response} [options.oldResponse] The previous cached value, if any.
|
268 | * @param {Response} options.newResponse The new value in the cache.
|
269 | * @param {Request} options.request The request that triggered the update.
|
270 | * @param {Request} [options.event] The event that triggered the update.
|
271 | */
|
272 | this.cacheDidUpdate = async options => {
|
273 | dontWaitFor_js.dontWaitFor(this._broadcastUpdate.notifyIfUpdated(options));
|
274 | };
|
275 |
|
276 | this._broadcastUpdate = new BroadcastCacheUpdate(options);
|
277 | }
|
278 |
|
279 | }
|
280 |
|
281 | exports.BroadcastCacheUpdate = BroadcastCacheUpdate;
|
282 | exports.BroadcastUpdatePlugin = BroadcastUpdatePlugin;
|
283 | exports.responsesAreSame = responsesAreSame;
|
284 |
|
285 | return exports;
|
286 |
|
287 | }({}, workbox.core._private, workbox.core._private, workbox.core._private, workbox.core._private, workbox.core._private, workbox.core._private));
|
288 |
|