@serwist/sw 9.0.0-preview.2 → 9.0.0-preview.20

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 (262) hide show
  1. package/dist/abstractions/Serwist.d.ts +82 -0
  2. package/dist/abstractions/Serwist.d.ts.map +1 -0
  3. package/dist/{disableDevLogs.d.ts → abstractions/disableDevLogs.d.ts} +2 -0
  4. package/dist/abstractions/disableDevLogs.d.ts.map +1 -0
  5. package/dist/{fallbacks.d.ts → abstractions/fallbacks.d.ts} +9 -20
  6. package/dist/abstractions/fallbacks.d.ts.map +1 -0
  7. package/dist/{handlePrecaching.d.ts → abstractions/handlePrecaching.d.ts} +8 -10
  8. package/dist/abstractions/handlePrecaching.d.ts.map +1 -0
  9. package/dist/abstractions/installSerwist.d.ts +15 -0
  10. package/dist/abstractions/installSerwist.d.ts.map +1 -0
  11. package/dist/abstractions/navigationPreload.d.ts +20 -0
  12. package/dist/abstractions/navigationPreload.d.ts.map +1 -0
  13. package/dist/{registerRuntimeCaching.d.ts → abstractions/registerRuntimeCaching.d.ts} +2 -0
  14. package/dist/abstractions/registerRuntimeCaching.d.ts.map +1 -0
  15. package/dist/abstractions/types.d.ts +29 -0
  16. package/dist/abstractions/types.d.ts.map +1 -0
  17. package/dist/chunks/NavigationRoute.js +54 -0
  18. package/dist/chunks/NetworkOnly.js +193 -0
  19. package/dist/chunks/PrecacheFallbackPlugin.js +573 -0
  20. package/dist/chunks/Strategy.js +410 -0
  21. package/dist/chunks/precacheAndRoute.js +113 -0
  22. package/dist/chunks/registerRoute.js +7 -0
  23. package/dist/chunks/singletonPrecacheController.js +433 -0
  24. package/dist/chunks/singletonRouter.js +435 -0
  25. package/dist/index.d.ts +11 -9
  26. package/dist/index.d.ts.map +1 -1
  27. package/dist/index.js +121 -124
  28. package/dist/index.plugins.d.ts +41 -0
  29. package/dist/index.plugins.d.ts.map +1 -0
  30. package/dist/index.plugins.js +671 -0
  31. package/dist/index.precaching.d.ts +25 -0
  32. package/dist/index.precaching.d.ts.map +1 -0
  33. package/dist/index.precaching.js +24 -0
  34. package/dist/index.routing.d.ts +15 -0
  35. package/dist/index.routing.d.ts.map +1 -0
  36. package/dist/index.routing.js +19 -0
  37. package/dist/index.strategies.d.ts +22 -0
  38. package/dist/index.strategies.d.ts.map +1 -0
  39. package/dist/index.strategies.js +146 -0
  40. package/dist/plugins/backgroundSync/BackgroundSyncPlugin.d.ts +23 -0
  41. package/dist/plugins/backgroundSync/BackgroundSyncPlugin.d.ts.map +1 -0
  42. package/dist/plugins/backgroundSync/Queue.d.ts +166 -0
  43. package/dist/plugins/backgroundSync/Queue.d.ts.map +1 -0
  44. package/dist/plugins/backgroundSync/QueueDb.d.ts +90 -0
  45. package/dist/plugins/backgroundSync/QueueDb.d.ts.map +1 -0
  46. package/dist/plugins/backgroundSync/QueueStore.d.ts +75 -0
  47. package/dist/plugins/backgroundSync/QueueStore.d.ts.map +1 -0
  48. package/dist/plugins/backgroundSync/StorableRequest.d.ts +51 -0
  49. package/dist/plugins/backgroundSync/StorableRequest.d.ts.map +1 -0
  50. package/dist/plugins/broadcastUpdate/BroadcastCacheUpdate.d.ts +45 -0
  51. package/dist/plugins/broadcastUpdate/BroadcastCacheUpdate.d.ts.map +1 -0
  52. package/dist/plugins/broadcastUpdate/BroadcastUpdatePlugin.d.ts +27 -0
  53. package/dist/plugins/broadcastUpdate/BroadcastUpdatePlugin.d.ts.map +1 -0
  54. package/dist/plugins/broadcastUpdate/constants.d.ts +5 -0
  55. package/dist/plugins/broadcastUpdate/constants.d.ts.map +1 -0
  56. package/dist/plugins/broadcastUpdate/responsesAreSame.d.ts +11 -0
  57. package/dist/plugins/broadcastUpdate/responsesAreSame.d.ts.map +1 -0
  58. package/dist/plugins/broadcastUpdate/types.d.ts +34 -0
  59. package/dist/plugins/broadcastUpdate/types.d.ts.map +1 -0
  60. package/dist/plugins/cacheableResponse/CacheableResponse.d.ts +40 -0
  61. package/dist/plugins/cacheableResponse/CacheableResponse.d.ts.map +1 -0
  62. package/dist/plugins/cacheableResponse/CacheableResponsePlugin.d.ts +27 -0
  63. package/dist/plugins/cacheableResponse/CacheableResponsePlugin.d.ts.map +1 -0
  64. package/dist/plugins/expiration/CacheExpiration.d.ts +66 -0
  65. package/dist/plugins/expiration/CacheExpiration.d.ts.map +1 -0
  66. package/dist/plugins/expiration/ExpirationPlugin.d.ts +116 -0
  67. package/dist/plugins/expiration/ExpirationPlugin.d.ts.map +1 -0
  68. package/dist/plugins/expiration/models/CacheTimestampsModel.d.ts +73 -0
  69. package/dist/plugins/expiration/models/CacheTimestampsModel.d.ts.map +1 -0
  70. package/dist/plugins/googleAnalytics/constants.d.ts +10 -0
  71. package/dist/plugins/googleAnalytics/constants.d.ts.map +1 -0
  72. package/dist/plugins/googleAnalytics/initialize.d.ts +34 -0
  73. package/dist/plugins/googleAnalytics/initialize.d.ts.map +1 -0
  74. package/dist/plugins/precaching/PrecacheFallbackPlugin.d.ts +54 -0
  75. package/dist/plugins/precaching/PrecacheFallbackPlugin.d.ts.map +1 -0
  76. package/dist/plugins/rangeRequests/RangeRequestsPlugin.d.ts +19 -0
  77. package/dist/plugins/rangeRequests/RangeRequestsPlugin.d.ts.map +1 -0
  78. package/dist/plugins/rangeRequests/createPartialResponse.d.ts +18 -0
  79. package/dist/plugins/rangeRequests/createPartialResponse.d.ts.map +1 -0
  80. package/dist/plugins/rangeRequests/utils/calculateEffectiveBoundaries.d.ts +14 -0
  81. package/dist/plugins/rangeRequests/utils/calculateEffectiveBoundaries.d.ts.map +1 -0
  82. package/dist/plugins/rangeRequests/utils/parseRangeHeader.d.ts +12 -0
  83. package/dist/plugins/rangeRequests/utils/parseRangeHeader.d.ts.map +1 -0
  84. package/dist/precaching/PrecacheController.d.ts +145 -0
  85. package/dist/precaching/PrecacheController.d.ts.map +1 -0
  86. package/dist/precaching/PrecacheRoute.d.ts +20 -0
  87. package/dist/precaching/PrecacheRoute.d.ts.map +1 -0
  88. package/dist/precaching/PrecacheStrategy.d.ts +68 -0
  89. package/dist/precaching/PrecacheStrategy.d.ts.map +1 -0
  90. package/dist/precaching/addPlugins.d.ts +8 -0
  91. package/dist/precaching/addPlugins.d.ts.map +1 -0
  92. package/dist/precaching/addRoute.d.ts +15 -0
  93. package/dist/precaching/addRoute.d.ts.map +1 -0
  94. package/dist/precaching/cleanupOutdatedCaches.d.ts +6 -0
  95. package/dist/precaching/cleanupOutdatedCaches.d.ts.map +1 -0
  96. package/dist/precaching/createHandlerBoundToURL.d.ts +17 -0
  97. package/dist/precaching/createHandlerBoundToURL.d.ts.map +1 -0
  98. package/dist/precaching/getCacheKeyForURL.d.ts +20 -0
  99. package/dist/precaching/getCacheKeyForURL.d.ts.map +1 -0
  100. package/dist/precaching/matchPrecache.d.ts +14 -0
  101. package/dist/precaching/matchPrecache.d.ts.map +1 -0
  102. package/dist/precaching/precache.d.ts +19 -0
  103. package/dist/precaching/precache.d.ts.map +1 -0
  104. package/dist/precaching/precacheAndRoute.d.ts +14 -0
  105. package/dist/precaching/precacheAndRoute.d.ts.map +1 -0
  106. package/dist/precaching/singletonPrecacheController.d.ts +38 -0
  107. package/dist/precaching/singletonPrecacheController.d.ts.map +1 -0
  108. package/dist/precaching/types.d.ts +37 -0
  109. package/dist/precaching/types.d.ts.map +1 -0
  110. package/dist/precaching/utils/PrecacheCacheKeyPlugin.d.ts +17 -0
  111. package/dist/precaching/utils/PrecacheCacheKeyPlugin.d.ts.map +1 -0
  112. package/dist/precaching/utils/PrecacheInstallReportPlugin.d.ts +15 -0
  113. package/dist/precaching/utils/PrecacheInstallReportPlugin.d.ts.map +1 -0
  114. package/dist/precaching/utils/createCacheKey.d.ts +16 -0
  115. package/dist/precaching/utils/createCacheKey.d.ts.map +1 -0
  116. package/dist/precaching/utils/deleteOutdatedCaches.d.ts +18 -0
  117. package/dist/precaching/utils/deleteOutdatedCaches.d.ts.map +1 -0
  118. package/dist/precaching/utils/generateURLVariations.d.ts +12 -0
  119. package/dist/precaching/utils/generateURLVariations.d.ts.map +1 -0
  120. package/dist/precaching/utils/getCacheKeyForURL.d.ts +14 -0
  121. package/dist/precaching/utils/getCacheKeyForURL.d.ts.map +1 -0
  122. package/dist/precaching/utils/printCleanupDetails.d.ts +6 -0
  123. package/dist/precaching/utils/printCleanupDetails.d.ts.map +1 -0
  124. package/dist/precaching/utils/printInstallDetails.d.ts +7 -0
  125. package/dist/precaching/utils/printInstallDetails.d.ts.map +1 -0
  126. package/dist/precaching/utils/removeIgnoredSearchParams.d.ts +12 -0
  127. package/dist/precaching/utils/removeIgnoredSearchParams.d.ts.map +1 -0
  128. package/dist/routing/NavigationRoute.d.ts +57 -0
  129. package/dist/routing/NavigationRoute.d.ts.map +1 -0
  130. package/dist/routing/RegExpRoute.d.ts +24 -0
  131. package/dist/routing/RegExpRoute.d.ts.map +1 -0
  132. package/dist/routing/Route.d.ts +33 -0
  133. package/dist/routing/Route.d.ts.map +1 -0
  134. package/dist/routing/Router.d.ts +150 -0
  135. package/dist/routing/Router.d.ts.map +1 -0
  136. package/dist/routing/parseRoute.d.ts +16 -0
  137. package/dist/routing/parseRoute.d.ts.map +1 -0
  138. package/dist/routing/registerRoute.d.ts +15 -0
  139. package/dist/routing/registerRoute.d.ts.map +1 -0
  140. package/dist/routing/setCatchHandler.d.ts +9 -0
  141. package/dist/routing/setCatchHandler.d.ts.map +1 -0
  142. package/dist/routing/setDefaultHandler.d.ts +12 -0
  143. package/dist/routing/setDefaultHandler.d.ts.map +1 -0
  144. package/dist/routing/singletonRouter.d.ts +47 -0
  145. package/dist/routing/singletonRouter.d.ts.map +1 -0
  146. package/dist/routing/unregisterRoute.d.ts +8 -0
  147. package/dist/routing/unregisterRoute.d.ts.map +1 -0
  148. package/dist/routing/utils/constants.d.ts +15 -0
  149. package/dist/routing/utils/constants.d.ts.map +1 -0
  150. package/dist/routing/utils/normalizeHandler.d.ts +10 -0
  151. package/dist/routing/utils/normalizeHandler.d.ts.map +1 -0
  152. package/dist/strategies/CacheFirst.d.ts +23 -0
  153. package/dist/strategies/CacheFirst.d.ts.map +1 -0
  154. package/dist/strategies/CacheOnly.d.ts +20 -0
  155. package/dist/strategies/CacheOnly.d.ts.map +1 -0
  156. package/dist/strategies/NetworkFirst.d.ts +61 -0
  157. package/dist/strategies/NetworkFirst.d.ts.map +1 -0
  158. package/dist/strategies/NetworkOnly.d.ts +32 -0
  159. package/dist/strategies/NetworkOnly.d.ts.map +1 -0
  160. package/dist/strategies/StaleWhileRevalidate.d.ts +35 -0
  161. package/dist/strategies/StaleWhileRevalidate.d.ts.map +1 -0
  162. package/dist/strategies/Strategy.d.ts +83 -0
  163. package/dist/strategies/Strategy.d.ts.map +1 -0
  164. package/dist/strategies/StrategyHandler.d.ts +189 -0
  165. package/dist/strategies/StrategyHandler.d.ts.map +1 -0
  166. package/dist/strategies/plugins/cacheOkAndOpaquePlugin.d.ts +3 -0
  167. package/dist/strategies/plugins/cacheOkAndOpaquePlugin.d.ts.map +1 -0
  168. package/dist/strategies/utils/messages.d.ts +5 -0
  169. package/dist/strategies/utils/messages.d.ts.map +1 -0
  170. package/package.json +42 -19
  171. package/src/abstractions/Serwist.ts +177 -0
  172. package/src/abstractions/disableDevLogs.ts +10 -0
  173. package/src/abstractions/fallbacks.ts +65 -0
  174. package/src/abstractions/handlePrecaching.ts +65 -0
  175. package/src/abstractions/installSerwist.ts +28 -0
  176. package/src/abstractions/navigationPreload.ts +64 -0
  177. package/src/abstractions/registerRuntimeCaching.ts +17 -0
  178. package/src/abstractions/types.ts +29 -0
  179. package/src/index.plugins.ts +95 -0
  180. package/src/index.precaching.ts +41 -0
  181. package/src/index.routing.ts +28 -0
  182. package/src/index.strategies.ts +26 -0
  183. package/src/index.ts +31 -9
  184. package/src/plugins/backgroundSync/BackgroundSyncPlugin.ts +39 -0
  185. package/src/plugins/backgroundSync/Queue.ts +438 -0
  186. package/src/plugins/backgroundSync/QueueDb.ts +176 -0
  187. package/src/plugins/backgroundSync/QueueStore.ts +161 -0
  188. package/src/plugins/backgroundSync/StorableRequest.ts +142 -0
  189. package/src/plugins/broadcastUpdate/BroadcastCacheUpdate.ts +159 -0
  190. package/src/plugins/broadcastUpdate/BroadcastUpdatePlugin.ts +43 -0
  191. package/src/plugins/broadcastUpdate/constants.ts +12 -0
  192. package/src/plugins/broadcastUpdate/responsesAreSame.ts +48 -0
  193. package/src/plugins/broadcastUpdate/types.ts +37 -0
  194. package/src/plugins/cacheableResponse/CacheableResponse.ts +141 -0
  195. package/src/plugins/cacheableResponse/CacheableResponsePlugin.ts +46 -0
  196. package/src/plugins/expiration/CacheExpiration.ts +192 -0
  197. package/src/plugins/expiration/ExpirationPlugin.ts +297 -0
  198. package/src/plugins/expiration/models/CacheTimestampsModel.ts +184 -0
  199. package/src/plugins/googleAnalytics/constants.ts +22 -0
  200. package/src/plugins/googleAnalytics/initialize.ts +213 -0
  201. package/src/plugins/precaching/PrecacheFallbackPlugin.ts +86 -0
  202. package/src/plugins/rangeRequests/RangeRequestsPlugin.ts +39 -0
  203. package/src/plugins/rangeRequests/createPartialResponse.ts +92 -0
  204. package/src/plugins/rangeRequests/utils/calculateEffectiveBoundaries.ts +58 -0
  205. package/src/plugins/rangeRequests/utils/parseRangeHeader.ts +54 -0
  206. package/src/precaching/PrecacheController.ts +332 -0
  207. package/src/precaching/PrecacheRoute.ts +50 -0
  208. package/src/precaching/PrecacheStrategy.ts +238 -0
  209. package/src/precaching/addPlugins.ts +21 -0
  210. package/src/precaching/addRoute.ts +30 -0
  211. package/src/precaching/cleanupOutdatedCaches.ts +33 -0
  212. package/src/precaching/createHandlerBoundToURL.ts +30 -0
  213. package/src/precaching/getCacheKeyForURL.ts +33 -0
  214. package/src/precaching/matchPrecache.ts +25 -0
  215. package/src/precaching/precache.ts +31 -0
  216. package/src/precaching/precacheAndRoute.ts +27 -0
  217. package/src/precaching/singletonPrecacheController.ts +57 -0
  218. package/src/precaching/types.ts +46 -0
  219. package/src/precaching/utils/PrecacheCacheKeyPlugin.ts +36 -0
  220. package/src/precaching/utils/PrecacheInstallReportPlugin.ts +49 -0
  221. package/src/precaching/utils/createCacheKey.ts +68 -0
  222. package/src/precaching/utils/deleteOutdatedCaches.ts +40 -0
  223. package/src/precaching/utils/generateURLVariations.ts +55 -0
  224. package/src/precaching/utils/getCacheKeyForURL.ts +36 -0
  225. package/src/precaching/utils/printCleanupDetails.ts +38 -0
  226. package/src/precaching/utils/printInstallDetails.ts +53 -0
  227. package/src/precaching/utils/removeIgnoredSearchParams.ts +29 -0
  228. package/src/routing/NavigationRoute.ts +119 -0
  229. package/src/routing/RegExpRoute.ts +74 -0
  230. package/src/routing/Route.ts +68 -0
  231. package/src/routing/Router.ts +481 -0
  232. package/src/routing/parseRoute.ts +78 -0
  233. package/src/routing/registerRoute.ts +27 -0
  234. package/src/routing/setCatchHandler.ts +21 -0
  235. package/src/routing/setDefaultHandler.ts +24 -0
  236. package/src/routing/singletonRouter.ts +76 -0
  237. package/src/routing/unregisterRoute.ts +11 -0
  238. package/src/routing/utils/constants.ts +24 -0
  239. package/src/routing/utils/normalizeHandler.ts +40 -0
  240. package/src/strategies/CacheFirst.ts +87 -0
  241. package/src/strategies/CacheOnly.ts +58 -0
  242. package/src/strategies/NetworkFirst.ts +228 -0
  243. package/src/strategies/NetworkOnly.ts +96 -0
  244. package/src/strategies/StaleWhileRevalidate.ts +109 -0
  245. package/src/strategies/Strategy.ts +202 -0
  246. package/src/strategies/StrategyHandler.ts +557 -0
  247. package/src/strategies/plugins/cacheOkAndOpaquePlugin.ts +26 -0
  248. package/src/strategies/utils/messages.ts +20 -0
  249. package/dist/disableDevLogs.d.ts.map +0 -1
  250. package/dist/fallbacks.d.ts.map +0 -1
  251. package/dist/handlePrecaching.d.ts.map +0 -1
  252. package/dist/installSerwist.d.ts +0 -21
  253. package/dist/installSerwist.d.ts.map +0 -1
  254. package/dist/registerRuntimeCaching.d.ts.map +0 -1
  255. package/dist/types.d.ts +0 -110
  256. package/dist/types.d.ts.map +0 -1
  257. package/src/disableDevLogs.ts +0 -10
  258. package/src/fallbacks.ts +0 -83
  259. package/src/handlePrecaching.ts +0 -65
  260. package/src/installSerwist.ts +0 -106
  261. package/src/registerRuntimeCaching.ts +0 -84
  262. package/src/types.ts +0 -113
