serwist 9.1.0-preview.0 → 10.0.0-preview.2

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.
Files changed (239) hide show
  1. package/dist/NavigationRoute.d.ts.map +1 -1
  2. package/dist/RegExpRoute.d.ts.map +1 -1
  3. package/dist/Route.d.ts.map +1 -1
  4. package/dist/Serwist.d.ts +75 -53
  5. package/dist/Serwist.d.ts.map +1 -1
  6. package/dist/cacheNames.d.ts.map +1 -1
  7. package/dist/chunks/waitUntil.js +49 -42
  8. package/dist/copyResponse.d.ts.map +1 -1
  9. package/dist/index.d.ts +11 -36
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.internal.d.ts +16 -16
  12. package/dist/index.internal.d.ts.map +1 -1
  13. package/dist/index.internal.js +25 -3
  14. package/dist/index.js +1918 -248
  15. package/dist/lib/backgroundSync/BackgroundSyncPlugin.d.ts +2 -2
  16. package/dist/lib/backgroundSync/BackgroundSyncPlugin.d.ts.map +1 -1
  17. package/dist/lib/backgroundSync/StorableRequest.d.ts.map +1 -1
  18. package/dist/lib/backgroundSync/index.d.ts +6 -0
  19. package/dist/lib/backgroundSync/index.d.ts.map +1 -0
  20. package/dist/lib/broadcastUpdate/BroadcastCacheUpdate.d.ts.map +1 -1
  21. package/dist/lib/broadcastUpdate/BroadcastUpdatePlugin.d.ts +2 -2
  22. package/dist/lib/broadcastUpdate/BroadcastUpdatePlugin.d.ts.map +1 -1
  23. package/dist/lib/broadcastUpdate/index.d.ts +6 -0
  24. package/dist/lib/broadcastUpdate/index.d.ts.map +1 -0
  25. package/dist/lib/broadcastUpdate/responsesAreSame.d.ts.map +1 -1
  26. package/dist/lib/cacheableResponse/CacheableResponsePlugin.d.ts +3 -3
  27. package/dist/lib/cacheableResponse/CacheableResponsePlugin.d.ts.map +1 -1
  28. package/dist/lib/cacheableResponse/index.d.ts +4 -0
  29. package/dist/lib/cacheableResponse/index.d.ts.map +1 -0
  30. package/dist/{legacy/utils → lib/controllers/PrecacheController}/PrecacheCacheKeyPlugin.d.ts +4 -4
  31. package/dist/lib/controllers/PrecacheController/PrecacheCacheKeyPlugin.d.ts.map +1 -0
  32. package/dist/{legacy → lib/controllers/PrecacheController}/PrecacheController.d.ts +77 -64
  33. package/dist/lib/controllers/PrecacheController/PrecacheController.d.ts.map +1 -0
  34. package/dist/lib/controllers/PrecacheController/PrecacheInstallReportPlugin.d.ts +14 -0
  35. package/dist/lib/controllers/PrecacheController/PrecacheInstallReportPlugin.d.ts.map +1 -0
  36. package/dist/{PrecacheRoute.d.ts → lib/controllers/PrecacheController/PrecacheRoute.d.ts} +4 -5
  37. package/dist/lib/controllers/PrecacheController/PrecacheRoute.d.ts.map +1 -0
  38. package/dist/lib/{strategies → controllers/PrecacheController}/PrecacheStrategy.d.ts +6 -6
  39. package/dist/lib/controllers/PrecacheController/PrecacheStrategy.d.ts.map +1 -0
  40. package/dist/lib/controllers/PrecacheController/parsePrecacheOptions.d.ts +25 -0
  41. package/dist/lib/controllers/PrecacheController/parsePrecacheOptions.d.ts.map +1 -0
  42. package/dist/lib/controllers/RuntimeCacheController.d.ts +70 -0
  43. package/dist/lib/controllers/RuntimeCacheController.d.ts.map +1 -0
  44. package/dist/lib/controllers/index.d.ts +4 -0
  45. package/dist/lib/controllers/index.d.ts.map +1 -0
  46. package/dist/lib/expiration/ExpirationPlugin.d.ts +2 -2
  47. package/dist/lib/expiration/ExpirationPlugin.d.ts.map +1 -1
  48. package/dist/lib/expiration/index.d.ts +4 -0
  49. package/dist/lib/expiration/index.d.ts.map +1 -0
  50. package/dist/lib/googleAnalytics/index.d.ts +3 -0
  51. package/dist/lib/googleAnalytics/index.d.ts.map +1 -0
  52. package/dist/lib/googleAnalytics/initializeGoogleAnalytics.d.ts.map +1 -1
  53. package/dist/lib/precaching/PrecacheFallbackPlugin.d.ts +15 -9
  54. package/dist/lib/precaching/PrecacheFallbackPlugin.d.ts.map +1 -1
  55. package/dist/lib/precaching/index.d.ts +3 -0
  56. package/dist/lib/precaching/index.d.ts.map +1 -0
  57. package/dist/lib/rangeRequests/RangeRequestsPlugin.d.ts +3 -3
  58. package/dist/lib/rangeRequests/RangeRequestsPlugin.d.ts.map +1 -1
  59. package/dist/lib/rangeRequests/createPartialResponse.d.ts.map +1 -1
  60. package/dist/lib/rangeRequests/index.d.ts +3 -0
  61. package/dist/lib/rangeRequests/index.d.ts.map +1 -0
  62. package/dist/lib/rangeRequests/utils/calculateEffectiveBoundaries.d.ts.map +1 -1
  63. package/dist/lib/rangeRequests/utils/parseRangeHeader.d.ts.map +1 -1
  64. package/dist/lib/strategies/Strategy.d.ts +3 -3
  65. package/dist/lib/strategies/Strategy.d.ts.map +1 -1
  66. package/dist/lib/strategies/StrategyHandler.d.ts +4 -4
  67. package/dist/lib/strategies/StrategyHandler.d.ts.map +1 -1
  68. package/dist/lib/strategies/index.d.ts +11 -0
  69. package/dist/lib/strategies/index.d.ts.map +1 -0
  70. package/dist/lib/strategies/plugins/cacheOkAndOpaquePlugin.d.ts +2 -2
  71. package/dist/lib/strategies/plugins/cacheOkAndOpaquePlugin.d.ts.map +1 -1
  72. package/dist/navigationPreload.d.ts.map +1 -1
  73. package/dist/registerQuotaErrorCallback.d.ts.map +1 -1
  74. package/dist/setCacheNameDetails.d.ts +1 -1
  75. package/dist/setCacheNameDetails.d.ts.map +1 -1
  76. package/dist/types.d.ts +35 -64
  77. package/dist/types.d.ts.map +1 -1
  78. package/dist/utils/SerwistError.d.ts +1 -1
  79. package/dist/utils/SerwistError.d.ts.map +1 -1
  80. package/dist/utils/cleanupOutdatedCaches.d.ts.map +1 -1
  81. package/dist/utils/createCacheKey.d.ts.map +1 -1
  82. package/dist/utils/deleteOutdatedCaches.d.ts.map +1 -1
  83. package/dist/utils/getFriendlyURL.d.ts.map +1 -1
  84. package/dist/utils/normalizeHandler.d.ts.map +1 -1
  85. package/dist/utils/parseRoute.d.ts.map +1 -1
  86. package/dist/utils/pluginUtils.d.ts +2 -2
  87. package/dist/utils/pluginUtils.d.ts.map +1 -1
  88. package/dist/utils/printCleanupDetails.d.ts.map +1 -1
  89. package/dist/utils/printInstallDetails.d.ts.map +1 -1
  90. package/dist/utils/removeIgnoredSearchParams.d.ts.map +1 -1
  91. package/dist/utils/waitUntil.d.ts.map +1 -1
  92. package/package.json +29 -13
  93. package/src/NavigationRoute.ts +2 -2
  94. package/src/RegExpRoute.ts +2 -2
  95. package/src/Route.ts +2 -2
  96. package/src/Serwist.ts +170 -271
  97. package/src/cacheNames.ts +1 -1
  98. package/src/copyResponse.ts +2 -2
  99. package/src/index.internal.ts +16 -16
  100. package/src/index.ts +68 -93
  101. package/src/lib/backgroundSync/BackgroundSyncPlugin.ts +2 -2
  102. package/src/lib/backgroundSync/BackgroundSyncQueue.ts +4 -4
  103. package/src/lib/backgroundSync/BackgroundSyncQueueStore.ts +1 -1
  104. package/src/lib/backgroundSync/StorableRequest.ts +1 -1
  105. package/src/lib/backgroundSync/index.ts +5 -0
  106. package/src/lib/broadcastUpdate/BroadcastCacheUpdate.ts +4 -4
  107. package/src/lib/broadcastUpdate/BroadcastUpdatePlugin.ts +2 -2
  108. package/src/lib/broadcastUpdate/index.ts +5 -0
  109. package/src/lib/broadcastUpdate/responsesAreSame.ts +2 -2
  110. package/src/lib/cacheableResponse/CacheableResponse.ts +4 -4
  111. package/src/lib/cacheableResponse/CacheableResponsePlugin.ts +3 -3
  112. package/src/lib/cacheableResponse/index.ts +3 -0
  113. package/src/{legacy/utils → lib/controllers/PrecacheController}/PrecacheCacheKeyPlugin.ts +5 -6
  114. package/src/lib/controllers/PrecacheController/PrecacheController.ts +328 -0
  115. package/src/{utils → lib/controllers/PrecacheController}/PrecacheInstallReportPlugin.ts +5 -5
  116. package/src/{PrecacheRoute.ts → lib/controllers/PrecacheController/PrecacheRoute.ts} +11 -12
  117. package/src/lib/{strategies → controllers/PrecacheController}/PrecacheStrategy.ts +13 -13
  118. package/src/lib/controllers/PrecacheController/parsePrecacheOptions.ts +46 -0
  119. package/src/lib/controllers/RuntimeCacheController.ts +119 -0
  120. package/src/lib/controllers/index.ts +3 -0
  121. package/src/lib/expiration/CacheExpiration.ts +3 -3
  122. package/src/lib/expiration/ExpirationPlugin.ts +8 -8
  123. package/src/lib/expiration/index.ts +3 -0
  124. package/src/lib/googleAnalytics/index.ts +2 -0
  125. package/src/lib/googleAnalytics/initializeGoogleAnalytics.ts +5 -5
  126. package/src/lib/precaching/PrecacheFallbackPlugin.ts +23 -12
  127. package/src/lib/precaching/index.ts +2 -0
  128. package/src/lib/rangeRequests/RangeRequestsPlugin.ts +3 -3
  129. package/src/lib/rangeRequests/createPartialResponse.ts +3 -3
  130. package/src/lib/rangeRequests/index.ts +2 -0
  131. package/src/lib/rangeRequests/utils/calculateEffectiveBoundaries.ts +2 -2
  132. package/src/lib/rangeRequests/utils/parseRangeHeader.ts +2 -2
  133. package/src/lib/strategies/CacheFirst.ts +3 -3
  134. package/src/lib/strategies/CacheOnly.ts +3 -3
  135. package/src/lib/strategies/NetworkFirst.ts +3 -3
  136. package/src/lib/strategies/NetworkOnly.ts +4 -4
  137. package/src/lib/strategies/StaleWhileRevalidate.ts +3 -3
  138. package/src/lib/strategies/Strategy.ts +7 -7
  139. package/src/lib/strategies/StrategyHandler.ts +18 -18
  140. package/src/lib/strategies/index.ts +10 -0
  141. package/src/lib/strategies/plugins/cacheOkAndOpaquePlugin.ts +2 -2
  142. package/src/lib/strategies/utils/messages.ts +2 -2
  143. package/src/models/messages/messages.ts +3 -3
  144. package/src/navigationPreload.ts +1 -1
  145. package/src/registerQuotaErrorCallback.ts +2 -2
  146. package/src/setCacheNameDetails.ts +4 -4
  147. package/src/types.ts +56 -68
  148. package/src/utils/SerwistError.ts +2 -2
  149. package/src/utils/cacheNames.ts +1 -1
  150. package/src/utils/executeQuotaErrorCallbacks.ts +1 -1
  151. package/src/utils/pluginUtils.ts +2 -2
  152. package/dist/PrecacheRoute.d.ts.map +0 -1
  153. package/dist/chunks/printInstallDetails.js +0 -1601
  154. package/dist/chunks/resultingClientExists.js +0 -32
  155. package/dist/index.legacy.d.ts +0 -28
  156. package/dist/index.legacy.d.ts.map +0 -1
  157. package/dist/index.legacy.js +0 -790
  158. package/dist/legacy/PrecacheController.d.ts.map +0 -1
  159. package/dist/legacy/PrecacheFallbackPlugin.d.ts +0 -61
  160. package/dist/legacy/PrecacheFallbackPlugin.d.ts.map +0 -1
  161. package/dist/legacy/PrecacheRoute.d.ts +0 -19
  162. package/dist/legacy/PrecacheRoute.d.ts.map +0 -1
  163. package/dist/legacy/Router.d.ts +0 -151
  164. package/dist/legacy/Router.d.ts.map +0 -1
  165. package/dist/legacy/addPlugins.d.ts +0 -9
  166. package/dist/legacy/addPlugins.d.ts.map +0 -1
  167. package/dist/legacy/addRoute.d.ts +0 -14
  168. package/dist/legacy/addRoute.d.ts.map +0 -1
  169. package/dist/legacy/constants.d.ts +0 -10
  170. package/dist/legacy/constants.d.ts.map +0 -1
  171. package/dist/legacy/createHandlerBoundToURL.d.ts +0 -17
  172. package/dist/legacy/createHandlerBoundToURL.d.ts.map +0 -1
  173. package/dist/legacy/fallbacks.d.ts +0 -59
  174. package/dist/legacy/fallbacks.d.ts.map +0 -1
  175. package/dist/legacy/getCacheKeyForURL.d.ts +0 -20
  176. package/dist/legacy/getCacheKeyForURL.d.ts.map +0 -1
  177. package/dist/legacy/handlePrecaching.d.ts +0 -54
  178. package/dist/legacy/handlePrecaching.d.ts.map +0 -1
  179. package/dist/legacy/initializeGoogleAnalytics.d.ts +0 -38
  180. package/dist/legacy/initializeGoogleAnalytics.d.ts.map +0 -1
  181. package/dist/legacy/installSerwist.d.ts +0 -81
  182. package/dist/legacy/installSerwist.d.ts.map +0 -1
  183. package/dist/legacy/matchPrecache.d.ts +0 -15
  184. package/dist/legacy/matchPrecache.d.ts.map +0 -1
  185. package/dist/legacy/precache.d.ts +0 -20
  186. package/dist/legacy/precache.d.ts.map +0 -1
  187. package/dist/legacy/precacheAndRoute.d.ts +0 -14
  188. package/dist/legacy/precacheAndRoute.d.ts.map +0 -1
  189. package/dist/legacy/registerRoute.d.ts +0 -16
  190. package/dist/legacy/registerRoute.d.ts.map +0 -1
  191. package/dist/legacy/registerRuntimeCaching.d.ts +0 -11
  192. package/dist/legacy/registerRuntimeCaching.d.ts.map +0 -1
  193. package/dist/legacy/setCatchHandler.d.ts +0 -10
  194. package/dist/legacy/setCatchHandler.d.ts.map +0 -1
  195. package/dist/legacy/setDefaultHandler.d.ts +0 -13
  196. package/dist/legacy/setDefaultHandler.d.ts.map +0 -1
  197. package/dist/legacy/singletonPrecacheController.d.ts +0 -34
  198. package/dist/legacy/singletonPrecacheController.d.ts.map +0 -1
  199. package/dist/legacy/singletonRouter.d.ts +0 -41
  200. package/dist/legacy/singletonRouter.d.ts.map +0 -1
  201. package/dist/legacy/unregisterRoute.d.ts +0 -9
  202. package/dist/legacy/unregisterRoute.d.ts.map +0 -1
  203. package/dist/legacy/utils/PrecacheCacheKeyPlugin.d.ts.map +0 -1
  204. package/dist/legacy/utils/getCacheKeyForURL.d.ts +0 -14
  205. package/dist/legacy/utils/getCacheKeyForURL.d.ts.map +0 -1
  206. package/dist/lib/strategies/PrecacheStrategy.d.ts.map +0 -1
  207. package/dist/utils/PrecacheCacheKeyPlugin.d.ts +0 -16
  208. package/dist/utils/PrecacheCacheKeyPlugin.d.ts.map +0 -1
  209. package/dist/utils/PrecacheInstallReportPlugin.d.ts +0 -14
  210. package/dist/utils/PrecacheInstallReportPlugin.d.ts.map +0 -1
  211. package/dist/utils/parsePrecacheOptions.d.ts +0 -26
  212. package/dist/utils/parsePrecacheOptions.d.ts.map +0 -1
  213. package/src/index.legacy.ts +0 -62
  214. package/src/legacy/PrecacheController.ts +0 -337
  215. package/src/legacy/PrecacheFallbackPlugin.ts +0 -92
  216. package/src/legacy/PrecacheRoute.ts +0 -48
  217. package/src/legacy/Router.ts +0 -484
  218. package/src/legacy/addPlugins.ts +0 -21
  219. package/src/legacy/addRoute.ts +0 -27
  220. package/src/legacy/constants.ts +0 -22
  221. package/src/legacy/createHandlerBoundToURL.ts +0 -30
  222. package/src/legacy/fallbacks.ts +0 -94
  223. package/src/legacy/getCacheKeyForURL.ts +0 -32
  224. package/src/legacy/handlePrecaching.ts +0 -86
  225. package/src/legacy/initializeGoogleAnalytics.ts +0 -218
  226. package/src/legacy/installSerwist.ts +0 -170
  227. package/src/legacy/matchPrecache.ts +0 -27
  228. package/src/legacy/precache.ts +0 -33
  229. package/src/legacy/precacheAndRoute.ts +0 -27
  230. package/src/legacy/registerRoute.ts +0 -28
  231. package/src/legacy/registerRuntimeCaching.ts +0 -17
  232. package/src/legacy/setCatchHandler.ts +0 -21
  233. package/src/legacy/setDefaultHandler.ts +0 -24
  234. package/src/legacy/singletonPrecacheController.ts +0 -53
  235. package/src/legacy/singletonRouter.ts +0 -70
  236. package/src/legacy/unregisterRoute.ts +0 -13
  237. package/src/legacy/utils/getCacheKeyForURL.ts +0 -36
  238. package/src/utils/PrecacheCacheKeyPlugin.ts +0 -33
  239. package/src/utils/parsePrecacheOptions.ts +0 -47
