serwist 9.0.0-preview.24 → 9.0.0-preview.26
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/NavigationRoute.d.ts +6 -7
- package/dist/NavigationRoute.d.ts.map +1 -1
- package/dist/PrecacheRoute.d.ts +2 -2
- package/dist/PrecacheRoute.d.ts.map +1 -1
- package/dist/RegExpRoute.d.ts +4 -4
- package/dist/RegExpRoute.d.ts.map +1 -1
- package/dist/Route.d.ts +10 -10
- package/dist/Route.d.ts.map +1 -1
- package/dist/Serwist.d.ts +39 -44
- package/dist/Serwist.d.ts.map +1 -1
- package/dist/chunks/printInstallDetails.js +1587 -0
- package/dist/chunks/resultingClientExists.js +1 -1
- package/dist/chunks/waitUntil.js +426 -1
- package/dist/copyResponse.d.ts +8 -10
- package/dist/copyResponse.d.ts.map +1 -1
- package/dist/disableDevLogs.d.ts +1 -1
- package/dist/index.d.ts +41 -12
- package/dist/index.d.ts.map +1 -1
- package/dist/index.internal.d.ts +3 -1
- package/dist/index.internal.d.ts.map +1 -1
- package/dist/index.internal.js +1 -2
- package/dist/index.js +1464 -8
- package/dist/index.legacy.d.ts +4 -8
- package/dist/index.legacy.d.ts.map +1 -1
- package/dist/index.legacy.js +166 -16
- package/dist/legacy/PrecacheController.d.ts +1 -1
- package/dist/legacy/PrecacheController.d.ts.map +1 -1
- package/dist/legacy/PrecacheRoute.d.ts.map +1 -1
- package/dist/legacy/Router.d.ts +1 -1
- package/dist/legacy/Router.d.ts.map +1 -1
- package/dist/legacy/constants.d.ts.map +1 -0
- package/dist/legacy/fallbacks.d.ts +1 -1
- package/dist/legacy/fallbacks.d.ts.map +1 -1
- package/dist/legacy/handlePrecaching.d.ts.map +1 -1
- package/dist/legacy/initializeGoogleAnalytics.d.ts +38 -0
- package/dist/legacy/initializeGoogleAnalytics.d.ts.map +1 -0
- package/dist/legacy/installSerwist.d.ts +69 -3
- package/dist/legacy/installSerwist.d.ts.map +1 -1
- package/dist/legacy/registerRoute.d.ts +1 -1
- package/dist/legacy/registerRoute.d.ts.map +1 -1
- package/dist/{plugins → lib}/backgroundSync/BackgroundSyncPlugin.d.ts +3 -3
- package/dist/lib/backgroundSync/BackgroundSyncPlugin.d.ts.map +1 -0
- package/dist/{plugins/backgroundSync/Queue.d.ts → lib/backgroundSync/BackgroundSyncQueue.d.ts} +29 -13
- package/dist/lib/backgroundSync/BackgroundSyncQueue.d.ts.map +1 -0
- package/dist/{plugins/backgroundSync/QueueDb.d.ts → lib/backgroundSync/BackgroundSyncQueueDb.d.ts} +7 -7
- package/dist/lib/backgroundSync/BackgroundSyncQueueDb.d.ts.map +1 -0
- package/dist/{plugins/backgroundSync/QueueStore.d.ts → lib/backgroundSync/BackgroundSyncQueueStore.d.ts} +7 -7
- package/dist/lib/backgroundSync/BackgroundSyncQueueStore.d.ts.map +1 -0
- package/dist/lib/backgroundSync/StorableRequest.d.ts.map +1 -0
- package/dist/lib/broadcastUpdate/BroadcastCacheUpdate.d.ts.map +1 -0
- package/dist/{plugins → lib}/broadcastUpdate/BroadcastUpdatePlugin.d.ts +1 -1
- package/dist/lib/broadcastUpdate/BroadcastUpdatePlugin.d.ts.map +1 -0
- package/dist/lib/broadcastUpdate/constants.d.ts +5 -0
- package/dist/lib/broadcastUpdate/constants.d.ts.map +1 -0
- package/dist/lib/broadcastUpdate/responsesAreSame.d.ts.map +1 -0
- package/dist/{plugins → lib}/broadcastUpdate/types.d.ts +3 -3
- package/dist/lib/broadcastUpdate/types.d.ts.map +1 -0
- package/dist/lib/cacheableResponse/CacheableResponse.d.ts.map +1 -0
- package/dist/lib/cacheableResponse/CacheableResponsePlugin.d.ts.map +1 -0
- package/dist/{plugins → lib}/expiration/CacheExpiration.d.ts +1 -1
- package/dist/lib/expiration/CacheExpiration.d.ts.map +1 -0
- package/dist/{plugins → lib}/expiration/ExpirationPlugin.d.ts +1 -1
- package/dist/lib/expiration/ExpirationPlugin.d.ts.map +1 -0
- package/dist/lib/expiration/models/CacheTimestampsModel.d.ts.map +1 -0
- package/dist/lib/googleAnalytics/constants.d.ts +10 -0
- package/dist/lib/googleAnalytics/constants.d.ts.map +1 -0
- package/dist/{plugins/googleAnalytics/initialize.d.ts → lib/googleAnalytics/initializeGoogleAnalytics.d.ts} +2 -2
- package/dist/lib/googleAnalytics/initializeGoogleAnalytics.d.ts.map +1 -0
- package/dist/{plugins → lib}/precaching/PrecacheFallbackPlugin.d.ts +1 -1
- package/dist/lib/precaching/PrecacheFallbackPlugin.d.ts.map +1 -0
- package/dist/lib/rangeRequests/RangeRequestsPlugin.d.ts.map +1 -0
- package/dist/lib/rangeRequests/createPartialResponse.d.ts.map +1 -0
- package/dist/lib/rangeRequests/utils/calculateEffectiveBoundaries.d.ts.map +1 -0
- package/dist/lib/rangeRequests/utils/parseRangeHeader.d.ts.map +1 -0
- package/dist/{strategies → lib/strategies}/CacheFirst.d.ts +1 -1
- package/dist/lib/strategies/CacheFirst.d.ts.map +1 -0
- package/dist/{strategies → lib/strategies}/CacheOnly.d.ts +2 -2
- package/dist/lib/strategies/CacheOnly.d.ts.map +1 -0
- package/dist/{strategies → lib/strategies}/NetworkFirst.d.ts +2 -2
- package/dist/lib/strategies/NetworkFirst.d.ts.map +1 -0
- package/dist/{strategies → lib/strategies}/NetworkOnly.d.ts +2 -2
- package/dist/lib/strategies/NetworkOnly.d.ts.map +1 -0
- package/dist/{PrecacheStrategy.d.ts → lib/strategies/PrecacheStrategy.d.ts} +5 -5
- package/dist/lib/strategies/PrecacheStrategy.d.ts.map +1 -0
- package/dist/{strategies → lib/strategies}/StaleWhileRevalidate.d.ts +1 -1
- package/dist/lib/strategies/StaleWhileRevalidate.d.ts.map +1 -0
- package/dist/{strategies → lib/strategies}/Strategy.d.ts +15 -17
- package/dist/lib/strategies/Strategy.d.ts.map +1 -0
- package/dist/{strategies → lib/strategies}/StrategyHandler.d.ts +35 -41
- package/dist/lib/strategies/StrategyHandler.d.ts.map +1 -0
- package/dist/lib/strategies/plugins/cacheOkAndOpaquePlugin.d.ts +3 -0
- package/dist/lib/strategies/plugins/cacheOkAndOpaquePlugin.d.ts.map +1 -0
- package/dist/lib/strategies/utils/messages.d.ts.map +1 -0
- package/dist/navigationPreload.d.ts +3 -3
- package/dist/types.d.ts +12 -7
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/PrecacheCacheKeyPlugin.d.ts +1 -1
- package/dist/utils/PrecacheCacheKeyPlugin.d.ts.map +1 -1
- package/dist/utils/assert.d.ts.map +1 -1
- package/dist/utils/cleanupOutdatedCaches.d.ts.map +1 -0
- package/dist/utils/clientsClaim.d.ts.map +1 -0
- package/dist/{parseRoute.d.ts → utils/parseRoute.d.ts} +3 -3
- package/dist/utils/parseRoute.d.ts.map +1 -0
- package/package.json +7 -21
- package/src/NavigationRoute.ts +6 -7
- package/src/PrecacheRoute.ts +5 -5
- package/src/RegExpRoute.ts +5 -5
- package/src/Route.ts +11 -11
- package/src/Serwist.ts +73 -80
- package/src/copyResponse.ts +7 -9
- package/src/disableDevLogs.ts +1 -1
- package/src/index.internal.ts +4 -0
- package/src/index.legacy.ts +14 -18
- package/src/index.ts +105 -19
- package/src/legacy/PrecacheController.ts +8 -8
- package/src/legacy/PrecacheFallbackPlugin.ts +1 -1
- package/src/legacy/PrecacheRoute.ts +3 -3
- package/src/legacy/Router.ts +5 -5
- package/src/legacy/fallbacks.ts +3 -3
- package/src/legacy/handlePrecaching.ts +1 -1
- package/src/legacy/initializeGoogleAnalytics.ts +218 -0
- package/src/legacy/installSerwist.ts +156 -5
- package/src/legacy/registerRoute.ts +1 -1
- package/src/{plugins → lib}/backgroundSync/BackgroundSyncPlugin.ts +6 -6
- package/src/{plugins/backgroundSync/Queue.ts → lib/backgroundSync/BackgroundSyncQueue.ts} +47 -26
- package/src/{plugins/backgroundSync/QueueDb.ts → lib/backgroundSync/BackgroundSyncQueueDb.ts} +16 -16
- package/src/{plugins/backgroundSync/QueueStore.ts → lib/backgroundSync/BackgroundSyncQueueStore.ts} +13 -13
- package/src/{plugins → lib}/backgroundSync/StorableRequest.ts +2 -2
- package/src/{plugins → lib}/broadcastUpdate/BroadcastCacheUpdate.ts +14 -9
- package/src/{plugins → lib}/broadcastUpdate/BroadcastUpdatePlugin.ts +1 -1
- package/src/lib/broadcastUpdate/constants.ts +12 -0
- package/src/{plugins → lib}/broadcastUpdate/types.ts +3 -3
- package/src/{plugins → lib}/cacheableResponse/CacheableResponse.ts +5 -5
- package/src/{plugins → lib}/expiration/CacheExpiration.ts +7 -7
- package/src/{plugins → lib}/expiration/ExpirationPlugin.ts +10 -10
- package/src/lib/googleAnalytics/constants.ts +22 -0
- package/src/{plugins/googleAnalytics/initialize.ts → lib/googleAnalytics/initializeGoogleAnalytics.ts} +9 -9
- package/src/{strategies → lib/strategies}/CacheFirst.ts +5 -5
- package/src/{strategies → lib/strategies}/CacheOnly.ts +6 -6
- package/src/{strategies → lib/strategies}/NetworkFirst.ts +7 -7
- package/src/{strategies → lib/strategies}/NetworkOnly.ts +7 -7
- package/src/{PrecacheStrategy.ts → lib/strategies/PrecacheStrategy.ts} +14 -10
- package/src/{strategies → lib/strategies}/StaleWhileRevalidate.ts +5 -5
- package/src/{strategies → lib/strategies}/Strategy.ts +19 -21
- package/src/{strategies → lib/strategies}/StrategyHandler.ts +45 -51
- package/src/{strategies → lib/strategies}/plugins/cacheOkAndOpaquePlugin.ts +1 -1
- package/src/{strategies → lib/strategies}/utils/messages.ts +2 -2
- package/src/models/messages/messages.ts +1 -1
- package/src/navigationPreload.ts +3 -3
- package/src/registerQuotaErrorCallback.ts +1 -1
- package/src/types.ts +12 -7
- package/src/utils/PrecacheCacheKeyPlugin.ts +1 -1
- package/src/utils/assert.ts +1 -1
- package/src/{cleanupOutdatedCaches.ts → utils/cleanupOutdatedCaches.ts} +3 -4
- package/src/utils/executeQuotaErrorCallbacks.ts +1 -1
- package/src/{parseRoute.ts → utils/parseRoute.ts} +21 -23
- package/src/utils/welcome.ts +1 -1
- package/dist/PrecacheStrategy.d.ts.map +0 -1
- package/dist/chunks/NetworkOnly.js +0 -599
- package/dist/chunks/PrecacheFallbackPlugin.js +0 -634
- package/dist/chunks/Serwist.js +0 -1034
- package/dist/chunks/registerQuotaErrorCallback.js +0 -17
- package/dist/chunks/timeout.js +0 -400
- package/dist/cleanupOutdatedCaches.d.ts.map +0 -1
- package/dist/clientsClaim.d.ts.map +0 -1
- package/dist/index.plugins.d.ts +0 -41
- package/dist/index.plugins.d.ts.map +0 -1
- package/dist/index.plugins.js +0 -669
- package/dist/index.strategies.d.ts +0 -22
- package/dist/index.strategies.d.ts.map +0 -1
- package/dist/index.strategies.js +0 -144
- package/dist/parseRoute.d.ts.map +0 -1
- package/dist/plugins/backgroundSync/BackgroundSyncPlugin.d.ts.map +0 -1
- package/dist/plugins/backgroundSync/Queue.d.ts.map +0 -1
- package/dist/plugins/backgroundSync/QueueDb.d.ts.map +0 -1
- package/dist/plugins/backgroundSync/QueueStore.d.ts.map +0 -1
- package/dist/plugins/backgroundSync/StorableRequest.d.ts.map +0 -1
- package/dist/plugins/broadcastUpdate/BroadcastCacheUpdate.d.ts.map +0 -1
- package/dist/plugins/broadcastUpdate/BroadcastUpdatePlugin.d.ts.map +0 -1
- package/dist/plugins/broadcastUpdate/constants.d.ts +0 -5
- package/dist/plugins/broadcastUpdate/constants.d.ts.map +0 -1
- package/dist/plugins/broadcastUpdate/responsesAreSame.d.ts.map +0 -1
- package/dist/plugins/broadcastUpdate/types.d.ts.map +0 -1
- package/dist/plugins/cacheableResponse/CacheableResponse.d.ts.map +0 -1
- package/dist/plugins/cacheableResponse/CacheableResponsePlugin.d.ts.map +0 -1
- package/dist/plugins/expiration/CacheExpiration.d.ts.map +0 -1
- package/dist/plugins/expiration/ExpirationPlugin.d.ts.map +0 -1
- package/dist/plugins/expiration/models/CacheTimestampsModel.d.ts.map +0 -1
- package/dist/plugins/googleAnalytics/constants.d.ts.map +0 -1
- package/dist/plugins/googleAnalytics/initialize.d.ts.map +0 -1
- package/dist/plugins/precaching/PrecacheFallbackPlugin.d.ts.map +0 -1
- package/dist/plugins/rangeRequests/RangeRequestsPlugin.d.ts.map +0 -1
- package/dist/plugins/rangeRequests/createPartialResponse.d.ts.map +0 -1
- package/dist/plugins/rangeRequests/utils/calculateEffectiveBoundaries.d.ts.map +0 -1
- package/dist/plugins/rangeRequests/utils/parseRangeHeader.d.ts.map +0 -1
- package/dist/strategies/CacheFirst.d.ts.map +0 -1
- package/dist/strategies/CacheOnly.d.ts.map +0 -1
- package/dist/strategies/NetworkFirst.d.ts.map +0 -1
- package/dist/strategies/NetworkOnly.d.ts.map +0 -1
- package/dist/strategies/StaleWhileRevalidate.d.ts.map +0 -1
- package/dist/strategies/Strategy.d.ts.map +0 -1
- package/dist/strategies/StrategyHandler.d.ts.map +0 -1
- package/dist/strategies/plugins/cacheOkAndOpaquePlugin.d.ts +0 -3
- package/dist/strategies/plugins/cacheOkAndOpaquePlugin.d.ts.map +0 -1
- package/dist/strategies/utils/messages.d.ts.map +0 -1
- package/src/index.plugins.ts +0 -95
- package/src/index.strategies.ts +0 -26
- package/src/plugins/broadcastUpdate/constants.ts +0 -12
- package/dist/{plugins/googleAnalytics → legacy}/constants.d.ts +0 -0
- package/dist/{plugins → lib}/backgroundSync/StorableRequest.d.ts +0 -0
- package/dist/{plugins → lib}/broadcastUpdate/BroadcastCacheUpdate.d.ts +0 -0
- package/dist/{plugins → lib}/broadcastUpdate/responsesAreSame.d.ts +0 -0
- package/dist/{plugins → lib}/cacheableResponse/CacheableResponse.d.ts +0 -0
- package/dist/{plugins → lib}/cacheableResponse/CacheableResponsePlugin.d.ts +0 -0
- package/dist/{plugins → lib}/expiration/models/CacheTimestampsModel.d.ts +0 -0
- package/dist/{plugins → lib}/rangeRequests/RangeRequestsPlugin.d.ts +0 -0
- package/dist/{plugins → lib}/rangeRequests/createPartialResponse.d.ts +0 -0
- package/dist/{plugins → lib}/rangeRequests/utils/calculateEffectiveBoundaries.d.ts +0 -0
- package/dist/{plugins → lib}/rangeRequests/utils/parseRangeHeader.d.ts +0 -0
- package/dist/{strategies → lib/strategies}/utils/messages.d.ts +0 -0
- package/dist/{cleanupOutdatedCaches.d.ts → utils/cleanupOutdatedCaches.d.ts} +0 -0
- package/dist/{clientsClaim.d.ts → utils/clientsClaim.d.ts} +0 -0
- package/src/{plugins/googleAnalytics → legacy}/constants.ts +0 -0
- package/src/{plugins → lib}/broadcastUpdate/responsesAreSame.ts +0 -0
- package/src/{plugins → lib}/cacheableResponse/CacheableResponsePlugin.ts +0 -0
- package/src/{plugins → lib}/expiration/models/CacheTimestampsModel.ts +0 -0
- package/src/{plugins → lib}/precaching/PrecacheFallbackPlugin.ts +1 -1
- package/src/{plugins → lib}/rangeRequests/RangeRequestsPlugin.ts +0 -0
- package/src/{plugins → lib}/rangeRequests/createPartialResponse.ts +1 -1
- package/src/{plugins → lib}/rangeRequests/utils/calculateEffectiveBoundaries.ts +1 -1
- package/src/{plugins → lib}/rangeRequests/utils/parseRangeHeader.ts +1 -1
- /package/src/{clientsClaim.ts → utils/clientsClaim.ts} +0 -0
package/src/legacy/Router.ts
CHANGED
|
@@ -6,16 +6,16 @@
|
|
|
6
6
|
https://opensource.org/licenses/MIT.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
+
import type { Route } from "../Route.js";
|
|
10
|
+
import type { HTTPMethod } from "../constants.js";
|
|
11
|
+
import { defaultMethod } from "../constants.js";
|
|
9
12
|
import type { RouteHandler, RouteHandlerCallbackOptions, RouteHandlerObject, RouteMatchCallback, RouteMatchCallbackOptions } from "../types.js";
|
|
10
|
-
import { assert } from "../utils/assert.js";
|
|
11
13
|
import { SerwistError } from "../utils/SerwistError.js";
|
|
14
|
+
import { assert } from "../utils/assert.js";
|
|
12
15
|
import { getFriendlyURL } from "../utils/getFriendlyURL.js";
|
|
13
16
|
import { logger } from "../utils/logger.js";
|
|
14
|
-
import type { Route } from "../Route.js";
|
|
15
|
-
import type { HTTPMethod } from "../constants.js";
|
|
16
|
-
import { defaultMethod } from "../constants.js";
|
|
17
|
-
import { parseRoute } from "../parseRoute.js";
|
|
18
17
|
import { normalizeHandler } from "../utils/normalizeHandler.js";
|
|
18
|
+
import { parseRoute } from "../utils/parseRoute.js";
|
|
19
19
|
|
|
20
20
|
declare const self: ServiceWorkerGlobalScope;
|
|
21
21
|
|
package/src/legacy/fallbacks.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Strategy } from "../strategies/Strategy.js";
|
|
1
|
+
import { Strategy } from "../lib/strategies/Strategy.js";
|
|
3
2
|
import type { PrecacheRouteOptions, RuntimeCaching } from "../types.js";
|
|
4
3
|
import type { PrecacheController } from "./PrecacheController.js";
|
|
4
|
+
import { PrecacheFallbackPlugin } from "./PrecacheFallbackPlugin.js";
|
|
5
|
+
import type { PrecacheFallbackEntry } from "./PrecacheFallbackPlugin.js";
|
|
5
6
|
import { PrecacheRoute } from "./PrecacheRoute.js";
|
|
6
7
|
import type { Router } from "./Router.js";
|
|
7
8
|
import { getSingletonPrecacheController } from "./singletonPrecacheController.js";
|
|
8
9
|
import { getSingletonRouter } from "./singletonRouter.js";
|
|
9
|
-
import type { PrecacheFallbackEntry } from "./PrecacheFallbackPlugin.js";
|
|
10
10
|
|
|
11
11
|
export interface FallbackEntry extends PrecacheFallbackEntry {
|
|
12
12
|
/**
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { NavigationRoute } from "../NavigationRoute.js";
|
|
2
|
-
import { cleanupOutdatedCaches as cleanupOutdatedCachesImpl } from "../cleanupOutdatedCaches.js";
|
|
3
2
|
import type { PrecacheEntry, PrecacheRouteOptions } from "../types.js";
|
|
3
|
+
import { cleanupOutdatedCaches as cleanupOutdatedCachesImpl } from "../utils/cleanupOutdatedCaches.js";
|
|
4
4
|
import type { PrecacheController } from "./PrecacheController.js";
|
|
5
5
|
import { PrecacheRoute } from "./PrecacheRoute.js";
|
|
6
6
|
import type { Router } from "./Router.js";
|
|
@@ -0,0 +1,218 @@
|
|
|
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 { Route } from "../Route.js";
|
|
10
|
+
import { BackgroundSyncPlugin } from "../lib/backgroundSync/BackgroundSyncPlugin.js";
|
|
11
|
+
import type { BackgroundSyncQueue, BackgroundSyncQueueEntry } from "../lib/backgroundSync/BackgroundSyncQueue.js";
|
|
12
|
+
import { NetworkFirst } from "../lib/strategies/NetworkFirst.js";
|
|
13
|
+
import { NetworkOnly } from "../lib/strategies/NetworkOnly.js";
|
|
14
|
+
import type { RouteMatchCallbackOptions } from "../types.js";
|
|
15
|
+
import { cacheNames as privateCacheNames } from "../utils/cacheNames.js";
|
|
16
|
+
import { getFriendlyURL } from "../utils/getFriendlyURL.js";
|
|
17
|
+
import { logger } from "../utils/logger.js";
|
|
18
|
+
import { Router } from "./Router.js";
|
|
19
|
+
import {
|
|
20
|
+
ANALYTICS_JS_PATH,
|
|
21
|
+
COLLECT_PATHS_REGEX,
|
|
22
|
+
GOOGLE_ANALYTICS_HOST,
|
|
23
|
+
GTAG_JS_PATH,
|
|
24
|
+
GTM_HOST,
|
|
25
|
+
GTM_JS_PATH,
|
|
26
|
+
MAX_RETENTION_TIME,
|
|
27
|
+
QUEUE_NAME,
|
|
28
|
+
} from "./constants.js";
|
|
29
|
+
import { getSingletonRouter } from "./singletonRouter.js";
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @deprecated
|
|
33
|
+
*/
|
|
34
|
+
export interface GoogleAnalyticsInitializeOptions {
|
|
35
|
+
/**
|
|
36
|
+
* An optional `Router` instance. If not provided, the singleton `Router`
|
|
37
|
+
* will be used.
|
|
38
|
+
*/
|
|
39
|
+
router?: Router;
|
|
40
|
+
/**
|
|
41
|
+
* The cache name to store and retrieve analytics.js. Defaults to Serwist's default cache names.
|
|
42
|
+
*/
|
|
43
|
+
cacheName?: string;
|
|
44
|
+
/**
|
|
45
|
+
* [Measurement Protocol parameters](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters),
|
|
46
|
+
* expressed as key/value pairs, to be added to replayed Google Analytics
|
|
47
|
+
* requests. This can be used to, e.g., set a custom dimension indicating
|
|
48
|
+
* that the request was replayed.
|
|
49
|
+
*/
|
|
50
|
+
parameterOverrides?: { [paramName: string]: string };
|
|
51
|
+
/**
|
|
52
|
+
* A function that allows you to modify the hit parameters prior to replaying
|
|
53
|
+
* the hit. The function is invoked with the original hit's URLSearchParams
|
|
54
|
+
* object as its only argument.
|
|
55
|
+
*/
|
|
56
|
+
hitFilter?: (params: URLSearchParams) => void;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Creates the requestWillDequeue callback to be used with the background
|
|
61
|
+
* sync plugin. The callback takes the failed request and adds the
|
|
62
|
+
* `qt` param based on the current time, as well as applies any other
|
|
63
|
+
* user-defined hit modifications.
|
|
64
|
+
*
|
|
65
|
+
* @param config
|
|
66
|
+
* @returns The requestWillDequeue callback function.
|
|
67
|
+
* @private
|
|
68
|
+
*/
|
|
69
|
+
const createOnSyncCallback = (config: Pick<GoogleAnalyticsInitializeOptions, "parameterOverrides" | "hitFilter">) => {
|
|
70
|
+
return async ({ queue }: { queue: BackgroundSyncQueue }) => {
|
|
71
|
+
let entry: BackgroundSyncQueueEntry | undefined = undefined;
|
|
72
|
+
while ((entry = await queue.shiftRequest())) {
|
|
73
|
+
const { request, timestamp } = entry;
|
|
74
|
+
const url = new URL(request.url);
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
// Measurement protocol requests can set their payload parameters in
|
|
78
|
+
// either the URL query string (for GET requests) or the POST body.
|
|
79
|
+
const params = request.method === "POST" ? new URLSearchParams(await request.clone().text()) : url.searchParams;
|
|
80
|
+
|
|
81
|
+
// Calculate the qt param, accounting for the fact that an existing
|
|
82
|
+
// qt param may be present and should be updated rather than replaced.
|
|
83
|
+
const originalHitTime = timestamp! - (Number(params.get("qt")) || 0);
|
|
84
|
+
const queueTime = Date.now() - originalHitTime;
|
|
85
|
+
|
|
86
|
+
// Set the qt param prior to applying hitFilter or parameterOverrides.
|
|
87
|
+
params.set("qt", String(queueTime));
|
|
88
|
+
|
|
89
|
+
// Apply `parameterOverrides`, if set.
|
|
90
|
+
if (config.parameterOverrides) {
|
|
91
|
+
for (const param of Object.keys(config.parameterOverrides)) {
|
|
92
|
+
const value = config.parameterOverrides[param];
|
|
93
|
+
params.set(param, value);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Apply `hitFilter`, if set.
|
|
98
|
+
if (typeof config.hitFilter === "function") {
|
|
99
|
+
config.hitFilter.call(null, params);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Retry the fetch. Ignore URL search params from the URL as they're
|
|
103
|
+
// now in the post body.
|
|
104
|
+
await fetch(
|
|
105
|
+
new Request(url.origin + url.pathname, {
|
|
106
|
+
body: params.toString(),
|
|
107
|
+
method: "POST",
|
|
108
|
+
mode: "cors",
|
|
109
|
+
credentials: "omit",
|
|
110
|
+
headers: { "Content-Type": "text/plain" },
|
|
111
|
+
}),
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
if (process.env.NODE_ENV !== "production") {
|
|
115
|
+
logger.log(`Request for '${getFriendlyURL(url.href)}' has been replayed`);
|
|
116
|
+
}
|
|
117
|
+
} catch (err) {
|
|
118
|
+
await queue.unshiftRequest(entry);
|
|
119
|
+
|
|
120
|
+
if (process.env.NODE_ENV !== "production") {
|
|
121
|
+
logger.log(`Request for '${getFriendlyURL(url.href)}' failed to replay, putting it back in the queue.`);
|
|
122
|
+
}
|
|
123
|
+
throw err;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (process.env.NODE_ENV !== "production") {
|
|
127
|
+
logger.log("All Google Analytics request successfully replayed; " + "the queue is now empty!");
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Creates GET and POST routes to catch failed Measurement Protocol hits.
|
|
134
|
+
*
|
|
135
|
+
* @param bgSyncPlugin
|
|
136
|
+
* @returns The created routes.
|
|
137
|
+
* @private
|
|
138
|
+
*/
|
|
139
|
+
const createCollectRoutes = (bgSyncPlugin: BackgroundSyncPlugin) => {
|
|
140
|
+
const match = ({ url }: RouteMatchCallbackOptions) => url.hostname === GOOGLE_ANALYTICS_HOST && COLLECT_PATHS_REGEX.test(url.pathname);
|
|
141
|
+
|
|
142
|
+
const handler = new NetworkOnly({
|
|
143
|
+
plugins: [bgSyncPlugin],
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
return [new Route(match, handler, "GET"), new Route(match, handler, "POST")];
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Creates a route with a network first strategy for the analytics.js script.
|
|
151
|
+
*
|
|
152
|
+
* @param cacheName
|
|
153
|
+
* @returns The created route.
|
|
154
|
+
* @private
|
|
155
|
+
*/
|
|
156
|
+
const createAnalyticsJsRoute = (cacheName: string) => {
|
|
157
|
+
const match = ({ url }: RouteMatchCallbackOptions) => url.hostname === GOOGLE_ANALYTICS_HOST && url.pathname === ANALYTICS_JS_PATH;
|
|
158
|
+
|
|
159
|
+
const handler = new NetworkFirst({ cacheName });
|
|
160
|
+
|
|
161
|
+
return new Route(match, handler, "GET");
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Creates a route with a network first strategy for the gtag.js script.
|
|
166
|
+
*
|
|
167
|
+
* @param cacheName
|
|
168
|
+
* @returns The created route.
|
|
169
|
+
* @private
|
|
170
|
+
*/
|
|
171
|
+
const createGtagJsRoute = (cacheName: string) => {
|
|
172
|
+
const match = ({ url }: RouteMatchCallbackOptions) => url.hostname === GTM_HOST && url.pathname === GTAG_JS_PATH;
|
|
173
|
+
|
|
174
|
+
const handler = new NetworkFirst({ cacheName });
|
|
175
|
+
|
|
176
|
+
return new Route(match, handler, "GET");
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Creates a route with a network first strategy for the gtm.js script.
|
|
181
|
+
*
|
|
182
|
+
* @param cacheName
|
|
183
|
+
* @returns The created route.
|
|
184
|
+
* @private
|
|
185
|
+
*/
|
|
186
|
+
const createGtmJsRoute = (cacheName: string) => {
|
|
187
|
+
const match = ({ url }: RouteMatchCallbackOptions) => url.hostname === GTM_HOST && url.pathname === GTM_JS_PATH;
|
|
188
|
+
|
|
189
|
+
const handler = new NetworkFirst({ cacheName });
|
|
190
|
+
|
|
191
|
+
return new Route(match, handler, "GET");
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Initialize Serwist's offline Google Analytics v3 support.
|
|
196
|
+
*
|
|
197
|
+
* @param options
|
|
198
|
+
* @deprecated Use `serwist.initializeGoogleAnalytics` instead.
|
|
199
|
+
*/
|
|
200
|
+
export const initializeGoogleAnalytics = ({ router = getSingletonRouter(), cacheName, ...options }: GoogleAnalyticsInitializeOptions = {}): void => {
|
|
201
|
+
const resolvedCacheName = privateCacheNames.getGoogleAnalyticsName(cacheName);
|
|
202
|
+
|
|
203
|
+
const bgSyncPlugin = new BackgroundSyncPlugin(QUEUE_NAME, {
|
|
204
|
+
maxRetentionTime: MAX_RETENTION_TIME,
|
|
205
|
+
onSync: createOnSyncCallback(options),
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
const routes = [
|
|
209
|
+
createGtmJsRoute(resolvedCacheName),
|
|
210
|
+
createAnalyticsJsRoute(resolvedCacheName),
|
|
211
|
+
createGtagJsRoute(resolvedCacheName),
|
|
212
|
+
...createCollectRoutes(bgSyncPlugin),
|
|
213
|
+
];
|
|
214
|
+
|
|
215
|
+
for (const route of routes) {
|
|
216
|
+
router.registerRoute(route);
|
|
217
|
+
}
|
|
218
|
+
};
|
|
@@ -1,11 +1,88 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { disableDevLogs as disableDevLogsImpl } from "../disableDevLogs.js";
|
|
2
|
+
import { enableNavigationPreload } from "../navigationPreload.js";
|
|
3
|
+
import { setCacheNameDetails } from "../setCacheNameDetails.js";
|
|
4
|
+
import type { RuntimeCaching } from "../types.js";
|
|
5
|
+
import { clientsClaim as clientsClaimImpl } from "../utils/clientsClaim.js";
|
|
6
|
+
import type { PrecacheController } from "./PrecacheController.js";
|
|
7
|
+
import type { Router } from "./Router.js";
|
|
8
|
+
import type { FallbacksOptions } from "./fallbacks.js";
|
|
9
|
+
import { fallbacks as fallbacksImpl } from "./fallbacks.js";
|
|
10
|
+
import { type HandlePrecachingOptions, handlePrecaching } from "./handlePrecaching.js";
|
|
11
|
+
import { initializeGoogleAnalytics } from "./initializeGoogleAnalytics.js";
|
|
12
|
+
import type { GoogleAnalyticsInitializeOptions } from "./initializeGoogleAnalytics.js";
|
|
13
|
+
import { registerRuntimeCaching } from "./registerRuntimeCaching.js";
|
|
14
|
+
import { getSingletonPrecacheController } from "./singletonPrecacheController.js";
|
|
15
|
+
import { getSingletonRouter } from "./singletonRouter.js";
|
|
16
|
+
|
|
17
|
+
declare const self: ServiceWorkerGlobalScope;
|
|
2
18
|
|
|
3
19
|
/**
|
|
4
20
|
* Options for `installSerwist`.
|
|
5
21
|
*
|
|
6
22
|
* @deprecated
|
|
7
23
|
*/
|
|
8
|
-
export
|
|
24
|
+
export interface InstallSerwistOptions extends Omit<HandlePrecachingOptions, "precacheController" | "router"> {
|
|
25
|
+
/**
|
|
26
|
+
* An optional `PrecacheController` instance. If not provided, the singleton
|
|
27
|
+
* `PrecacheController` will be used.
|
|
28
|
+
*/
|
|
29
|
+
precacheController?: PrecacheController;
|
|
30
|
+
/**
|
|
31
|
+
* An optional `Router` instance. If not provided, the singleton `Router`
|
|
32
|
+
* will be used.
|
|
33
|
+
*/
|
|
34
|
+
router?: Router;
|
|
35
|
+
/**
|
|
36
|
+
* Forces the waiting service worker to become the active one.
|
|
37
|
+
*
|
|
38
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/skipWaiting
|
|
39
|
+
*/
|
|
40
|
+
skipWaiting?: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Imports external scripts. They are executed in the order they
|
|
43
|
+
* are passed.
|
|
44
|
+
*
|
|
45
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/importScripts
|
|
46
|
+
*/
|
|
47
|
+
importScripts?: string[];
|
|
48
|
+
/**
|
|
49
|
+
* Enables navigation preloading if it is supported.
|
|
50
|
+
*
|
|
51
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/navigationPreload
|
|
52
|
+
*/
|
|
53
|
+
navigationPreload?: boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Modifies the prefix of the default cache names used by Serwist packages.
|
|
56
|
+
*/
|
|
57
|
+
cacheId?: string | undefined;
|
|
58
|
+
/**
|
|
59
|
+
* Claims any currently available clients once the service worker
|
|
60
|
+
* becomes active. This is normally used in conjunction with `skipWaiting()`.
|
|
61
|
+
*
|
|
62
|
+
* @default false
|
|
63
|
+
*/
|
|
64
|
+
clientsClaim?: boolean;
|
|
65
|
+
/**
|
|
66
|
+
* A list of caching strategies.
|
|
67
|
+
*/
|
|
68
|
+
runtimeCaching?: RuntimeCaching[];
|
|
69
|
+
/**
|
|
70
|
+
* Your configuration for `@serwist/google-analytics`. This plugin is
|
|
71
|
+
* only initialized when this option is not `undefined` or `false`.
|
|
72
|
+
*/
|
|
73
|
+
offlineAnalyticsConfig?: GoogleAnalyticsInitializeOptions | boolean;
|
|
74
|
+
/**
|
|
75
|
+
* Disables Serwist's logging in development mode.
|
|
76
|
+
*
|
|
77
|
+
* @default false
|
|
78
|
+
*/
|
|
79
|
+
disableDevLogs?: boolean;
|
|
80
|
+
/**
|
|
81
|
+
* Precaches routes so that they can be used as a fallback when
|
|
82
|
+
* a Strategy fails to generate a response.
|
|
83
|
+
*/
|
|
84
|
+
fallbacks?: Pick<FallbacksOptions, "entries">;
|
|
85
|
+
}
|
|
9
86
|
|
|
10
87
|
/**
|
|
11
88
|
* Abstracts away the core APIs of Serwist.
|
|
@@ -13,7 +90,81 @@ export type InstallSerwistOptions = SerwistOptions;
|
|
|
13
90
|
* @param options - `installSerwist` options.
|
|
14
91
|
* @deprecated
|
|
15
92
|
*/
|
|
16
|
-
export const installSerwist = (
|
|
17
|
-
|
|
18
|
-
|
|
93
|
+
export const installSerwist = ({
|
|
94
|
+
precacheController = getSingletonPrecacheController(),
|
|
95
|
+
router = getSingletonRouter(),
|
|
96
|
+
precacheEntries,
|
|
97
|
+
precacheOptions,
|
|
98
|
+
cleanupOutdatedCaches,
|
|
99
|
+
navigateFallback,
|
|
100
|
+
navigateFallbackAllowlist,
|
|
101
|
+
navigateFallbackDenylist,
|
|
102
|
+
skipWaiting,
|
|
103
|
+
importScripts,
|
|
104
|
+
navigationPreload = false,
|
|
105
|
+
cacheId,
|
|
106
|
+
clientsClaim = false,
|
|
107
|
+
runtimeCaching,
|
|
108
|
+
offlineAnalyticsConfig,
|
|
109
|
+
disableDevLogs = false,
|
|
110
|
+
fallbacks,
|
|
111
|
+
}: InstallSerwistOptions): void => {
|
|
112
|
+
if (!!importScripts && importScripts.length > 0) self.importScripts(...importScripts);
|
|
113
|
+
|
|
114
|
+
if (navigationPreload) enableNavigationPreload();
|
|
115
|
+
|
|
116
|
+
if (cacheId !== undefined) {
|
|
117
|
+
setCacheNameDetails({
|
|
118
|
+
prefix: cacheId,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (skipWaiting) {
|
|
123
|
+
self.skipWaiting();
|
|
124
|
+
} else {
|
|
125
|
+
self.addEventListener("message", (event) => {
|
|
126
|
+
if (event.data && event.data.type === "SKIP_WAITING") {
|
|
127
|
+
self.skipWaiting();
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (clientsClaim) clientsClaimImpl();
|
|
133
|
+
|
|
134
|
+
handlePrecaching({
|
|
135
|
+
precacheController,
|
|
136
|
+
router,
|
|
137
|
+
precacheEntries,
|
|
138
|
+
precacheOptions,
|
|
139
|
+
cleanupOutdatedCaches,
|
|
140
|
+
navigateFallback,
|
|
141
|
+
navigateFallbackAllowlist,
|
|
142
|
+
navigateFallbackDenylist,
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
if (runtimeCaching !== undefined) {
|
|
146
|
+
if (fallbacks !== undefined) {
|
|
147
|
+
runtimeCaching = fallbacksImpl({
|
|
148
|
+
precacheController,
|
|
149
|
+
router,
|
|
150
|
+
runtimeCaching,
|
|
151
|
+
entries: fallbacks.entries,
|
|
152
|
+
precacheOptions,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
registerRuntimeCaching(...runtimeCaching);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (offlineAnalyticsConfig !== undefined) {
|
|
159
|
+
if (typeof offlineAnalyticsConfig === "boolean") {
|
|
160
|
+
offlineAnalyticsConfig && initializeGoogleAnalytics({ router });
|
|
161
|
+
} else {
|
|
162
|
+
initializeGoogleAnalytics({
|
|
163
|
+
...offlineAnalyticsConfig,
|
|
164
|
+
router,
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (disableDevLogs) disableDevLogsImpl();
|
|
19
170
|
};
|
|
@@ -6,9 +6,9 @@
|
|
|
6
6
|
https://opensource.org/licenses/MIT.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import type { RouteHandler, RouteMatchCallback } from "../types.js";
|
|
10
9
|
import type { Route } from "../Route.js";
|
|
11
10
|
import type { HTTPMethod } from "../constants.js";
|
|
11
|
+
import type { RouteHandler, RouteMatchCallback } from "../types.js";
|
|
12
12
|
import { getSingletonRouter } from "./singletonRouter.js";
|
|
13
13
|
|
|
14
14
|
/**
|
|
@@ -7,25 +7,25 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import type { FetchDidFailCallbackParam, SerwistPlugin } from "../../types.js";
|
|
10
|
-
import type {
|
|
11
|
-
import {
|
|
10
|
+
import type { BackgroundSyncQueueOptions } from "./BackgroundSyncQueue.js";
|
|
11
|
+
import { BackgroundSyncQueue } from "./BackgroundSyncQueue.js";
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* A class implementing the `fetchDidFail` lifecycle callback. This makes it
|
|
15
15
|
* easier to add failed requests to a background sync Queue.
|
|
16
16
|
*/
|
|
17
17
|
export class BackgroundSyncPlugin implements SerwistPlugin {
|
|
18
|
-
private readonly _queue:
|
|
18
|
+
private readonly _queue: BackgroundSyncQueue;
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
21
|
* @param name See the `serwist/plugins.BackgroundSyncQueue`
|
|
22
22
|
* documentation for parameter details.
|
|
23
23
|
* @param options See the `serwist/plugins.BackgroundSyncQueue`
|
|
24
24
|
* documentation for parameter details.
|
|
25
|
-
* @see https://serwist.pages.dev/docs/background-sync
|
|
25
|
+
* @see https://serwist.pages.dev/docs/core/background-sync-queue
|
|
26
26
|
*/
|
|
27
|
-
constructor(name: string, options?:
|
|
28
|
-
this._queue = new
|
|
27
|
+
constructor(name: string, options?: BackgroundSyncQueueOptions) {
|
|
28
|
+
this._queue = new BackgroundSyncQueue(name, options);
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
/**
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { SerwistError } from "../../utils/SerwistError.js";
|
|
1
2
|
/*
|
|
2
3
|
Copyright 2018 Google LLC
|
|
3
4
|
|
|
@@ -6,23 +7,43 @@
|
|
|
6
7
|
https://opensource.org/licenses/MIT.
|
|
7
8
|
*/
|
|
8
9
|
import { assert } from "../../utils/assert.js";
|
|
9
|
-
import { SerwistError } from "../../utils/SerwistError.js";
|
|
10
10
|
import { getFriendlyURL } from "../../utils/getFriendlyURL.js";
|
|
11
11
|
import { logger } from "../../utils/logger.js";
|
|
12
|
-
import type {
|
|
13
|
-
import {
|
|
12
|
+
import type { BackgroundSyncQueueStoreEntry, UnidentifiedQueueStoreEntry } from "./BackgroundSyncQueueDb.js";
|
|
13
|
+
import { BackgroundSyncQueueStore } from "./BackgroundSyncQueueStore.js";
|
|
14
14
|
import { StorableRequest } from "./StorableRequest.js";
|
|
15
15
|
|
|
16
|
+
// See https://github.com/GoogleChrome/workbox/issues/2946
|
|
17
|
+
interface SyncManager {
|
|
18
|
+
getTags(): Promise<string[]>;
|
|
19
|
+
register(tag: string): Promise<void>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
declare global {
|
|
23
|
+
interface ServiceWorkerRegistration {
|
|
24
|
+
readonly sync: SyncManager;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
interface SyncEvent extends ExtendableEvent {
|
|
28
|
+
readonly lastChance: boolean;
|
|
29
|
+
readonly tag: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface ServiceWorkerGlobalScopeEventMap {
|
|
33
|
+
sync: SyncEvent;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
16
37
|
// Give TypeScript the correct global.
|
|
17
|
-
declare
|
|
38
|
+
declare const self: ServiceWorkerGlobalScope;
|
|
18
39
|
|
|
19
40
|
interface OnSyncCallbackOptions {
|
|
20
|
-
queue:
|
|
41
|
+
queue: BackgroundSyncQueue;
|
|
21
42
|
}
|
|
22
43
|
|
|
23
44
|
type OnSyncCallback = (options: OnSyncCallbackOptions) => void | Promise<void>;
|
|
24
45
|
|
|
25
|
-
export interface
|
|
46
|
+
export interface BackgroundSyncQueueOptions {
|
|
26
47
|
/**
|
|
27
48
|
* If `true`, instead of attempting to use background sync events, always attempt
|
|
28
49
|
* to replay queued request at service worker startup. Most folks will not need
|
|
@@ -50,7 +71,7 @@ export interface QueueOptions {
|
|
|
50
71
|
onSync?: OnSyncCallback;
|
|
51
72
|
}
|
|
52
73
|
|
|
53
|
-
export interface
|
|
74
|
+
export interface BackgroundSyncQueueEntry {
|
|
54
75
|
/**
|
|
55
76
|
* The request to store in the queue.
|
|
56
77
|
*/
|
|
@@ -85,8 +106,8 @@ const queueNames = new Set<string>();
|
|
|
85
106
|
* @returns
|
|
86
107
|
* @private
|
|
87
108
|
*/
|
|
88
|
-
const convertEntry = (queueStoreEntry: UnidentifiedQueueStoreEntry):
|
|
89
|
-
const queueEntry:
|
|
109
|
+
const convertEntry = (queueStoreEntry: UnidentifiedQueueStoreEntry): BackgroundSyncQueueEntry => {
|
|
110
|
+
const queueEntry: BackgroundSyncQueueEntry = {
|
|
90
111
|
request: new StorableRequest(queueStoreEntry.requestData).toRequest(),
|
|
91
112
|
timestamp: queueStoreEntry.timestamp,
|
|
92
113
|
};
|
|
@@ -101,11 +122,11 @@ const convertEntry = (queueStoreEntry: UnidentifiedQueueStoreEntry): QueueEntry
|
|
|
101
122
|
* later. All parts of the storing and replaying process are observable via
|
|
102
123
|
* callbacks.
|
|
103
124
|
*/
|
|
104
|
-
export class
|
|
125
|
+
export class BackgroundSyncQueue {
|
|
105
126
|
private readonly _name: string;
|
|
106
127
|
private readonly _onSync: OnSyncCallback;
|
|
107
128
|
private readonly _maxRetentionTime: number;
|
|
108
|
-
private readonly _queueStore:
|
|
129
|
+
private readonly _queueStore: BackgroundSyncQueueStore;
|
|
109
130
|
private readonly _forceSyncFallback: boolean;
|
|
110
131
|
private _syncInProgress = false;
|
|
111
132
|
private _requestsAddedDuringSync = false;
|
|
@@ -119,7 +140,7 @@ export class Queue {
|
|
|
119
140
|
* a duplicate name is detected.
|
|
120
141
|
* @param options
|
|
121
142
|
*/
|
|
122
|
-
constructor(name: string, { forceSyncFallback, onSync, maxRetentionTime }:
|
|
143
|
+
constructor(name: string, { forceSyncFallback, onSync, maxRetentionTime }: BackgroundSyncQueueOptions = {}) {
|
|
123
144
|
// Ensure the store name is not already being used
|
|
124
145
|
if (queueNames.has(name)) {
|
|
125
146
|
throw new SerwistError("duplicate-queue-name", { name });
|
|
@@ -130,7 +151,7 @@ export class Queue {
|
|
|
130
151
|
this._onSync = onSync || this.replayRequests;
|
|
131
152
|
this._maxRetentionTime = maxRetentionTime || MAX_RETENTION_TIME;
|
|
132
153
|
this._forceSyncFallback = Boolean(forceSyncFallback);
|
|
133
|
-
this._queueStore = new
|
|
154
|
+
this._queueStore = new BackgroundSyncQueueStore(this._name);
|
|
134
155
|
|
|
135
156
|
this._addSyncListener();
|
|
136
157
|
}
|
|
@@ -148,16 +169,16 @@ export class Queue {
|
|
|
148
169
|
*
|
|
149
170
|
* @param entry
|
|
150
171
|
*/
|
|
151
|
-
async pushRequest(entry:
|
|
172
|
+
async pushRequest(entry: BackgroundSyncQueueEntry): Promise<void> {
|
|
152
173
|
if (process.env.NODE_ENV !== "production") {
|
|
153
174
|
assert!.isType(entry, "object", {
|
|
154
|
-
moduleName: "serwist
|
|
175
|
+
moduleName: "serwist",
|
|
155
176
|
className: "BackgroundSyncQueue",
|
|
156
177
|
funcName: "pushRequest",
|
|
157
178
|
paramName: "entry",
|
|
158
179
|
});
|
|
159
180
|
assert!.isInstance(entry.request, Request, {
|
|
160
|
-
moduleName: "serwist
|
|
181
|
+
moduleName: "serwist",
|
|
161
182
|
className: "BackgroundSyncQueue",
|
|
162
183
|
funcName: "pushRequest",
|
|
163
184
|
paramName: "entry.request",
|
|
@@ -173,16 +194,16 @@ export class Queue {
|
|
|
173
194
|
*
|
|
174
195
|
* @param entry
|
|
175
196
|
*/
|
|
176
|
-
async unshiftRequest(entry:
|
|
197
|
+
async unshiftRequest(entry: BackgroundSyncQueueEntry): Promise<void> {
|
|
177
198
|
if (process.env.NODE_ENV !== "production") {
|
|
178
199
|
assert!.isType(entry, "object", {
|
|
179
|
-
moduleName: "serwist
|
|
200
|
+
moduleName: "serwist",
|
|
180
201
|
className: "BackgroundSyncQueue",
|
|
181
202
|
funcName: "unshiftRequest",
|
|
182
203
|
paramName: "entry",
|
|
183
204
|
});
|
|
184
205
|
assert!.isInstance(entry.request, Request, {
|
|
185
|
-
moduleName: "serwist
|
|
206
|
+
moduleName: "serwist",
|
|
186
207
|
className: "BackgroundSyncQueue",
|
|
187
208
|
funcName: "unshiftRequest",
|
|
188
209
|
paramName: "entry.request",
|
|
@@ -198,7 +219,7 @@ export class Queue {
|
|
|
198
219
|
*
|
|
199
220
|
* @returns
|
|
200
221
|
*/
|
|
201
|
-
async popRequest(): Promise<
|
|
222
|
+
async popRequest(): Promise<BackgroundSyncQueueEntry | undefined> {
|
|
202
223
|
return this._removeRequest("pop");
|
|
203
224
|
}
|
|
204
225
|
|
|
@@ -208,7 +229,7 @@ export class Queue {
|
|
|
208
229
|
*
|
|
209
230
|
* @returns
|
|
210
231
|
*/
|
|
211
|
-
async shiftRequest(): Promise<
|
|
232
|
+
async shiftRequest(): Promise<BackgroundSyncQueueEntry | undefined> {
|
|
212
233
|
return this._removeRequest("shift");
|
|
213
234
|
}
|
|
214
235
|
|
|
@@ -218,7 +239,7 @@ export class Queue {
|
|
|
218
239
|
*
|
|
219
240
|
* @returns
|
|
220
241
|
*/
|
|
221
|
-
async getAll(): Promise<
|
|
242
|
+
async getAll(): Promise<BackgroundSyncQueueEntry[]> {
|
|
222
243
|
const allEntries = await this._queueStore.getAll();
|
|
223
244
|
const now = Date.now();
|
|
224
245
|
|
|
@@ -254,7 +275,7 @@ export class Queue {
|
|
|
254
275
|
* @param operation
|
|
255
276
|
* @private
|
|
256
277
|
*/
|
|
257
|
-
async _addRequest({ request, metadata, timestamp = Date.now() }:
|
|
278
|
+
async _addRequest({ request, metadata, timestamp = Date.now() }: BackgroundSyncQueueEntry, operation: "push" | "unshift"): Promise<void> {
|
|
258
279
|
const storableRequest = await StorableRequest.fromRequest(request.clone());
|
|
259
280
|
const entry: UnidentifiedQueueStoreEntry = {
|
|
260
281
|
requestData: storableRequest.toObject(),
|
|
@@ -297,9 +318,9 @@ export class Queue {
|
|
|
297
318
|
* @returns
|
|
298
319
|
* @private
|
|
299
320
|
*/
|
|
300
|
-
async _removeRequest(operation: "pop" | "shift"): Promise<
|
|
321
|
+
async _removeRequest(operation: "pop" | "shift"): Promise<BackgroundSyncQueueEntry | undefined> {
|
|
301
322
|
const now = Date.now();
|
|
302
|
-
let entry:
|
|
323
|
+
let entry: BackgroundSyncQueueStoreEntry | undefined;
|
|
303
324
|
switch (operation) {
|
|
304
325
|
case "pop":
|
|
305
326
|
entry = await this._queueStore.popEntry();
|
|
@@ -329,7 +350,7 @@ export class Queue {
|
|
|
329
350
|
* the queue (which registers a retry for the next sync event).
|
|
330
351
|
*/
|
|
331
352
|
async replayRequests(): Promise<void> {
|
|
332
|
-
let entry:
|
|
353
|
+
let entry: BackgroundSyncQueueEntry | undefined = undefined;
|
|
333
354
|
while ((entry = await this.shiftRequest())) {
|
|
334
355
|
try {
|
|
335
356
|
await fetch(entry.request.clone());
|