@@ -0,0 +1,30 @@
1
+ /*
2
+ Copyright 2019 Google LLC
3
+ Use of this source code is governed by an MIT-style
4
+ license that can be found in the LICENSE file or at
5
+ https://opensource.org/licenses/MIT.
6
+ */
7
+
8
+ import { registerRoute } from "../routing/registerRoute.js";
9
+ import { PrecacheRoute } from "./PrecacheRoute.js";
10
+ import { getSingletonPrecacheController } from "./singletonPrecacheController.js";
11
+ import type { PrecacheRouteOptions } from "./types.js";
12
+
13
+ /**
14
+ * Add a `fetch` listener to the service worker that will
15
+ * respond to
16
+ * [network requests](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers#Custom_responses_to_requests)
17
+ * with precached assets.
18
+ *
19
+ * Requests for assets that aren't precached, the `FetchEvent` will not be
20
+ * responded to, allowing the event to fall through to other `fetch` event
21
+ * listeners.
22
+ *
23
+ * @param options See the `@serwist/precaching.PrecacheRoute` options.
24
+ */
25
+ export const addRoute = (options?: PrecacheRouteOptions): void => {
26
+ const precacheController = getSingletonPrecacheController();
27
+
28
+ const precacheRoute = new PrecacheRoute(precacheController, options);
29
+ registerRoute(precacheRoute);
30
+ };
@@ -0,0 +1,33 @@
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 { logger, privateCacheNames } from "@serwist/core/internal";
10
+
11
+ import { deleteOutdatedCaches } from "./utils/deleteOutdatedCaches.js";
12
+
13
+ declare const self: ServiceWorkerGlobalScope;
14
+
15
+ /**
16
+ * Adds an `activate` event listener which will clean up incompatible
17
+ * precaches that were created by older versions of Serwist.
18
+ */
19
+ export const cleanupOutdatedCaches = (): void => {
20
+ self.addEventListener("activate", (event: ExtendableEvent) => {
21
+ const cacheName = privateCacheNames.getPrecacheName();
22
+
23
+ event.waitUntil(
24
+ deleteOutdatedCaches(cacheName).then((cachesDeleted) => {
25
+ if (process.env.NODE_ENV !== "production") {
26
+ if (cachesDeleted.length > 0) {
27
+ logger.log("The following out-of-date precaches were cleaned up automatically:", cachesDeleted);
28
+ }
29
+ }
30
+ }),
31
+ );
32
+ });
33
+ };
@@ -0,0 +1,30 @@
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 { RouteHandlerCallback } from "@serwist/core";
10
+
11
+ import { getSingletonPrecacheController } from "./singletonPrecacheController.js";
12
+
13
+ /**
14
+ * Helper function that calls `PrecacheController#createHandlerBoundToURL`
15
+ * on the default `PrecacheController` instance.
16
+ *
17
+ * If you are creating your own `PrecacheController`, then call the
18
+ * `PrecacheController#createHandlerBoundToURL` on that instance,
19
+ * instead of using this function.
20
+ *
21
+ * @param url The precached URL which will be used to lookup the
22
+ * `Response`.
23
+ * @param fallbackToNetwork Whether to attempt to get the
24
+ * response from the network if there's a precache miss.
25
+ * @return
26
+ */
27
+ export const createHandlerBoundToURL = (url: string): RouteHandlerCallback => {
28
+ const precacheController = getSingletonPrecacheController();
29
+ return precacheController.createHandlerBoundToURL(url);
30
+ };
@@ -0,0 +1,33 @@
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
+ */
28
+ function getCacheKeyForURL(url: string): string | undefined {
29
+ const precacheController = getSingletonPrecacheController();
30
+ return precacheController.getCacheKeyForURL(url);
31
+ }
32
+
33
+ export { getCacheKeyForURL };
@@ -0,0 +1,25 @@
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
+ * Helper function that calls `PrecacheController#matchPrecache`
13
+ * on the default `PrecacheController` instance.
14
+ *
15
+ * If you are creating your own `PrecacheController`, then call
16
+ * `PrecacheController#matchPrecache` on that instance,
17
+ * instead of using this function.
18
+ *
19
+ * @param request The key (without revisioning parameters)
20
+ * to look up in the precache.
21
+ * @returns
22
+ */
23
+ export const matchPrecache = (request: string | Request): Promise<Response | undefined> => {
24
+ return getSingletonPrecacheController().matchPrecache(request);
25
+ };
@@ -0,0 +1,31 @@
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 { PrecacheEntry } from "./types.js";
11
+
12
+ /**
13
+ * Adds items to the precache list, removing any duplicates and
14
+ * stores the files in the precache cache when the service
15
+ * worker installs.
16
+ *
17
+ * This method can be called multiple times.
18
+ *
19
+ * Please note: This method **will not** serve any of the cached files for you.
20
+ * It only precaches files. To respond to a network request you call
21
+ * `@serwist/precaching.addRoute`.
22
+ *
23
+ * If you have a single array of files to precache, you can just call
24
+ * `@serwist/precaching.precacheAndRoute`.
25
+ *
26
+ * @param entries Array of entries to precache.
27
+ */
28
+ export const precache = (entries: (PrecacheEntry | string)[]): void => {
29
+ const precacheController = getSingletonPrecacheController();
30
+ precacheController.precache(entries);
31
+ };
@@ -0,0 +1,27 @@
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 { addRoute } from "./addRoute.js";
10
+ import { precache } from "./precache.js";
11
+ import type { PrecacheEntry, PrecacheRouteOptions } from "./types.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
+ * `@serwist/precaching.precache` and
19
+ * `@serwist/precaching.addRoute` in a single call.
20
+ *
21
+ * @param entries Array of entries to precache.
22
+ * @param options See the `@serwist/precaching.PrecacheRoute` options.
23
+ */
24
+ export const precacheAndRoute = (entries: (PrecacheEntry | string)[], options?: PrecacheRouteOptions): void => {
25
+ precache(entries);
26
+ addRoute(options);
27
+ };
@@ -0,0 +1,57 @@
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 { PrecacheController } from "./PrecacheController.js";
10
+
11
+ let defaultPrecacheController: PrecacheController | undefined = undefined;
12
+
13
+ /**
14
+ * Creates a new, singleton `PrecacheController` if one does not exist. If one does
15
+ * already exist, that instance is returned. This instance is used by Serwist's
16
+ * `PrecacheController`-dependent functions and classes unless you provide a different
17
+ * `Router` to them.
18
+ *
19
+ * @returns The singleton `PrecacheController`.
20
+ */
21
+ export const getSingletonPrecacheController = (): PrecacheController => {
22
+ if (!defaultPrecacheController) {
23
+ defaultPrecacheController = new PrecacheController();
24
+ }
25
+ return defaultPrecacheController;
26
+ };
27
+
28
+ /**
29
+ * Changes the singleton `PrecacheController` to a different instance. This is meant for when you do not
30
+ * want to pass your own `PrecacheController` to every one of Serwist's `PrecacheController`-dependent
31
+ * functions and classes.
32
+ *
33
+ * It is highly recommended that you call this before anything else, if you plan on doing so.
34
+ *
35
+ * @example
36
+ * ```js
37
+ * import { PrecacheController, setSingletonPrecacheController } from "@serwist/sw/precaching";
38
+ *
39
+ * const controller = new PrecacheController();
40
+ *
41
+ * setSingletonPrecacheController(controller);
42
+ *
43
+ * // Do something with your controller...
44
+ *
45
+ * // This class now automatically picks up your `PrecacheController`! Without `setSingletonPrecacheController`,
46
+ * // you'd need to write this instead: `new Serwist({ controller })`
47
+ * const serwist = new Serwist();
48
+ *
49
+ * serwist.install();
50
+ * ```
51
+ * @param router
52
+ * @returns The new singleton `PrecacheController`.
53
+ */
54
+ export const setSingletonPrecacheController = (precacheController: PrecacheController): PrecacheController => {
55
+ defaultPrecacheController = precacheController;
56
+ return defaultPrecacheController;
57
+ };
@@ -0,0 +1,46 @@
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 interface InstallResult {
10
+ updatedURLs: string[];
11
+ notUpdatedURLs: string[];
12
+ }
13
+
14
+ export interface CleanupResult {
15
+ deletedCacheRequests: string[];
16
+ }
17
+
18
+ export declare interface PrecacheEntry {
19
+ integrity?: string;
20
+ url: string;
21
+ revision?: string | null;
22
+ }
23
+
24
+ export interface PrecacheRouteOptions {
25
+ /**
26
+ * The `directoryIndex` will check cache entries for a URL ending with '/'
27
+ * to see if there is a hit when appending the `directoryIndex` value.
28
+ */
29
+ directoryIndex?: string | null;
30
+ /**
31
+ * An array of RegExp's to remove search params when looking for a cache match.
32
+ */
33
+ ignoreURLParametersMatching?: RegExp[];
34
+ /**
35
+ * The `cleanURLs` option will check the cache for the URL with a `.html` added
36
+ * to the end of the end.
37
+ */
38
+ cleanURLs?: boolean;
39
+ /**
40
+ * This is a function that should take a URL and return an array of
41
+ * alternative URLs that should be checked for precache matches.
42
+ */
43
+ urlManipulation?: UrlManipulation;
44
+ }
45
+
46
+ export type UrlManipulation = ({ url }: { url: URL }) => URL[];
@@ -0,0 +1,36 @@
1
+ /*
2
+ Copyright 2020 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 { SerwistPlugin, SerwistPluginCallbackParam } from "@serwist/core";
10
+
11
+ import type { PrecacheController } from "../PrecacheController.js";
12
+
13
+ /**
14
+ * A plugin, designed to be used with PrecacheController, to translate URLs into
15
+ * the corresponding cache key, based on the current revision info.
16
+ *
17
+ * @private
18
+ */
19
+ class PrecacheCacheKeyPlugin implements SerwistPlugin {
20
+ private readonly _precacheController: PrecacheController;
21
+
22
+ constructor({ precacheController }: { precacheController: PrecacheController }) {
23
+ this._precacheController = precacheController;
24
+ }
25
+
26
+ cacheKeyWillBeUsed: SerwistPlugin["cacheKeyWillBeUsed"] = async ({ request, params }: SerwistPluginCallbackParam["cacheKeyWillBeUsed"]) => {
27
+ // Params is type any, can't change right now.
28
+ /* eslint-disable */
29
+ const cacheKey = params?.cacheKey || this._precacheController.getCacheKeyForURL(request.url);
30
+ /* eslint-enable */
31
+
32
+ return cacheKey ? new Request(cacheKey, { headers: request.headers }) : request;
33
+ };
34
+ }
35
+
36
+ export { PrecacheCacheKeyPlugin };
@@ -0,0 +1,49 @@
1
+ /*
2
+ Copyright 2020 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 { SerwistPlugin, SerwistPluginCallbackParam } from "@serwist/core";
10
+
11
+ /**
12
+ * A plugin, designed to be used with PrecacheController, to determine the
13
+ * of assets that were updated (or not updated) during the install event.
14
+ *
15
+ * @private
16
+ */
17
+ class PrecacheInstallReportPlugin implements SerwistPlugin {
18
+ updatedURLs: string[] = [];
19
+ notUpdatedURLs: string[] = [];
20
+
21
+ handlerWillStart: SerwistPlugin["handlerWillStart"] = async ({ request, state }: SerwistPluginCallbackParam["handlerWillStart"]) => {
22
+ // TODO: `state` should never be undefined...
23
+ if (state) {
24
+ state.originalRequest = request;
25
+ }
26
+ };
27
+
28
+ cachedResponseWillBeUsed: SerwistPlugin["cachedResponseWillBeUsed"] = async ({
29
+ event,
30
+ state,
31
+ cachedResponse,
32
+ }: SerwistPluginCallbackParam["cachedResponseWillBeUsed"]) => {
33
+ if (event.type === "install") {
34
+ if (state?.originalRequest && state.originalRequest instanceof Request) {
35
+ // TODO: `state` should never be undefined...
36
+ const url = state.originalRequest.url;
37
+
38
+ if (cachedResponse) {
39
+ this.notUpdatedURLs.push(url);
40
+ } else {
41
+ this.updatedURLs.push(url);
42
+ }
43
+ }
44
+ }
45
+ return cachedResponse;
46
+ };
47
+ }
48
+
49
+ export { PrecacheInstallReportPlugin };
@@ -0,0 +1,68 @@
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 { SerwistError } from "@serwist/core/internal";
10
+
11
+ import type { PrecacheEntry } from "../types.js";
12
+
13
+ interface CacheKey {
14
+ cacheKey: string;
15
+ url: string;
16
+ }
17
+
18
+ // Name of the search parameter used to store revision info.
19
+ const REVISION_SEARCH_PARAM = "__WB_REVISION__";
20
+
21
+ /**
22
+ * Converts a manifest entry into a versioned URL suitable for precaching.
23
+ *
24
+ * @param entry
25
+ * @returns A URL with versioning info.
26
+ *
27
+ * @private
28
+ */
29
+ export function createCacheKey(entry: PrecacheEntry | string): CacheKey {
30
+ if (!entry) {
31
+ throw new SerwistError("add-to-cache-list-unexpected-type", { entry });
32
+ }
33
+
34
+ // If a precache manifest entry is a string, it's assumed to be a versioned
35
+ // URL, like '/app.abcd1234.js'. Return as-is.
36
+ if (typeof entry === "string") {
37
+ const urlObject = new URL(entry, location.href);
38
+ return {
39
+ cacheKey: urlObject.href,
40
+ url: urlObject.href,
41
+ };
42
+ }
43
+
44
+ const { revision, url } = entry;
45
+ if (!url) {
46
+ throw new SerwistError("add-to-cache-list-unexpected-type", { entry });
47
+ }
48
+
49
+ // If there's just a URL and no revision, then it's also assumed to be a
50
+ // versioned URL.
51
+ if (!revision) {
52
+ const urlObject = new URL(url, location.href);
53
+ return {
54
+ cacheKey: urlObject.href,
55
+ url: urlObject.href,
56
+ };
57
+ }
58
+
59
+ // Otherwise, construct a properly versioned URL using the custom Serwist
60
+ // search parameter along with the revision info.
61
+ const cacheKeyURL = new URL(url, location.href);
62
+ const originalURL = new URL(url, location.href);
63
+ cacheKeyURL.searchParams.set(REVISION_SEARCH_PARAM, revision);
64
+ return {
65
+ cacheKey: cacheKeyURL.href,
66
+ url: originalURL.href,
67
+ };
68
+ }
@@ -0,0 +1,40 @@
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
+ // Give TypeScript the correct global.
10
+ declare let self: ServiceWorkerGlobalScope;
11
+
12
+ const SUBSTRING_TO_FIND = "-precache-";
13
+
14
+ /**
15
+ * Cleans up incompatible precaches that were created by older versions of
16
+ * Serwist, by a service worker registered under the current scope.
17
+ *
18
+ * This is meant to be called as part of the `activate` event.
19
+ *
20
+ * This should be safe to use as long as you don't include `substringToFind`
21
+ * (defaulting to `-precache-`) in your non-precache cache names.
22
+ *
23
+ * @param currentPrecacheName The cache name currently in use for
24
+ * precaching. This cache won't be deleted.
25
+ * @param substringToFind Cache names which include this
26
+ * substring will be deleted (excluding `currentPrecacheName`).
27
+ * @returns A list of all the cache names that were deleted.
28
+ * @private
29
+ */
30
+ export const deleteOutdatedCaches = async (currentPrecacheName: string, substringToFind: string = SUBSTRING_TO_FIND): Promise<string[]> => {
31
+ const cacheNames = await self.caches.keys();
32
+
33
+ const cacheNamesToDelete = cacheNames.filter((cacheName) => {
34
+ return cacheName.includes(substringToFind) && cacheName.includes(self.registration.scope) && cacheName !== currentPrecacheName;
35
+ });
36
+
37
+ await Promise.all(cacheNamesToDelete.map((cacheName) => self.caches.delete(cacheName)));
38
+
39
+ return cacheNamesToDelete;
40
+ };
@@ -0,0 +1,55 @@
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 { PrecacheRouteOptions } from "../types.js";
10
+ import { removeIgnoredSearchParams } from "./removeIgnoredSearchParams.js";
11
+
12
+ /**
13
+ * Generator function that yields possible variations on the original URL to
14
+ * check, one at a time.
15
+ *
16
+ * @param url
17
+ * @param options
18
+ *
19
+ * @private
20
+ */
21
+ export function* generateURLVariations(
22
+ url: string,
23
+ {
24
+ ignoreURLParametersMatching = [/^utm_/, /^fbclid$/],
25
+ directoryIndex = "index.html",
26
+ cleanURLs = true,
27
+ urlManipulation,
28
+ }: PrecacheRouteOptions = {},
29
+ ): Generator<string, void, unknown> {
30
+ const urlObject = new URL(url, location.href);
31
+ urlObject.hash = "";
32
+ yield urlObject.href;
33
+
34
+ const urlWithoutIgnoredParams = removeIgnoredSearchParams(urlObject, ignoreURLParametersMatching);
35
+ yield urlWithoutIgnoredParams.href;
36
+
37
+ if (directoryIndex && urlWithoutIgnoredParams.pathname.endsWith("/")) {
38
+ const directoryURL = new URL(urlWithoutIgnoredParams.href);
39
+ directoryURL.pathname += directoryIndex;
40
+ yield directoryURL.href;
41
+ }
42
+
43
+ if (cleanURLs) {
44
+ const cleanURL = new URL(urlWithoutIgnoredParams.href);
45
+ cleanURL.pathname += ".html";
46
+ yield cleanURL.href;
47
+ }
48
+
49
+ if (urlManipulation) {
50
+ const additionalURLs = urlManipulation({ url: urlObject });
51
+ for (const urlToAttempt of additionalURLs) {
52
+ yield urlToAttempt.href;
53
+ }
54
+ }
55
+ }
@@ -0,0 +1,36 @@
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 { PrecacheRouteOptions } from "../types.js";
11
+ import { generateURLVariations } from "./generateURLVariations.js";
12
+
13
+ /**
14
+ * This function will take the request URL and manipulate it based on the
15
+ * configuration options.
16
+ *
17
+ * @param url
18
+ * @param options
19
+ * @returns Returns the URL in the cache that matches the request,
20
+ * if possible.
21
+ *
22
+ * @private
23
+ */
24
+ export const getCacheKeyForURL = (url: string, options: PrecacheRouteOptions): string | undefined => {
25
+ const precacheController = getSingletonPrecacheController();
26
+
27
+ const urlsToCacheKeys = precacheController.getURLsToCacheKeys();
28
+ for (const possibleURL of generateURLVariations(url, options)) {
29
+ const possibleCacheKey = urlsToCacheKeys.get(possibleURL);
30
+ if (possibleCacheKey) {
31
+ return possibleCacheKey;
32
+ }
33
+ }
34
+
35
+ return undefined;
36
+ };
@@ -0,0 +1,38 @@
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 { logger } from "@serwist/core/internal";
10
+
11
+ /**
12
+ * @param groupTitle
13
+ * @param deletedURLs
14
+ *
15
+ * @private
16
+ */
17
+ const logGroup = (groupTitle: string, deletedURLs: string[]) => {
18
+ logger.groupCollapsed(groupTitle);
19
+
20
+ for (const url of deletedURLs) {
21
+ logger.log(url);
22
+ }
23
+
24
+ logger.groupEnd();
25
+ };
26
+
27
+ /**
28
+ * @param deletedURLs
29
+ * @private
30
+ */
31
+ export function printCleanupDetails(deletedURLs: string[]): void {
32
+ const deletionCount = deletedURLs.length;
33
+ if (deletionCount > 0) {
34
+ logger.groupCollapsed(`During precaching cleanup, ${deletionCount} cached request${deletionCount === 1 ? " was" : "s were"} deleted.`);
35
+ logGroup("Deleted Cache Requests", deletedURLs);
36
+ logger.groupEnd();
37
+ }
38
+ }