@@ -1,32 +0,0 @@
1
- /*
2
- Copyright 2019 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 { getSingletonPrecacheController } from "./singletonPrecacheController.js";
10
-
11
- /**
12
- * Takes in a URL, and returns the corresponding URL that could be used to
13
- * lookup the entry in the precache.
14
- *
15
- * If a relative URL is provided, the location of the service worker file will
16
- * be used as the base.
17
- *
18
- * For precached entries without revision information, the cache key will be the
19
- * same as the original URL.
20
- *
21
- * For precached entries with revision information, the cache key will be the
22
- * original URL with the addition of a query parameter used for keeping track of
23
- * the revision info.
24
- *
25
- * @param url The URL whose cache key to look up.
26
- * @returns The cache key that corresponds to that URL.
27
- * @deprecated
28
- */
29
- export const getCacheKeyForURL = (url: string): string | undefined => {
30
- const precacheController = getSingletonPrecacheController();
31
- return precacheController.getCacheKeyForURL(url);
32
- };
@@ -1,86 +0,0 @@
1
- import { NavigationRoute } from "../NavigationRoute.js";
2
- import type { PrecacheEntry, PrecacheRouteOptions } from "../types.js";
3
- import { cleanupOutdatedCaches as cleanupOutdatedCachesImpl } from "../utils/cleanupOutdatedCaches.js";
4
- import type { PrecacheController } from "./PrecacheController.js";
5
- import { PrecacheRoute } from "./PrecacheRoute.js";
6
- import type { Router } from "./Router.js";
7
- import { createHandlerBoundToURL } from "./createHandlerBoundToURL.js";
8
- import { getSingletonPrecacheController } from "./singletonPrecacheController.js";
9
- import { getSingletonRouter } from "./singletonRouter.js";
10
-
11
- /**
12
- * @deprecated
13
- */
14
- export interface HandlePrecachingOptions {
15
- /**
16
- * An optional {@linkcode PrecacheController} instance. If not provided, the singleton
17
- * {@linkcode PrecacheController} will be used.
18
- */
19
- precacheController?: PrecacheController;
20
- /**
21
- * An optional {@linkcode Router} instance. If not provided, the singleton {@linkcode Router}
22
- * will be used.
23
- */
24
- router?: Router;
25
- /**
26
- * A list of URLs that should be cached.
27
- */
28
- precacheEntries?: (PrecacheEntry | string)[];
29
- /**
30
- * Options to customize how Serwist precaches the URLs.
31
- */
32
- precacheOptions?: PrecacheRouteOptions;
33
- /**
34
- * Whether outdated caches should be removed.
35
- *
36
- * @default false
37
- */
38
- cleanupOutdatedCaches?: boolean;
39
- /**
40
- * An URL that should point to a HTML file with which navigation requests for URLs that aren't
41
- * precached will be fulfilled.
42
- */
43
- navigateFallback?: string;
44
- /**
45
- * URLs that should be allowed to use the `navigateFallback` handler.
46
- */
47
- navigateFallbackAllowlist?: RegExp[];
48
- /**
49
- * URLs that should not be allowed to use the `navigateFallback` handler. This takes precedence
50
- * over `navigateFallbackAllowlist`.
51
- */
52
- navigateFallbackDenylist?: RegExp[];
53
- }
54
-
55
- /**
56
- * Handles a list of precache entries and cleans up outdated caches.
57
- *
58
- * @param options
59
- * @deprecated
60
- */
61
- export const handlePrecaching = ({
62
- precacheController = getSingletonPrecacheController(),
63
- router = getSingletonRouter(),
64
- precacheEntries,
65
- precacheOptions,
66
- cleanupOutdatedCaches = false,
67
- navigateFallback,
68
- navigateFallbackAllowlist,
69
- navigateFallbackDenylist,
70
- }: HandlePrecachingOptions): void => {
71
- if (!!precacheEntries && precacheEntries.length > 0) {
72
- precacheController.precache(precacheEntries);
73
- router.registerRoute(new PrecacheRoute(precacheController, precacheOptions));
74
-
75
- if (cleanupOutdatedCaches) cleanupOutdatedCachesImpl();
76
-
77
- if (navigateFallback) {
78
- router.registerRoute(
79
- new NavigationRoute(createHandlerBoundToURL(navigateFallback), {
80
- allowlist: navigateFallbackAllowlist,
81
- denylist: navigateFallbackDenylist,
82
- }),
83
- );
84
- }
85
- }
86
- };
@@ -1,218 +0,0 @@
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 {@linkcode Router} instance. If not provided, the singleton {@linkcode 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,170 +0,0 @@
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;
18
-
19
- /**
20
- * Options for {@linkcode installSerwist}.
21
- *
22
- * @deprecated
23
- */
24
- export interface InstallSerwistOptions extends Omit<HandlePrecachingOptions, "precacheController" | "router"> {
25
- /**
26
- * An optional {@linkcode PrecacheController} instance. If not provided, the singleton
27
- * {@linkcode PrecacheController} will be used.
28
- */
29
- precacheController?: PrecacheController;
30
- /**
31
- * An optional {@linkcode Router} instance. If not provided, the singleton {@linkcode 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
- }
86
-
87
- /**
88
- * Abstracts away the core APIs of Serwist.
89
- *
90
- * @param options - `installSerwist` options.
91
- * @deprecated
92
- */
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();
170
- };
@@ -1,27 +0,0 @@
1
- /*
2
- Copyright 2019 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 { getSingletonPrecacheController } from "./singletonPrecacheController.js";
10
- import type { PrecacheController } from "./PrecacheController.js";
11
-
12
- /**
13
- * Helper function that calls {@linkcode PrecacheController.matchPrecache}
14
- * on the default {@linkcode PrecacheController} instance.
15
- *
16
- * If you are creating your own {@linkcode PrecacheController}, then call
17
- * the {@linkcode PrecacheController.matchPrecache} function on that instance
18
- * instead of using this function.
19
- *
20
- * @param request The key (without revisioning parameters)
21
- * to look up in the precache.
22
- * @returns
23
- * @deprecated
24
- */
25
- export const matchPrecache = (request: string | Request): Promise<Response | undefined> => {
26
- return getSingletonPrecacheController().matchPrecache(request);
27
- };
@@ -1,33 +0,0 @@
1
- /*
2
- Copyright 2019 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 { PrecacheEntry } from "../types.js";
10
- import { getSingletonPrecacheController } from "./singletonPrecacheController.js";
11
- import type { addRoute } from "./addRoute.js";
12
- import type { precacheAndRoute } from "./precacheAndRoute.js";
13
-
14
- /**
15
- * Adds items to the precache list, removing any duplicates and
16
- * stores the files in the precache cache when the service
17
- * worker installs.
18
- *
19
- * This method can be called multiple times.
20
- *
21
- * Please note: This method **will not** serve any of the cached files for you.
22
- * It only precaches files. To respond to a network request you call
23
- * {@linkcode addRoute}.
24
- *
25
- * If you have a single array of files to precache, you can just call
26
- * {@linkcode precacheAndRoute}.
27
- *
28
- * @param entries Array of entries to precache.
29
- * @deprecated
30
- */
31
- export const precache = (entries: (PrecacheEntry | string)[]): void => {
32
- getSingletonPrecacheController().precache(entries);
33
- };
@@ -1,27 +0,0 @@
1
- /*
2
- Copyright 2019 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 { PrecacheEntry, PrecacheRouteOptions } from "../types.js";
10
- import { addRoute } from "./addRoute.js";
11
- import { precache } from "./precache.js";
12
-
13
- /**
14
- * This method will add entries to the precache list and add a route to
15
- * respond to `fetch` events.
16
- *
17
- * This is a convenience method that will call
18
- * {@linkcode precache} and {@linkcode addRoute} in a single call.
19
- *
20
- * @param entries Array of entries to precache.
21
- * @param options See the {@linkcode PrecacheRouteOptions} options.
22
- * @deprecated
23
- */
24
- export const precacheAndRoute = (entries: (PrecacheEntry | string)[], options?: PrecacheRouteOptions): void => {
25
- precache(entries);
26
- addRoute(options);
27
- };
@@ -1,28 +0,0 @@
1
- /*
2
- Copyright 2019 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
- import type { Router } from "./Router.js";
9
- import type { Route } from "../Route.js";
10
- import type { HTTPMethod } from "../constants.js";
11
- import type { RouteHandler, RouteMatchCallback } from "../types.js";
12
- import type { unregisterRoute } from "./unregisterRoute.js";
13
- import { getSingletonRouter } from "./singletonRouter.js";
14
-
15
- /**
16
- * Registers a `RegExp`, string, or function with a caching
17
- * strategy to a singleton {@linkcode Router} instance.
18
- *
19
- * @param capture If the capture param is a {@linkcode Route}, all other arguments will be ignored.
20
- * @param handler A callback function that returns a promise resulting in a response.
21
- * This parameter is required if `capture` is not a {@linkcode Route} object.
22
- * @param method The HTTP method to match the route against. Defaults to `'GET'`.
23
- * @returns The generated {@linkcode Route} object, which can then be provided to {@linkcode unregisterRoute} if needed.
24
- * @deprecated
25
- */
26
- export const registerRoute = (capture: RegExp | string | RouteMatchCallback | Route, handler?: RouteHandler, method?: HTTPMethod): Route => {
27
- return getSingletonRouter().registerCapture(capture, handler, method);
28
- };
@@ -1,17 +0,0 @@
1
- import { registerRoute } from "./registerRoute.js";
2
-
3
- import type { RuntimeCaching } from "../types.js";
4
-
5
- /**
6
- * Registers caching strategies to a singleton Router instance. It is a simple
7
- * syntatic sugar for {@linkcode registerRoute}.
8
- *
9
- * @param runtimeCachingList
10
- * @returns
11
- * @deprecated
12
- */
13
- export const registerRuntimeCaching = (...runtimeCachingList: RuntimeCaching[]): void => {
14
- for (const entry of runtimeCachingList) {
15
- registerRoute(entry.matcher, entry.handler, entry.method);
16
- }
17
- };
@@ -1,21 +0,0 @@
1
- /*
2
- Copyright 2019 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 { RouteHandler } from "../types.js";
10
- import { getSingletonRouter } from "./singletonRouter.js";
11
-
12
- /**
13
- * If a route throws an error while handling a request, this handler
14
- * will be called and given a chance to provide a response.
15
- *
16
- * @param handler A callback function that returns a promise resulting in a response.
17
- * @deprecated
18
- */
19
- export const setCatchHandler = (handler: RouteHandler): void => {
20
- getSingletonRouter().setCatchHandler(handler);
21
- };
@@ -1,24 +0,0 @@
1
- /*
2
- Copyright 2019 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 { RouteHandler } from "../types.js";
10
- import { getSingletonRouter } from "./singletonRouter.js";
11
-
12
- /**
13
- * Defines a default handler that's called when no routes explicitly
14
- * match the incoming request.
15
- *
16
- * Without a default handler, unmatched requests will go against the
17
- * network as if there were no service worker present.
18
- *
19
- * @param handler A callback function that returns a promise resulting in a response.
20
- * @deprecated
21
- */
22
- export const setDefaultHandler = (handler: RouteHandler): void => {
23
- getSingletonRouter().setDefaultHandler(handler);
24
- };