@serwist/sw 9.0.0-preview.17 → 9.0.0-preview.18
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/{Serwist.d.ts → abstractions/Serwist.d.ts} +6 -15
- package/dist/abstractions/Serwist.d.ts.map +1 -0
- package/dist/abstractions/disableDevLogs.d.ts.map +1 -0
- package/dist/{fallbacks.d.ts → abstractions/fallbacks.d.ts} +2 -2
- package/dist/abstractions/fallbacks.d.ts.map +1 -0
- package/dist/{handlePrecaching.d.ts → abstractions/handlePrecaching.d.ts} +1 -1
- package/dist/abstractions/handlePrecaching.d.ts.map +1 -0
- package/dist/abstractions/installSerwist.d.ts.map +1 -0
- package/dist/abstractions/registerRuntimeCaching.d.ts.map +1 -0
- package/dist/{types.d.ts → abstractions/types.d.ts} +1 -1
- package/dist/abstractions/types.d.ts.map +1 -0
- package/dist/chunks/NavigationRoute.js +54 -0
- package/dist/chunks/NetworkOnly.js +193 -0
- package/dist/chunks/PrecacheFallbackPlugin.js +573 -0
- package/dist/chunks/Strategy.js +410 -0
- package/dist/chunks/getOrCreatePrecacheController.js +429 -0
- package/dist/chunks/precacheAndRoute.js +112 -0
- package/dist/chunks/registerRoute.js +408 -0
- package/dist/index.d.ts +8 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -9
- package/dist/index.plugins.d.ts +40 -11
- package/dist/index.plugins.d.ts.map +1 -1
- package/dist/index.plugins.js +671 -5
- package/dist/index.precaching.d.ts +24 -0
- package/dist/index.precaching.d.ts.map +1 -0
- package/dist/index.precaching.js +24 -0
- package/dist/index.routing.d.ts +13 -0
- package/dist/index.routing.d.ts.map +1 -0
- package/dist/index.routing.js +21 -0
- package/dist/index.strategies.d.ts +19 -2
- package/dist/index.strategies.d.ts.map +1 -1
- package/dist/index.strategies.js +146 -1
- package/dist/plugins/backgroundSync/BackgroundSyncPlugin.d.ts +23 -0
- package/dist/plugins/backgroundSync/BackgroundSyncPlugin.d.ts.map +1 -0
- package/dist/plugins/backgroundSync/Queue.d.ts +166 -0
- package/dist/plugins/backgroundSync/Queue.d.ts.map +1 -0
- package/dist/plugins/backgroundSync/QueueDb.d.ts +90 -0
- package/dist/plugins/backgroundSync/QueueDb.d.ts.map +1 -0
- package/dist/plugins/backgroundSync/QueueStore.d.ts +75 -0
- package/dist/plugins/backgroundSync/QueueStore.d.ts.map +1 -0
- package/dist/plugins/backgroundSync/StorableRequest.d.ts +51 -0
- package/dist/plugins/backgroundSync/StorableRequest.d.ts.map +1 -0
- package/dist/plugins/broadcastUpdate/BroadcastCacheUpdate.d.ts +45 -0
- package/dist/plugins/broadcastUpdate/BroadcastCacheUpdate.d.ts.map +1 -0
- package/dist/plugins/broadcastUpdate/BroadcastUpdatePlugin.d.ts +27 -0
- package/dist/plugins/broadcastUpdate/BroadcastUpdatePlugin.d.ts.map +1 -0
- package/dist/plugins/broadcastUpdate/constants.d.ts +5 -0
- package/dist/plugins/broadcastUpdate/constants.d.ts.map +1 -0
- package/dist/plugins/broadcastUpdate/responsesAreSame.d.ts +11 -0
- package/dist/plugins/broadcastUpdate/responsesAreSame.d.ts.map +1 -0
- package/dist/plugins/broadcastUpdate/types.d.ts +34 -0
- package/dist/plugins/broadcastUpdate/types.d.ts.map +1 -0
- package/dist/plugins/cacheableResponse/CacheableResponse.d.ts +40 -0
- package/dist/plugins/cacheableResponse/CacheableResponse.d.ts.map +1 -0
- package/dist/plugins/cacheableResponse/CacheableResponsePlugin.d.ts +27 -0
- package/dist/plugins/cacheableResponse/CacheableResponsePlugin.d.ts.map +1 -0
- package/dist/plugins/expiration/CacheExpiration.d.ts +66 -0
- package/dist/plugins/expiration/CacheExpiration.d.ts.map +1 -0
- package/dist/plugins/expiration/ExpirationPlugin.d.ts +116 -0
- package/dist/plugins/expiration/ExpirationPlugin.d.ts.map +1 -0
- package/dist/plugins/expiration/models/CacheTimestampsModel.d.ts +73 -0
- package/dist/plugins/expiration/models/CacheTimestampsModel.d.ts.map +1 -0
- package/dist/plugins/googleAnalytics/constants.d.ts +10 -0
- package/dist/plugins/googleAnalytics/constants.d.ts.map +1 -0
- package/dist/plugins/googleAnalytics/initialize.d.ts +26 -0
- package/dist/plugins/googleAnalytics/initialize.d.ts.map +1 -0
- package/dist/plugins/precaching/PrecacheFallbackPlugin.d.ts +54 -0
- package/dist/plugins/precaching/PrecacheFallbackPlugin.d.ts.map +1 -0
- package/dist/plugins/rangeRequests/RangeRequestsPlugin.d.ts +19 -0
- package/dist/plugins/rangeRequests/RangeRequestsPlugin.d.ts.map +1 -0
- package/dist/plugins/rangeRequests/createPartialResponse.d.ts +18 -0
- package/dist/plugins/rangeRequests/createPartialResponse.d.ts.map +1 -0
- package/dist/plugins/rangeRequests/utils/calculateEffectiveBoundaries.d.ts +14 -0
- package/dist/plugins/rangeRequests/utils/calculateEffectiveBoundaries.d.ts.map +1 -0
- package/dist/plugins/rangeRequests/utils/parseRangeHeader.d.ts +12 -0
- package/dist/plugins/rangeRequests/utils/parseRangeHeader.d.ts.map +1 -0
- package/dist/precaching/PrecacheController.d.ts +145 -0
- package/dist/precaching/PrecacheController.d.ts.map +1 -0
- package/dist/precaching/PrecacheRoute.d.ts +20 -0
- package/dist/precaching/PrecacheRoute.d.ts.map +1 -0
- package/dist/precaching/PrecacheStrategy.d.ts +68 -0
- package/dist/precaching/PrecacheStrategy.d.ts.map +1 -0
- package/dist/precaching/addPlugins.d.ts +8 -0
- package/dist/precaching/addPlugins.d.ts.map +1 -0
- package/dist/precaching/addRoute.d.ts +15 -0
- package/dist/precaching/addRoute.d.ts.map +1 -0
- package/dist/precaching/cleanupOutdatedCaches.d.ts +6 -0
- package/dist/precaching/cleanupOutdatedCaches.d.ts.map +1 -0
- package/dist/precaching/createHandlerBoundToURL.d.ts +17 -0
- package/dist/precaching/createHandlerBoundToURL.d.ts.map +1 -0
- package/dist/precaching/getCacheKeyForURL.d.ts +20 -0
- package/dist/precaching/getCacheKeyForURL.d.ts.map +1 -0
- package/dist/precaching/matchPrecache.d.ts +14 -0
- package/dist/precaching/matchPrecache.d.ts.map +1 -0
- package/dist/precaching/precache.d.ts +19 -0
- package/dist/precaching/precache.d.ts.map +1 -0
- package/dist/precaching/precacheAndRoute.d.ts +14 -0
- package/dist/precaching/precacheAndRoute.d.ts.map +1 -0
- package/dist/precaching/types.d.ts +37 -0
- package/dist/precaching/types.d.ts.map +1 -0
- package/dist/precaching/utils/PrecacheCacheKeyPlugin.d.ts +17 -0
- package/dist/precaching/utils/PrecacheCacheKeyPlugin.d.ts.map +1 -0
- package/dist/precaching/utils/PrecacheInstallReportPlugin.d.ts +15 -0
- package/dist/precaching/utils/PrecacheInstallReportPlugin.d.ts.map +1 -0
- package/dist/precaching/utils/createCacheKey.d.ts +16 -0
- package/dist/precaching/utils/createCacheKey.d.ts.map +1 -0
- package/dist/precaching/utils/deleteOutdatedCaches.d.ts +18 -0
- package/dist/precaching/utils/deleteOutdatedCaches.d.ts.map +1 -0
- package/dist/precaching/utils/generateURLVariations.d.ts +12 -0
- package/dist/precaching/utils/generateURLVariations.d.ts.map +1 -0
- package/dist/precaching/utils/getCacheKeyForURL.d.ts +14 -0
- package/dist/precaching/utils/getCacheKeyForURL.d.ts.map +1 -0
- package/dist/precaching/utils/getOrCreatePrecacheController.d.ts +7 -0
- package/dist/precaching/utils/getOrCreatePrecacheController.d.ts.map +1 -0
- package/dist/precaching/utils/printCleanupDetails.d.ts +6 -0
- package/dist/precaching/utils/printCleanupDetails.d.ts.map +1 -0
- package/dist/precaching/utils/printInstallDetails.d.ts +7 -0
- package/dist/precaching/utils/printInstallDetails.d.ts.map +1 -0
- package/dist/precaching/utils/removeIgnoredSearchParams.d.ts +12 -0
- package/dist/precaching/utils/removeIgnoredSearchParams.d.ts.map +1 -0
- package/dist/routing/NavigationRoute.d.ts +57 -0
- package/dist/routing/NavigationRoute.d.ts.map +1 -0
- package/dist/routing/RegExpRoute.d.ts +24 -0
- package/dist/routing/RegExpRoute.d.ts.map +1 -0
- package/dist/routing/Route.d.ts +33 -0
- package/dist/routing/Route.d.ts.map +1 -0
- package/dist/routing/Router.d.ts +124 -0
- package/dist/routing/Router.d.ts.map +1 -0
- package/dist/routing/registerRoute.d.ts +15 -0
- package/dist/routing/registerRoute.d.ts.map +1 -0
- package/dist/routing/setCatchHandler.d.ts +9 -0
- package/dist/routing/setCatchHandler.d.ts.map +1 -0
- package/dist/routing/setDefaultHandler.d.ts +12 -0
- package/dist/routing/setDefaultHandler.d.ts.map +1 -0
- package/dist/routing/unregisterRoute.d.ts +8 -0
- package/dist/routing/unregisterRoute.d.ts.map +1 -0
- package/dist/routing/utils/constants.d.ts +15 -0
- package/dist/routing/utils/constants.d.ts.map +1 -0
- package/dist/routing/utils/getOrCreateDefaultRouter.d.ts +10 -0
- package/dist/routing/utils/getOrCreateDefaultRouter.d.ts.map +1 -0
- package/dist/routing/utils/normalizeHandler.d.ts +10 -0
- package/dist/routing/utils/normalizeHandler.d.ts.map +1 -0
- package/dist/routing/utils/parseRoute.d.ts +5 -0
- package/dist/routing/utils/parseRoute.d.ts.map +1 -0
- package/dist/strategies/CacheFirst.d.ts +23 -0
- package/dist/strategies/CacheFirst.d.ts.map +1 -0
- package/dist/strategies/CacheOnly.d.ts +20 -0
- package/dist/strategies/CacheOnly.d.ts.map +1 -0
- package/dist/strategies/NetworkFirst.d.ts +61 -0
- package/dist/strategies/NetworkFirst.d.ts.map +1 -0
- package/dist/strategies/NetworkOnly.d.ts +32 -0
- package/dist/strategies/NetworkOnly.d.ts.map +1 -0
- package/dist/strategies/StaleWhileRevalidate.d.ts +35 -0
- package/dist/strategies/StaleWhileRevalidate.d.ts.map +1 -0
- package/dist/strategies/Strategy.d.ts +83 -0
- package/dist/strategies/Strategy.d.ts.map +1 -0
- package/dist/strategies/StrategyHandler.d.ts +189 -0
- package/dist/strategies/StrategyHandler.d.ts.map +1 -0
- package/dist/strategies/plugins/cacheOkAndOpaquePlugin.d.ts +3 -0
- package/dist/strategies/plugins/cacheOkAndOpaquePlugin.d.ts.map +1 -0
- package/dist/strategies/utils/messages.d.ts +5 -0
- package/dist/strategies/utils/messages.d.ts.map +1 -0
- package/package.json +20 -14
- package/src/{Serwist.ts → abstractions/Serwist.ts} +16 -19
- package/src/{fallbacks.ts → abstractions/fallbacks.ts} +5 -5
- package/src/{handlePrecaching.ts → abstractions/handlePrecaching.ts} +6 -3
- package/src/{installSerwist.ts → abstractions/installSerwist.ts} +2 -2
- package/src/{registerRuntimeCaching.ts → abstractions/registerRuntimeCaching.ts} +1 -1
- package/src/{types.ts → abstractions/types.ts} +1 -1
- package/src/index.plugins.ts +65 -16
- package/src/index.precaching.ts +38 -0
- package/src/index.routing.ts +14 -0
- package/src/index.strategies.ts +22 -2
- package/src/index.ts +8 -8
- package/src/plugins/backgroundSync/BackgroundSyncPlugin.ts +39 -0
- package/src/plugins/backgroundSync/Queue.ts +438 -0
- package/src/plugins/backgroundSync/QueueDb.ts +176 -0
- package/src/plugins/backgroundSync/QueueStore.ts +161 -0
- package/src/plugins/backgroundSync/StorableRequest.ts +142 -0
- package/src/plugins/broadcastUpdate/BroadcastCacheUpdate.ts +159 -0
- package/src/plugins/broadcastUpdate/BroadcastUpdatePlugin.ts +43 -0
- package/src/plugins/broadcastUpdate/constants.ts +12 -0
- package/src/plugins/broadcastUpdate/responsesAreSame.ts +48 -0
- package/src/plugins/broadcastUpdate/types.ts +37 -0
- package/src/plugins/cacheableResponse/CacheableResponse.ts +141 -0
- package/src/plugins/cacheableResponse/CacheableResponsePlugin.ts +46 -0
- package/src/plugins/expiration/CacheExpiration.ts +192 -0
- package/src/plugins/expiration/ExpirationPlugin.ts +297 -0
- package/src/plugins/expiration/models/CacheTimestampsModel.ts +184 -0
- package/src/plugins/googleAnalytics/constants.ts +22 -0
- package/src/plugins/googleAnalytics/initialize.ts +200 -0
- package/src/plugins/precaching/PrecacheFallbackPlugin.ts +86 -0
- package/src/plugins/rangeRequests/RangeRequestsPlugin.ts +39 -0
- package/src/plugins/rangeRequests/createPartialResponse.ts +92 -0
- package/src/plugins/rangeRequests/utils/calculateEffectiveBoundaries.ts +58 -0
- package/src/plugins/rangeRequests/utils/parseRangeHeader.ts +54 -0
- package/src/precaching/PrecacheController.ts +332 -0
- package/src/precaching/PrecacheRoute.ts +50 -0
- package/src/precaching/PrecacheStrategy.ts +238 -0
- package/src/precaching/addPlugins.ts +21 -0
- package/src/precaching/addRoute.ts +30 -0
- package/src/precaching/cleanupOutdatedCaches.ts +33 -0
- package/src/precaching/createHandlerBoundToURL.ts +30 -0
- package/src/precaching/getCacheKeyForURL.ts +33 -0
- package/src/precaching/matchPrecache.ts +26 -0
- package/src/precaching/precache.ts +31 -0
- package/src/precaching/precacheAndRoute.ts +27 -0
- package/src/precaching/types.ts +46 -0
- package/src/precaching/utils/PrecacheCacheKeyPlugin.ts +36 -0
- package/src/precaching/utils/PrecacheInstallReportPlugin.ts +49 -0
- package/src/precaching/utils/createCacheKey.ts +68 -0
- package/src/precaching/utils/deleteOutdatedCaches.ts +40 -0
- package/src/precaching/utils/generateURLVariations.ts +55 -0
- package/src/precaching/utils/getCacheKeyForURL.ts +36 -0
- package/src/precaching/utils/getOrCreatePrecacheController.ts +22 -0
- package/src/precaching/utils/printCleanupDetails.ts +38 -0
- package/src/precaching/utils/printInstallDetails.ts +53 -0
- package/src/precaching/utils/removeIgnoredSearchParams.ts +29 -0
- package/src/routing/NavigationRoute.ts +119 -0
- package/src/routing/RegExpRoute.ts +74 -0
- package/src/routing/Route.ts +68 -0
- package/src/routing/Router.ts +432 -0
- package/src/routing/registerRoute.ts +33 -0
- package/src/routing/setCatchHandler.ts +22 -0
- package/src/routing/setDefaultHandler.ts +25 -0
- package/src/routing/unregisterRoute.ts +12 -0
- package/src/routing/utils/constants.ts +24 -0
- package/src/routing/utils/getOrCreateDefaultRouter.ts +29 -0
- package/src/routing/utils/normalizeHandler.ts +40 -0
- package/src/routing/utils/parseRoute.ts +67 -0
- package/src/strategies/CacheFirst.ts +87 -0
- package/src/strategies/CacheOnly.ts +58 -0
- package/src/strategies/NetworkFirst.ts +228 -0
- package/src/strategies/NetworkOnly.ts +96 -0
- package/src/strategies/StaleWhileRevalidate.ts +109 -0
- package/src/strategies/Strategy.ts +202 -0
- package/src/strategies/StrategyHandler.ts +557 -0
- package/src/strategies/plugins/cacheOkAndOpaquePlugin.ts +26 -0
- package/src/strategies/utils/messages.ts +20 -0
- package/dist/Serwist.d.ts.map +0 -1
- package/dist/disableDevLogs.d.ts.map +0 -1
- package/dist/fallbacks.d.ts.map +0 -1
- package/dist/handlePrecaching.d.ts.map +0 -1
- package/dist/installSerwist.d.ts.map +0 -1
- package/dist/registerRuntimeCaching.d.ts.map +0 -1
- package/dist/types.d.ts.map +0 -1
- /package/dist/{disableDevLogs.d.ts → abstractions/disableDevLogs.d.ts} +0 -0
- /package/dist/{installSerwist.d.ts → abstractions/installSerwist.d.ts} +0 -0
- /package/dist/{registerRuntimeCaching.d.ts → abstractions/registerRuntimeCaching.d.ts} +0 -0
- /package/src/{disableDevLogs.ts → abstractions/disableDevLogs.ts} +0 -0
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { CacheDidUpdateCallbackParam, CachedResponseWillBeUsedCallbackParam, SerwistPlugin } from "@serwist/core";
|
|
10
|
+
import { registerQuotaErrorCallback } from "@serwist/core";
|
|
11
|
+
import { assert, SerwistError, getFriendlyURL, logger, privateCacheNames } from "@serwist/core/internal";
|
|
12
|
+
|
|
13
|
+
import { CacheExpiration } from "./CacheExpiration.js";
|
|
14
|
+
|
|
15
|
+
export interface ExpirationPluginOptions {
|
|
16
|
+
/**
|
|
17
|
+
* The maximum number of entries to cache. Entries used the least will be removed
|
|
18
|
+
* as the maximum is reached.
|
|
19
|
+
*/
|
|
20
|
+
maxEntries?: number;
|
|
21
|
+
/**
|
|
22
|
+
* The maximum number of seconds before an entry is treated as stale and removed.
|
|
23
|
+
*/
|
|
24
|
+
maxAgeSeconds?: number;
|
|
25
|
+
/**
|
|
26
|
+
* Determines whether `maxAgeSeconds` should be calculated from when an
|
|
27
|
+
* entry was last fetched or when it was last used.
|
|
28
|
+
*
|
|
29
|
+
* @default "last-fetched"
|
|
30
|
+
*/
|
|
31
|
+
maxAgeFrom?: "last-fetched" | "last-used";
|
|
32
|
+
/**
|
|
33
|
+
* The [`CacheQueryOptions`](https://developer.mozilla.org/en-US/docs/Web/API/Cache/delete#Parameters)
|
|
34
|
+
* that will be used when calling `delete()` on the cache.
|
|
35
|
+
*/
|
|
36
|
+
matchOptions?: CacheQueryOptions;
|
|
37
|
+
/**
|
|
38
|
+
* Whether to opt this cache into automatic deletion if the available storage quota has been exceeded.
|
|
39
|
+
*/
|
|
40
|
+
purgeOnQuotaError?: boolean;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* This plugin can be used in a `@serwist/strategies` Strategy to regularly enforce a
|
|
45
|
+
* limit on the age and/or the number of cached requests.
|
|
46
|
+
*
|
|
47
|
+
* It can only be used with Strategy instances that have a custom `cacheName` property set.
|
|
48
|
+
* In other words, it can't be used to expire entries in strategies that use the default runtime
|
|
49
|
+
* cache name.
|
|
50
|
+
*
|
|
51
|
+
* Whenever a cached response is used or updated, this plugin will look
|
|
52
|
+
* at the associated cache and remove any old or extra responses.
|
|
53
|
+
*
|
|
54
|
+
* When using `maxAgeSeconds`, responses may be used *once* after expiring
|
|
55
|
+
* because the expiration clean up will not have occurred until *after* the
|
|
56
|
+
* cached response has been used. If the response has a "Date" header, then a lightweight expiration
|
|
57
|
+
* check is performed, and the response will not be used immediately.
|
|
58
|
+
*
|
|
59
|
+
* When using `maxEntries`, the least recently requested entry will be removed
|
|
60
|
+
* from the cache.
|
|
61
|
+
*
|
|
62
|
+
* @see https://serwist.pages.dev/docs/expiration/expiration-plugin
|
|
63
|
+
*/
|
|
64
|
+
export class ExpirationPlugin implements SerwistPlugin {
|
|
65
|
+
private readonly _config: ExpirationPluginOptions;
|
|
66
|
+
private _cacheExpirations: Map<string, CacheExpiration>;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* @param config
|
|
70
|
+
*/
|
|
71
|
+
constructor(config: ExpirationPluginOptions = {}) {
|
|
72
|
+
if (process.env.NODE_ENV !== "production") {
|
|
73
|
+
if (!(config.maxEntries || config.maxAgeSeconds)) {
|
|
74
|
+
throw new SerwistError("max-entries-or-age-required", {
|
|
75
|
+
moduleName: "@serwist/expiration",
|
|
76
|
+
className: "ExpirationPlugin",
|
|
77
|
+
funcName: "constructor",
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (config.maxEntries) {
|
|
82
|
+
assert!.isType(config.maxEntries, "number", {
|
|
83
|
+
moduleName: "@serwist/expiration",
|
|
84
|
+
className: "ExpirationPlugin",
|
|
85
|
+
funcName: "constructor",
|
|
86
|
+
paramName: "config.maxEntries",
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (config.maxAgeSeconds) {
|
|
91
|
+
assert!.isType(config.maxAgeSeconds, "number", {
|
|
92
|
+
moduleName: "@serwist/expiration",
|
|
93
|
+
className: "ExpirationPlugin",
|
|
94
|
+
funcName: "constructor",
|
|
95
|
+
paramName: "config.maxAgeSeconds",
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (config.maxAgeFrom) {
|
|
100
|
+
assert!.isType(config.maxAgeFrom, "string", {
|
|
101
|
+
moduleName: "@serwist/expiration",
|
|
102
|
+
className: "ExpirationPlugin",
|
|
103
|
+
funcName: "constructor",
|
|
104
|
+
paramName: "config.maxAgeFrom",
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
this._config = config;
|
|
110
|
+
this._cacheExpirations = new Map();
|
|
111
|
+
|
|
112
|
+
if (!this._config.maxAgeFrom) {
|
|
113
|
+
this._config.maxAgeFrom = "last-fetched";
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (this._config.purgeOnQuotaError) {
|
|
117
|
+
registerQuotaErrorCallback(() => this.deleteCacheAndMetadata());
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* A simple helper method to return a CacheExpiration instance for a given
|
|
123
|
+
* cache name.
|
|
124
|
+
*
|
|
125
|
+
* @param cacheName
|
|
126
|
+
* @returns
|
|
127
|
+
* @private
|
|
128
|
+
*/
|
|
129
|
+
private _getCacheExpiration(cacheName: string): CacheExpiration {
|
|
130
|
+
if (cacheName === privateCacheNames.getRuntimeName()) {
|
|
131
|
+
throw new SerwistError("expire-custom-caches-only");
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
let cacheExpiration = this._cacheExpirations.get(cacheName);
|
|
135
|
+
if (!cacheExpiration) {
|
|
136
|
+
cacheExpiration = new CacheExpiration(cacheName, this._config);
|
|
137
|
+
this._cacheExpirations.set(cacheName, cacheExpiration);
|
|
138
|
+
}
|
|
139
|
+
return cacheExpiration;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* A "lifecycle" callback that will be triggered automatically by the
|
|
144
|
+
* `@serwist/strategies` handlers when a `Response` is about to be returned
|
|
145
|
+
* from a [Cache](https://developer.mozilla.org/en-US/docs/Web/API/Cache) to
|
|
146
|
+
* the handler. It allows the `Response` to be inspected for freshness and
|
|
147
|
+
* prevents it from being used if the `Response`'s `Date` header value is
|
|
148
|
+
* older than the configured `maxAgeSeconds`.
|
|
149
|
+
*
|
|
150
|
+
* @param options
|
|
151
|
+
* @returns `cachedResponse` if it is fresh and `null` if it is stale or
|
|
152
|
+
* not available.
|
|
153
|
+
* @private
|
|
154
|
+
*/
|
|
155
|
+
cachedResponseWillBeUsed({ event, cacheName, request, cachedResponse }: CachedResponseWillBeUsedCallbackParam) {
|
|
156
|
+
if (!cachedResponse) {
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const isFresh = this._isResponseDateFresh(cachedResponse);
|
|
161
|
+
|
|
162
|
+
// Expire entries to ensure that even if the expiration date has
|
|
163
|
+
// expired, it'll only be used once.
|
|
164
|
+
const cacheExpiration = this._getCacheExpiration(cacheName);
|
|
165
|
+
|
|
166
|
+
const isMaxAgeFromLastUsed = this._config.maxAgeFrom === "last-used";
|
|
167
|
+
|
|
168
|
+
const done = (async () => {
|
|
169
|
+
// Update the metadata for the request URL to the current timestamp.
|
|
170
|
+
// Only applies if `maxAgeFrom` is `"last-used"`, since the current
|
|
171
|
+
// lifecycle callback is `cachedResponseWillBeUsed`.
|
|
172
|
+
// This needs to be called before `expireEntries()` so as to avoid
|
|
173
|
+
// this URL being marked as expired.
|
|
174
|
+
if (isMaxAgeFromLastUsed) {
|
|
175
|
+
await cacheExpiration.updateTimestamp(request.url);
|
|
176
|
+
}
|
|
177
|
+
await cacheExpiration.expireEntries();
|
|
178
|
+
})();
|
|
179
|
+
try {
|
|
180
|
+
event.waitUntil(done);
|
|
181
|
+
} catch (error) {
|
|
182
|
+
if (process.env.NODE_ENV !== "production") {
|
|
183
|
+
if (event instanceof FetchEvent) {
|
|
184
|
+
logger.warn(`Unable to ensure service worker stays alive when updating cache entry for '${getFriendlyURL(event.request.url)}'.`);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return isFresh ? cachedResponse : null;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* @param cachedResponse
|
|
194
|
+
* @returns
|
|
195
|
+
* @private
|
|
196
|
+
*/
|
|
197
|
+
private _isResponseDateFresh(cachedResponse: Response): boolean {
|
|
198
|
+
const isMaxAgeFromLastUsed = this._config.maxAgeFrom === "last-used";
|
|
199
|
+
// If `maxAgeFrom` is `"last-used"`, the `Date` header doesn't really
|
|
200
|
+
// matter since it is about when the response was created.
|
|
201
|
+
if (isMaxAgeFromLastUsed) {
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
204
|
+
const now = Date.now();
|
|
205
|
+
if (!this._config.maxAgeSeconds) {
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
// Check if the `Date` header will suffice a quick expiration check.
|
|
209
|
+
// See https://github.com/GoogleChromeLabs/sw-toolbox/issues/164 for
|
|
210
|
+
// discussion.
|
|
211
|
+
const dateHeaderTimestamp = this._getDateHeaderTimestamp(cachedResponse);
|
|
212
|
+
if (dateHeaderTimestamp === null) {
|
|
213
|
+
// Unable to parse date, so assume it's fresh.
|
|
214
|
+
return true;
|
|
215
|
+
}
|
|
216
|
+
// If we have a valid headerTime, then our response is fresh if the
|
|
217
|
+
// headerTime plus maxAgeSeconds is greater than the current time.
|
|
218
|
+
return dateHeaderTimestamp >= now - this._config.maxAgeSeconds * 1000;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Extracts the `Date` header and parse it into an useful value.
|
|
223
|
+
*
|
|
224
|
+
* @param cachedResponse
|
|
225
|
+
* @returns
|
|
226
|
+
* @private
|
|
227
|
+
*/
|
|
228
|
+
private _getDateHeaderTimestamp(cachedResponse: Response): number | null {
|
|
229
|
+
if (!cachedResponse.headers.has("date")) {
|
|
230
|
+
return null;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const dateHeader = cachedResponse.headers.get("date")!;
|
|
234
|
+
const parsedDate = new Date(dateHeader);
|
|
235
|
+
const headerTime = parsedDate.getTime();
|
|
236
|
+
|
|
237
|
+
// If the `Date` header is invalid for some reason, `parsedDate.getTime()`
|
|
238
|
+
// will return NaN.
|
|
239
|
+
if (Number.isNaN(headerTime)) {
|
|
240
|
+
return null;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return headerTime;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* A "lifecycle" callback that will be triggered automatically by the
|
|
248
|
+
* `@serwist/strategies` handlers when an entry is added to a cache.
|
|
249
|
+
*
|
|
250
|
+
* @param options
|
|
251
|
+
* @private
|
|
252
|
+
*/
|
|
253
|
+
async cacheDidUpdate({ cacheName, request }: CacheDidUpdateCallbackParam) {
|
|
254
|
+
if (process.env.NODE_ENV !== "production") {
|
|
255
|
+
assert!.isType(cacheName, "string", {
|
|
256
|
+
moduleName: "@serwist/expiration",
|
|
257
|
+
className: "Plugin",
|
|
258
|
+
funcName: "cacheDidUpdate",
|
|
259
|
+
paramName: "cacheName",
|
|
260
|
+
});
|
|
261
|
+
assert!.isInstance(request, Request, {
|
|
262
|
+
moduleName: "@serwist/expiration",
|
|
263
|
+
className: "Plugin",
|
|
264
|
+
funcName: "cacheDidUpdate",
|
|
265
|
+
paramName: "request",
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const cacheExpiration = this._getCacheExpiration(cacheName);
|
|
270
|
+
await cacheExpiration.updateTimestamp(request.url);
|
|
271
|
+
await cacheExpiration.expireEntries();
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Deletes the underlying `Cache` instance associated with this instance and the metadata
|
|
276
|
+
* from IndexedDB used to keep track of expiration details for each `Cache` instance.
|
|
277
|
+
*
|
|
278
|
+
* When using cache expiration, calling this method is preferable to calling
|
|
279
|
+
* `caches.delete()` directly, since this will ensure that the IndexedDB
|
|
280
|
+
* metadata is also cleanly removed and that open IndexedDB instances are deleted.
|
|
281
|
+
*
|
|
282
|
+
* Note that if you're *not* using cache expiration for a given cache, calling
|
|
283
|
+
* `caches.delete()` and passing in the cache's name should be sufficient.
|
|
284
|
+
* There is no Serwist-specific method needed for cleanup in that case.
|
|
285
|
+
*/
|
|
286
|
+
async deleteCacheAndMetadata(): Promise<void> {
|
|
287
|
+
// Do this one at a time instead of all at once via `Promise.all()` to
|
|
288
|
+
// reduce the chance of inconsistency if a promise rejects.
|
|
289
|
+
for (const [cacheName, cacheExpiration] of this._cacheExpirations) {
|
|
290
|
+
await self.caches.delete(cacheName);
|
|
291
|
+
await cacheExpiration.delete();
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// Reset this._cacheExpirations to its initial state.
|
|
295
|
+
this._cacheExpirations = new Map();
|
|
296
|
+
}
|
|
297
|
+
}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { DBSchema, IDBPDatabase } from "idb";
|
|
10
|
+
import { deleteDB, openDB } from "idb";
|
|
11
|
+
|
|
12
|
+
const DB_NAME = "serwist-expiration";
|
|
13
|
+
const CACHE_OBJECT_STORE = "cache-entries";
|
|
14
|
+
|
|
15
|
+
const normalizeURL = (unNormalizedUrl: string) => {
|
|
16
|
+
const url = new URL(unNormalizedUrl, location.href);
|
|
17
|
+
url.hash = "";
|
|
18
|
+
|
|
19
|
+
return url.href;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
interface CacheTimestampsModelEntry {
|
|
23
|
+
id: string;
|
|
24
|
+
cacheName: string;
|
|
25
|
+
url: string;
|
|
26
|
+
timestamp: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface CacheDbSchema extends DBSchema {
|
|
30
|
+
"cache-entries": {
|
|
31
|
+
key: string;
|
|
32
|
+
value: CacheTimestampsModelEntry;
|
|
33
|
+
indexes: { cacheName: string; timestamp: number };
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Returns the timestamp model.
|
|
39
|
+
*
|
|
40
|
+
* @private
|
|
41
|
+
*/
|
|
42
|
+
export class CacheTimestampsModel {
|
|
43
|
+
private readonly _cacheName: string;
|
|
44
|
+
private _db: IDBPDatabase<CacheDbSchema> | null = null;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
*
|
|
48
|
+
* @param cacheName
|
|
49
|
+
*
|
|
50
|
+
* @private
|
|
51
|
+
*/
|
|
52
|
+
constructor(cacheName: string) {
|
|
53
|
+
this._cacheName = cacheName;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Takes a URL and returns an ID that will be unique in the object store.
|
|
58
|
+
*
|
|
59
|
+
* @param url
|
|
60
|
+
* @returns
|
|
61
|
+
* @private
|
|
62
|
+
*/
|
|
63
|
+
private _getId(url: string): string {
|
|
64
|
+
return `${this._cacheName}|${normalizeURL(url)}`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Performs an upgrade of indexedDB.
|
|
69
|
+
*
|
|
70
|
+
* @param db
|
|
71
|
+
*
|
|
72
|
+
* @private
|
|
73
|
+
*/
|
|
74
|
+
private _upgradeDb(db: IDBPDatabase<CacheDbSchema>) {
|
|
75
|
+
const objStore = db.createObjectStore(CACHE_OBJECT_STORE, {
|
|
76
|
+
keyPath: "id",
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
// TODO(philipwalton): once we don't have to support EdgeHTML, we can
|
|
80
|
+
// create a single index with the keyPath `['cacheName', 'timestamp']`
|
|
81
|
+
// instead of doing both these indexes.
|
|
82
|
+
objStore.createIndex("cacheName", "cacheName", { unique: false });
|
|
83
|
+
objStore.createIndex("timestamp", "timestamp", { unique: false });
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Performs an upgrade of indexedDB and deletes deprecated DBs.
|
|
88
|
+
*
|
|
89
|
+
* @param db
|
|
90
|
+
*
|
|
91
|
+
* @private
|
|
92
|
+
*/
|
|
93
|
+
private _upgradeDbAndDeleteOldDbs(db: IDBPDatabase<CacheDbSchema>) {
|
|
94
|
+
this._upgradeDb(db);
|
|
95
|
+
if (this._cacheName) {
|
|
96
|
+
void deleteDB(this._cacheName);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* @param url
|
|
102
|
+
* @param timestamp
|
|
103
|
+
*
|
|
104
|
+
* @private
|
|
105
|
+
*/
|
|
106
|
+
async setTimestamp(url: string, timestamp: number): Promise<void> {
|
|
107
|
+
url = normalizeURL(url);
|
|
108
|
+
|
|
109
|
+
const entry = {
|
|
110
|
+
id: this._getId(url),
|
|
111
|
+
cacheName: this._cacheName,
|
|
112
|
+
url,
|
|
113
|
+
timestamp,
|
|
114
|
+
} satisfies CacheTimestampsModelEntry;
|
|
115
|
+
const db = await this.getDb();
|
|
116
|
+
const tx = db.transaction(CACHE_OBJECT_STORE, "readwrite", {
|
|
117
|
+
durability: "relaxed",
|
|
118
|
+
});
|
|
119
|
+
await tx.store.put(entry);
|
|
120
|
+
await tx.done;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Returns the timestamp stored for a given URL.
|
|
125
|
+
*
|
|
126
|
+
* @param url
|
|
127
|
+
* @returns
|
|
128
|
+
* @private
|
|
129
|
+
*/
|
|
130
|
+
async getTimestamp(url: string): Promise<number | undefined> {
|
|
131
|
+
const db = await this.getDb();
|
|
132
|
+
const entry = await db.get(CACHE_OBJECT_STORE, this._getId(url));
|
|
133
|
+
return entry?.timestamp;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Iterates through all the entries in the object store (from newest to
|
|
138
|
+
* oldest) and removes entries once either `maxCount` is reached or the
|
|
139
|
+
* entry's timestamp is less than `minTimestamp`.
|
|
140
|
+
*
|
|
141
|
+
* @param minTimestamp
|
|
142
|
+
* @param maxCount
|
|
143
|
+
* @returns
|
|
144
|
+
* @private
|
|
145
|
+
*/
|
|
146
|
+
async expireEntries(minTimestamp: number, maxCount?: number): Promise<string[]> {
|
|
147
|
+
const db = await this.getDb();
|
|
148
|
+
let cursor = await db.transaction(CACHE_OBJECT_STORE, "readwrite").store.index("timestamp").openCursor(null, "prev");
|
|
149
|
+
const urlsDeleted: string[] = [];
|
|
150
|
+
let entriesNotDeletedCount = 0;
|
|
151
|
+
while (cursor) {
|
|
152
|
+
const result = cursor.value;
|
|
153
|
+
// TODO(philipwalton): once we can use a multi-key index, we
|
|
154
|
+
// won't have to check `cacheName` here.
|
|
155
|
+
if (result.cacheName === this._cacheName) {
|
|
156
|
+
// Delete an entry if it's older than the max age or
|
|
157
|
+
// if we already have the max number allowed.
|
|
158
|
+
if ((minTimestamp && result.timestamp < minTimestamp) || (maxCount && entriesNotDeletedCount >= maxCount)) {
|
|
159
|
+
cursor.delete();
|
|
160
|
+
urlsDeleted.push(result.url);
|
|
161
|
+
} else {
|
|
162
|
+
entriesNotDeletedCount++;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
cursor = await cursor.continue();
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return urlsDeleted;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Returns an open connection to the database.
|
|
173
|
+
*
|
|
174
|
+
* @private
|
|
175
|
+
*/
|
|
176
|
+
private async getDb() {
|
|
177
|
+
if (!this._db) {
|
|
178
|
+
this._db = await openDB(DB_NAME, 1, {
|
|
179
|
+
upgrade: this._upgradeDbAndDeleteOldDbs.bind(this),
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
return this._db;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export const QUEUE_NAME = "serwist-google-analytics";
|
|
10
|
+
export const MAX_RETENTION_TIME = 60 * 48; // Two days in minutes
|
|
11
|
+
export const GOOGLE_ANALYTICS_HOST = "www.google-analytics.com";
|
|
12
|
+
export const GTM_HOST = "www.googletagmanager.com";
|
|
13
|
+
export const ANALYTICS_JS_PATH = "/analytics.js";
|
|
14
|
+
export const GTAG_JS_PATH = "/gtag/js";
|
|
15
|
+
export const GTM_JS_PATH = "/gtm.js";
|
|
16
|
+
export const COLLECT_DEFAULT_PATH = "/collect";
|
|
17
|
+
|
|
18
|
+
// This RegExp matches all known Measurement Protocol single-hit collect
|
|
19
|
+
// endpoints. Most of the time the default path (/collect) is used, but
|
|
20
|
+
// occasionally an experimental endpoint is used when testing new features,
|
|
21
|
+
// (e.g. /r/collect or /j/collect)
|
|
22
|
+
export const COLLECT_PATHS_REGEX = /^\/(\w+\/)?collect/;
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { RouteMatchCallbackOptions } from "@serwist/core";
|
|
10
|
+
import { getFriendlyURL, logger, privateCacheNames } from "@serwist/core/internal";
|
|
11
|
+
import { Route } from "../../routing/Route.js";
|
|
12
|
+
import { registerRoute } from "../../routing/registerRoute.js";
|
|
13
|
+
import { NetworkFirst } from "../../strategies/NetworkFirst.js";
|
|
14
|
+
import { NetworkOnly } from "../../strategies/NetworkOnly.js";
|
|
15
|
+
import { BackgroundSyncPlugin } from "../backgroundSync/BackgroundSyncPlugin.js";
|
|
16
|
+
import type { Queue, QueueEntry } from "../backgroundSync/Queue.js";
|
|
17
|
+
|
|
18
|
+
import {
|
|
19
|
+
ANALYTICS_JS_PATH,
|
|
20
|
+
COLLECT_PATHS_REGEX,
|
|
21
|
+
GOOGLE_ANALYTICS_HOST,
|
|
22
|
+
GTAG_JS_PATH,
|
|
23
|
+
GTM_HOST,
|
|
24
|
+
GTM_JS_PATH,
|
|
25
|
+
MAX_RETENTION_TIME,
|
|
26
|
+
QUEUE_NAME,
|
|
27
|
+
} from "./constants.js";
|
|
28
|
+
|
|
29
|
+
export interface GoogleAnalyticsInitializeOptions {
|
|
30
|
+
/**
|
|
31
|
+
* The cache name to store and retrieve analytics.js. Defaults to the cache names provided by `@serwist/core`.
|
|
32
|
+
*/
|
|
33
|
+
cacheName?: string;
|
|
34
|
+
/**
|
|
35
|
+
* [Measurement Protocol parameters](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters),
|
|
36
|
+
* expressed as key/value pairs, to be added to replayed Google Analytics
|
|
37
|
+
* requests. This can be used to, e.g., set a custom dimension indicating
|
|
38
|
+
* that the request was replayed.
|
|
39
|
+
*/
|
|
40
|
+
parameterOverrides?: { [paramName: string]: string };
|
|
41
|
+
/**
|
|
42
|
+
* A function that allows you to modify the hit parameters prior to replaying
|
|
43
|
+
* the hit. The function is invoked with the original hit's URLSearchParams
|
|
44
|
+
* object as its only argument.
|
|
45
|
+
*/
|
|
46
|
+
hitFilter?: (params: URLSearchParams) => void;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Creates the requestWillDequeue callback to be used with the background
|
|
51
|
+
* sync plugin. The callback takes the failed request and adds the
|
|
52
|
+
* `qt` param based on the current time, as well as applies any other
|
|
53
|
+
* user-defined hit modifications.
|
|
54
|
+
*
|
|
55
|
+
* @param config
|
|
56
|
+
* @returns The requestWillDequeue callback function.
|
|
57
|
+
* @private
|
|
58
|
+
*/
|
|
59
|
+
const createOnSyncCallback = (config: GoogleAnalyticsInitializeOptions) => {
|
|
60
|
+
return async ({ queue }: { queue: Queue }) => {
|
|
61
|
+
let entry: QueueEntry | undefined = undefined;
|
|
62
|
+
while ((entry = await queue.shiftRequest())) {
|
|
63
|
+
const { request, timestamp } = entry;
|
|
64
|
+
const url = new URL(request.url);
|
|
65
|
+
|
|
66
|
+
try {
|
|
67
|
+
// Measurement protocol requests can set their payload parameters in
|
|
68
|
+
// either the URL query string (for GET requests) or the POST body.
|
|
69
|
+
const params = request.method === "POST" ? new URLSearchParams(await request.clone().text()) : url.searchParams;
|
|
70
|
+
|
|
71
|
+
// Calculate the qt param, accounting for the fact that an existing
|
|
72
|
+
// qt param may be present and should be updated rather than replaced.
|
|
73
|
+
const originalHitTime = timestamp! - (Number(params.get("qt")) || 0);
|
|
74
|
+
const queueTime = Date.now() - originalHitTime;
|
|
75
|
+
|
|
76
|
+
// Set the qt param prior to applying hitFilter or parameterOverrides.
|
|
77
|
+
params.set("qt", String(queueTime));
|
|
78
|
+
|
|
79
|
+
// Apply `parameterOverrides`, if set.
|
|
80
|
+
if (config.parameterOverrides) {
|
|
81
|
+
for (const param of Object.keys(config.parameterOverrides)) {
|
|
82
|
+
const value = config.parameterOverrides[param];
|
|
83
|
+
params.set(param, value);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Apply `hitFilter`, if set.
|
|
88
|
+
if (typeof config.hitFilter === "function") {
|
|
89
|
+
config.hitFilter.call(null, params);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Retry the fetch. Ignore URL search params from the URL as they're
|
|
93
|
+
// now in the post body.
|
|
94
|
+
await fetch(
|
|
95
|
+
new Request(url.origin + url.pathname, {
|
|
96
|
+
body: params.toString(),
|
|
97
|
+
method: "POST",
|
|
98
|
+
mode: "cors",
|
|
99
|
+
credentials: "omit",
|
|
100
|
+
headers: { "Content-Type": "text/plain" },
|
|
101
|
+
}),
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
if (process.env.NODE_ENV !== "production") {
|
|
105
|
+
logger.log(`Request for '${getFriendlyURL(url.href)}' has been replayed`);
|
|
106
|
+
}
|
|
107
|
+
} catch (err) {
|
|
108
|
+
await queue.unshiftRequest(entry);
|
|
109
|
+
|
|
110
|
+
if (process.env.NODE_ENV !== "production") {
|
|
111
|
+
logger.log(`Request for '${getFriendlyURL(url.href)}' failed to replay, putting it back in the queue.`);
|
|
112
|
+
}
|
|
113
|
+
throw err;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
if (process.env.NODE_ENV !== "production") {
|
|
117
|
+
logger.log("All Google Analytics request successfully replayed; " + "the queue is now empty!");
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Creates GET and POST routes to catch failed Measurement Protocol hits.
|
|
124
|
+
*
|
|
125
|
+
* @param bgSyncPlugin
|
|
126
|
+
* @returns The created routes.
|
|
127
|
+
* @private
|
|
128
|
+
*/
|
|
129
|
+
const createCollectRoutes = (bgSyncPlugin: BackgroundSyncPlugin) => {
|
|
130
|
+
const match = ({ url }: RouteMatchCallbackOptions) => url.hostname === GOOGLE_ANALYTICS_HOST && COLLECT_PATHS_REGEX.test(url.pathname);
|
|
131
|
+
|
|
132
|
+
const handler = new NetworkOnly({
|
|
133
|
+
plugins: [bgSyncPlugin],
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
return [new Route(match, handler, "GET"), new Route(match, handler, "POST")];
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Creates a route with a network first strategy for the analytics.js script.
|
|
141
|
+
*
|
|
142
|
+
* @param cacheName
|
|
143
|
+
* @returns The created route.
|
|
144
|
+
* @private
|
|
145
|
+
*/
|
|
146
|
+
const createAnalyticsJsRoute = (cacheName: string) => {
|
|
147
|
+
const match = ({ url }: RouteMatchCallbackOptions) => url.hostname === GOOGLE_ANALYTICS_HOST && url.pathname === ANALYTICS_JS_PATH;
|
|
148
|
+
|
|
149
|
+
const handler = new NetworkFirst({ cacheName });
|
|
150
|
+
|
|
151
|
+
return new Route(match, handler, "GET");
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Creates a route with a network first strategy for the gtag.js script.
|
|
156
|
+
*
|
|
157
|
+
* @param cacheName
|
|
158
|
+
* @returns The created route.
|
|
159
|
+
* @private
|
|
160
|
+
*/
|
|
161
|
+
const createGtagJsRoute = (cacheName: string) => {
|
|
162
|
+
const match = ({ url }: RouteMatchCallbackOptions) => url.hostname === GTM_HOST && url.pathname === GTAG_JS_PATH;
|
|
163
|
+
|
|
164
|
+
const handler = new NetworkFirst({ cacheName });
|
|
165
|
+
|
|
166
|
+
return new Route(match, handler, "GET");
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Creates a route with a network first strategy for the gtm.js script.
|
|
171
|
+
*
|
|
172
|
+
* @param cacheName
|
|
173
|
+
* @returns The created route.
|
|
174
|
+
* @private
|
|
175
|
+
*/
|
|
176
|
+
const createGtmJsRoute = (cacheName: string) => {
|
|
177
|
+
const match = ({ url }: RouteMatchCallbackOptions) => url.hostname === GTM_HOST && url.pathname === GTM_JS_PATH;
|
|
178
|
+
|
|
179
|
+
const handler = new NetworkFirst({ cacheName });
|
|
180
|
+
|
|
181
|
+
return new Route(match, handler, "GET");
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* @param options
|
|
186
|
+
*/
|
|
187
|
+
export const initialize = (options: GoogleAnalyticsInitializeOptions = {}): void => {
|
|
188
|
+
const cacheName = privateCacheNames.getGoogleAnalyticsName(options.cacheName);
|
|
189
|
+
|
|
190
|
+
const bgSyncPlugin = new BackgroundSyncPlugin(QUEUE_NAME, {
|
|
191
|
+
maxRetentionTime: MAX_RETENTION_TIME,
|
|
192
|
+
onSync: createOnSyncCallback(options),
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
const routes = [createGtmJsRoute(cacheName), createAnalyticsJsRoute(cacheName), createGtagJsRoute(cacheName), ...createCollectRoutes(bgSyncPlugin)];
|
|
196
|
+
|
|
197
|
+
for (const route of routes) {
|
|
198
|
+
registerRoute(route);
|
|
199
|
+
}
|
|
200
|
+
};
|