@serwist/broadcast-update
Advanced tools
Comparing version 9.0.0-preview.0 to 9.0.0-preview.1
import { SerwistError, logger, assert, resultingClientExists, timeout, dontWaitFor } from '@serwist/core/internal'; | ||
/** | ||
* Given two `Response's`, compares several header values to see if they are | ||
* the same or not. | ||
* | ||
* @param firstResponse | ||
* @param secondResponse | ||
* @param headersToCheck | ||
* @returns | ||
*/ const responsesAreSame = (firstResponse, secondResponse, headersToCheck)=>{ | ||
const responsesAreSame = (firstResponse, secondResponse, headersToCheck)=>{ | ||
if (process.env.NODE_ENV !== "production") { | ||
@@ -25,4 +17,2 @@ if (!(firstResponse instanceof Response && secondResponse instanceof Response)) { | ||
} | ||
// Just return true, indicating the that responses are the same, since we | ||
// can't determine otherwise. | ||
return true; | ||
@@ -37,9 +27,3 @@ } | ||
/* | ||
Copyright 2018 Google LLC | ||
Use of this source code is governed by an MIT-style | ||
license that can be found in the LICENSE file or at | ||
https://opensource.org/licenses/MIT. | ||
*/ const CACHE_UPDATED_MESSAGE_TYPE = "CACHE_UPDATED"; | ||
const CACHE_UPDATED_MESSAGE_TYPE = "CACHE_UPDATED"; | ||
const CACHE_UPDATED_MESSAGE_META = "serwist-broadcast-update"; | ||
@@ -53,13 +37,4 @@ const NOTIFY_ALL_CLIENTS = true; | ||
// UA-sniff Safari: https://stackoverflow.com/questions/7944460/detect-safari-browser | ||
// TODO(philipwalton): remove once this Safari bug fix has been released. | ||
// https://bugs.webkit.org/show_bug.cgi?id=201169 | ||
const isSafari = typeof navigator !== "undefined" && /^((?!chrome|android).)*safari/i.test(navigator.userAgent); | ||
/** | ||
* Generates the default payload used in update messages. By default the | ||
* payload includes the `cacheName` and `updatedURL` fields. | ||
* | ||
* @returns | ||
* @private | ||
*/ function defaultPayloadGenerator(data) { | ||
function defaultPayloadGenerator(data) { | ||
return { | ||
@@ -70,18 +45,7 @@ cacheName: data.cacheName, | ||
} | ||
/** | ||
* Uses the `postMessage()` API to inform any open windows/tabs when a cached | ||
* response has been updated. | ||
* | ||
* For efficiency's sake, the underlying response bodies are not compared; | ||
* only specific response headers are checked. | ||
*/ class BroadcastCacheUpdate { | ||
class BroadcastCacheUpdate { | ||
_headersToCheck; | ||
_generatePayload; | ||
_notifyAllClients; | ||
/** | ||
* Construct a BroadcastCacheUpdate instance with a specific `channelName` to | ||
* broadcast messages on | ||
* | ||
* @param options | ||
*/ constructor({ generatePayload, headersToCheck, notifyAllClients } = {}){ | ||
constructor({ generatePayload, headersToCheck, notifyAllClients } = {}){ | ||
this._headersToCheck = headersToCheck || DEFAULT_HEADERS_TO_CHECK; | ||
@@ -91,26 +55,3 @@ this._generatePayload = generatePayload || defaultPayloadGenerator; | ||
} | ||
/** | ||
* Compares two [Responses](https://developer.mozilla.org/en-US/docs/Web/API/Response) | ||
* and sends a message (via `postMessage()`) to all window clients if the | ||
* responses differ. Neither of the Responses can be | ||
* [opaque](https://developer.chrome.com/docs/workbox/caching-resources-during-runtime/#opaque-responses). | ||
* | ||
* The message that's posted has the following format (where `payload` can | ||
* be customized via the `generatePayload` option the instance is created | ||
* with): | ||
* | ||
* ``` | ||
* { | ||
* type: 'CACHE_UPDATED', | ||
* meta: 'workbox-broadcast-update', | ||
* payload: { | ||
* cacheName: 'the-cache-name', | ||
* updatedURL: 'https://example.com/' | ||
* } | ||
* } | ||
* ``` | ||
* | ||
* @param options | ||
* @returns Resolves once the update is sent. | ||
*/ async notifyIfUpdated(options) { | ||
async notifyIfUpdated(options) { | ||
if (process.env.NODE_ENV !== "production") { | ||
@@ -136,3 +77,2 @@ assert.isType(options.cacheName, "string", { | ||
} | ||
// Without two responses there is nothing to compare. | ||
if (!options.oldResponse) { | ||
@@ -150,4 +90,2 @@ return; | ||
}; | ||
// For navigation requests, wait until the new window client exists | ||
// before sending the message | ||
if (options.request.mode === "navigate") { | ||
@@ -159,12 +97,3 @@ let resultingClientId; | ||
const resultingWin = await resultingClientExists(resultingClientId); | ||
// Safari does not currently implement postMessage buffering and | ||
// there's no good way to feature detect that, so to increase the | ||
// chances of the message being delivered in Safari, we add a timeout. | ||
// We also do this if `resultingClientExists()` didn't return a client, | ||
// which means it timed out, so it's worth waiting a bit longer. | ||
if (!resultingWin || isSafari) { | ||
// 3500 is chosen because (according to CrUX data) 80% of mobile | ||
// websites hit the DOMContentLoaded event in less than 3.5 seconds. | ||
// And presumably sites implementing service worker are on the | ||
// higher end of the performance spectrum. | ||
await timeout(3500); | ||
@@ -181,3 +110,2 @@ } | ||
} else { | ||
// See https://github.com/GoogleChrome/workbox/issues/2895 | ||
if (options.event instanceof FetchEvent) { | ||
@@ -192,24 +120,8 @@ const client = await self.clients.get(options.event.clientId); | ||
/** | ||
* This plugin will automatically broadcast a message whenever a cached response | ||
* is updated. | ||
*/ class BroadcastUpdatePlugin { | ||
class BroadcastUpdatePlugin { | ||
_broadcastUpdate; | ||
/** | ||
* Construct a `@serwist/broadcast-update.BroadcastCacheUpdate` instance with | ||
* the passed options and calls its `notifyIfUpdated` method whenever the | ||
* plugin's `cacheDidUpdate` callback is invoked. | ||
* | ||
* @param options | ||
*/ constructor(options){ | ||
constructor(options){ | ||
this._broadcastUpdate = new BroadcastCacheUpdate(options); | ||
} | ||
/** | ||
* A "lifecycle" callback that will be triggered automatically by | ||
* `@serwist/build.RuntimeCaching` handlers when an entry is | ||
* added to a cache. | ||
* | ||
* @private | ||
* @param options The input object to this function. | ||
*/ cacheDidUpdate = async (options)=>{ | ||
cacheDidUpdate = async (options)=>{ | ||
dontWaitFor(this._broadcastUpdate.notifyIfUpdated(options)); | ||
@@ -216,0 +128,0 @@ }; |
{ | ||
"name": "@serwist/broadcast-update", | ||
"version": "9.0.0-preview.0", | ||
"version": "9.0.0-preview.1", | ||
"type": "module", | ||
@@ -32,3 +32,3 @@ "description": "A service worker helper library that uses the Broadcast Channel API to announce when a cached response has updated", | ||
"dependencies": { | ||
"@serwist/core": "9.0.0-preview.0" | ||
"@serwist/core": "9.0.0-preview.1" | ||
}, | ||
@@ -38,3 +38,3 @@ "devDependencies": { | ||
"typescript": "5.4.0-dev.20240203", | ||
"@serwist/constants": "9.0.0-preview.0" | ||
"@serwist/constants": "9.0.0-preview.1" | ||
}, | ||
@@ -41,0 +41,0 @@ "peerDependencies": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 2 instances in 1 package
3
25381
512
+ Added@serwist/core@9.0.0-preview.1(transitive)
- Removed@serwist/core@9.0.0-preview.0(transitive)