serwist 9.0.0-preview.24

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 (330) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1 -0
  3. package/dist/NavigationRoute.d.ts +57 -0
  4. package/dist/NavigationRoute.d.ts.map +1 -0
  5. package/dist/PrecacheRoute.d.ts +17 -0
  6. package/dist/PrecacheRoute.d.ts.map +1 -0
  7. package/dist/PrecacheStrategy.d.ts +66 -0
  8. package/dist/PrecacheStrategy.d.ts.map +1 -0
  9. package/dist/RegExpRoute.d.ts +24 -0
  10. package/dist/RegExpRoute.d.ts.map +1 -0
  11. package/dist/Route.d.ts +33 -0
  12. package/dist/Route.d.ts.map +1 -0
  13. package/dist/Serwist.d.ts +331 -0
  14. package/dist/Serwist.d.ts.map +1 -0
  15. package/dist/cacheNames.d.ts +20 -0
  16. package/dist/cacheNames.d.ts.map +1 -0
  17. package/dist/chunks/NetworkOnly.js +599 -0
  18. package/dist/chunks/PrecacheFallbackPlugin.js +634 -0
  19. package/dist/chunks/Serwist.js +1034 -0
  20. package/dist/chunks/registerQuotaErrorCallback.js +17 -0
  21. package/dist/chunks/resultingClientExists.js +32 -0
  22. package/dist/chunks/timeout.js +400 -0
  23. package/dist/chunks/waitUntil.js +24 -0
  24. package/dist/cleanupOutdatedCaches.d.ts +6 -0
  25. package/dist/cleanupOutdatedCaches.d.ts.map +1 -0
  26. package/dist/clientsClaim.d.ts +6 -0
  27. package/dist/clientsClaim.d.ts.map +1 -0
  28. package/dist/constants.d.ts +15 -0
  29. package/dist/constants.d.ts.map +1 -0
  30. package/dist/copyResponse.d.ts +20 -0
  31. package/dist/copyResponse.d.ts.map +1 -0
  32. package/dist/disableDevLogs.d.ts +7 -0
  33. package/dist/disableDevLogs.d.ts.map +1 -0
  34. package/dist/index.d.ts +21 -0
  35. package/dist/index.d.ts.map +1 -0
  36. package/dist/index.internal.d.ts +16 -0
  37. package/dist/index.internal.d.ts.map +1 -0
  38. package/dist/index.internal.js +24 -0
  39. package/dist/index.js +27 -0
  40. package/dist/index.legacy.d.ts +32 -0
  41. package/dist/index.legacy.d.ts.map +1 -0
  42. package/dist/index.legacy.js +640 -0
  43. package/dist/index.plugins.d.ts +41 -0
  44. package/dist/index.plugins.d.ts.map +1 -0
  45. package/dist/index.plugins.js +669 -0
  46. package/dist/index.strategies.d.ts +22 -0
  47. package/dist/index.strategies.d.ts.map +1 -0
  48. package/dist/index.strategies.js +144 -0
  49. package/dist/legacy/PrecacheController.d.ts +146 -0
  50. package/dist/legacy/PrecacheController.d.ts.map +1 -0
  51. package/dist/legacy/PrecacheFallbackPlugin.d.ts +62 -0
  52. package/dist/legacy/PrecacheFallbackPlugin.d.ts.map +1 -0
  53. package/dist/legacy/PrecacheRoute.d.ts +19 -0
  54. package/dist/legacy/PrecacheRoute.d.ts.map +1 -0
  55. package/dist/legacy/Router.d.ts +151 -0
  56. package/dist/legacy/Router.d.ts.map +1 -0
  57. package/dist/legacy/addPlugins.d.ts +9 -0
  58. package/dist/legacy/addPlugins.d.ts.map +1 -0
  59. package/dist/legacy/addRoute.d.ts +16 -0
  60. package/dist/legacy/addRoute.d.ts.map +1 -0
  61. package/dist/legacy/createHandlerBoundToURL.d.ts +18 -0
  62. package/dist/legacy/createHandlerBoundToURL.d.ts.map +1 -0
  63. package/dist/legacy/fallbacks.d.ts +59 -0
  64. package/dist/legacy/fallbacks.d.ts.map +1 -0
  65. package/dist/legacy/getCacheKeyForURL.d.ts +20 -0
  66. package/dist/legacy/getCacheKeyForURL.d.ts.map +1 -0
  67. package/dist/legacy/handlePrecaching.d.ts +54 -0
  68. package/dist/legacy/handlePrecaching.d.ts.map +1 -0
  69. package/dist/legacy/installSerwist.d.ts +15 -0
  70. package/dist/legacy/installSerwist.d.ts.map +1 -0
  71. package/dist/legacy/matchPrecache.d.ts +15 -0
  72. package/dist/legacy/matchPrecache.d.ts.map +1 -0
  73. package/dist/legacy/precache.d.ts +20 -0
  74. package/dist/legacy/precache.d.ts.map +1 -0
  75. package/dist/legacy/precacheAndRoute.d.ts +15 -0
  76. package/dist/legacy/precacheAndRoute.d.ts.map +1 -0
  77. package/dist/legacy/registerRoute.d.ts +16 -0
  78. package/dist/legacy/registerRoute.d.ts.map +1 -0
  79. package/dist/legacy/registerRuntimeCaching.d.ts +11 -0
  80. package/dist/legacy/registerRuntimeCaching.d.ts.map +1 -0
  81. package/dist/legacy/setCatchHandler.d.ts +10 -0
  82. package/dist/legacy/setCatchHandler.d.ts.map +1 -0
  83. package/dist/legacy/setDefaultHandler.d.ts +13 -0
  84. package/dist/legacy/setDefaultHandler.d.ts.map +1 -0
  85. package/dist/legacy/singletonPrecacheController.d.ts +34 -0
  86. package/dist/legacy/singletonPrecacheController.d.ts.map +1 -0
  87. package/dist/legacy/singletonRouter.d.ts +41 -0
  88. package/dist/legacy/singletonRouter.d.ts.map +1 -0
  89. package/dist/legacy/unregisterRoute.d.ts +9 -0
  90. package/dist/legacy/unregisterRoute.d.ts.map +1 -0
  91. package/dist/legacy/utils/PrecacheCacheKeyPlugin.d.ts +16 -0
  92. package/dist/legacy/utils/PrecacheCacheKeyPlugin.d.ts.map +1 -0
  93. package/dist/legacy/utils/getCacheKeyForURL.d.ts +14 -0
  94. package/dist/legacy/utils/getCacheKeyForURL.d.ts.map +1 -0
  95. package/dist/models/messages/messageGenerator.d.ts +4 -0
  96. package/dist/models/messages/messageGenerator.d.ts.map +1 -0
  97. package/dist/models/messages/messages.d.ts +44 -0
  98. package/dist/models/messages/messages.d.ts.map +1 -0
  99. package/dist/models/pluginEvents.d.ts +10 -0
  100. package/dist/models/pluginEvents.d.ts.map +1 -0
  101. package/dist/models/quotaErrorCallbacks.d.ts +3 -0
  102. package/dist/models/quotaErrorCallbacks.d.ts.map +1 -0
  103. package/dist/navigationPreload.d.ts +24 -0
  104. package/dist/navigationPreload.d.ts.map +1 -0
  105. package/dist/parseRoute.d.ts +16 -0
  106. package/dist/parseRoute.d.ts.map +1 -0
  107. package/dist/plugins/backgroundSync/BackgroundSyncPlugin.d.ts +23 -0
  108. package/dist/plugins/backgroundSync/BackgroundSyncPlugin.d.ts.map +1 -0
  109. package/dist/plugins/backgroundSync/Queue.d.ts +166 -0
  110. package/dist/plugins/backgroundSync/Queue.d.ts.map +1 -0
  111. package/dist/plugins/backgroundSync/QueueDb.d.ts +90 -0
  112. package/dist/plugins/backgroundSync/QueueDb.d.ts.map +1 -0
  113. package/dist/plugins/backgroundSync/QueueStore.d.ts +75 -0
  114. package/dist/plugins/backgroundSync/QueueStore.d.ts.map +1 -0
  115. package/dist/plugins/backgroundSync/StorableRequest.d.ts +51 -0
  116. package/dist/plugins/backgroundSync/StorableRequest.d.ts.map +1 -0
  117. package/dist/plugins/broadcastUpdate/BroadcastCacheUpdate.d.ts +45 -0
  118. package/dist/plugins/broadcastUpdate/BroadcastCacheUpdate.d.ts.map +1 -0
  119. package/dist/plugins/broadcastUpdate/BroadcastUpdatePlugin.d.ts +27 -0
  120. package/dist/plugins/broadcastUpdate/BroadcastUpdatePlugin.d.ts.map +1 -0
  121. package/dist/plugins/broadcastUpdate/constants.d.ts +5 -0
  122. package/dist/plugins/broadcastUpdate/constants.d.ts.map +1 -0
  123. package/dist/plugins/broadcastUpdate/responsesAreSame.d.ts +11 -0
  124. package/dist/plugins/broadcastUpdate/responsesAreSame.d.ts.map +1 -0
  125. package/dist/plugins/broadcastUpdate/types.d.ts +34 -0
  126. package/dist/plugins/broadcastUpdate/types.d.ts.map +1 -0
  127. package/dist/plugins/cacheableResponse/CacheableResponse.d.ts +40 -0
  128. package/dist/plugins/cacheableResponse/CacheableResponse.d.ts.map +1 -0
  129. package/dist/plugins/cacheableResponse/CacheableResponsePlugin.d.ts +27 -0
  130. package/dist/plugins/cacheableResponse/CacheableResponsePlugin.d.ts.map +1 -0
  131. package/dist/plugins/expiration/CacheExpiration.d.ts +66 -0
  132. package/dist/plugins/expiration/CacheExpiration.d.ts.map +1 -0
  133. package/dist/plugins/expiration/ExpirationPlugin.d.ts +116 -0
  134. package/dist/plugins/expiration/ExpirationPlugin.d.ts.map +1 -0
  135. package/dist/plugins/expiration/models/CacheTimestampsModel.d.ts +73 -0
  136. package/dist/plugins/expiration/models/CacheTimestampsModel.d.ts.map +1 -0
  137. package/dist/plugins/googleAnalytics/constants.d.ts +10 -0
  138. package/dist/plugins/googleAnalytics/constants.d.ts.map +1 -0
  139. package/dist/plugins/googleAnalytics/initialize.d.ts +30 -0
  140. package/dist/plugins/googleAnalytics/initialize.d.ts.map +1 -0
  141. package/dist/plugins/precaching/PrecacheFallbackPlugin.d.ts +53 -0
  142. package/dist/plugins/precaching/PrecacheFallbackPlugin.d.ts.map +1 -0
  143. package/dist/plugins/rangeRequests/RangeRequestsPlugin.d.ts +19 -0
  144. package/dist/plugins/rangeRequests/RangeRequestsPlugin.d.ts.map +1 -0
  145. package/dist/plugins/rangeRequests/createPartialResponse.d.ts +18 -0
  146. package/dist/plugins/rangeRequests/createPartialResponse.d.ts.map +1 -0
  147. package/dist/plugins/rangeRequests/utils/calculateEffectiveBoundaries.d.ts +14 -0
  148. package/dist/plugins/rangeRequests/utils/calculateEffectiveBoundaries.d.ts.map +1 -0
  149. package/dist/plugins/rangeRequests/utils/parseRangeHeader.d.ts +12 -0
  150. package/dist/plugins/rangeRequests/utils/parseRangeHeader.d.ts.map +1 -0
  151. package/dist/registerQuotaErrorCallback.d.ts +8 -0
  152. package/dist/registerQuotaErrorCallback.d.ts.map +1 -0
  153. package/dist/setCacheNameDetails.d.ts +9 -0
  154. package/dist/setCacheNameDetails.d.ts.map +1 -0
  155. package/dist/strategies/CacheFirst.d.ts +23 -0
  156. package/dist/strategies/CacheFirst.d.ts.map +1 -0
  157. package/dist/strategies/CacheOnly.d.ts +20 -0
  158. package/dist/strategies/CacheOnly.d.ts.map +1 -0
  159. package/dist/strategies/NetworkFirst.d.ts +61 -0
  160. package/dist/strategies/NetworkFirst.d.ts.map +1 -0
  161. package/dist/strategies/NetworkOnly.d.ts +32 -0
  162. package/dist/strategies/NetworkOnly.d.ts.map +1 -0
  163. package/dist/strategies/StaleWhileRevalidate.d.ts +35 -0
  164. package/dist/strategies/StaleWhileRevalidate.d.ts.map +1 -0
  165. package/dist/strategies/Strategy.d.ts +83 -0
  166. package/dist/strategies/Strategy.d.ts.map +1 -0
  167. package/dist/strategies/StrategyHandler.d.ts +189 -0
  168. package/dist/strategies/StrategyHandler.d.ts.map +1 -0
  169. package/dist/strategies/plugins/cacheOkAndOpaquePlugin.d.ts +3 -0
  170. package/dist/strategies/plugins/cacheOkAndOpaquePlugin.d.ts.map +1 -0
  171. package/dist/strategies/utils/messages.d.ts +5 -0
  172. package/dist/strategies/utils/messages.d.ts.map +1 -0
  173. package/dist/types.d.ts +317 -0
  174. package/dist/types.d.ts.map +1 -0
  175. package/dist/utils/Deferred.d.ts +19 -0
  176. package/dist/utils/Deferred.d.ts.map +1 -0
  177. package/dist/utils/PrecacheCacheKeyPlugin.d.ts +16 -0
  178. package/dist/utils/PrecacheCacheKeyPlugin.d.ts.map +1 -0
  179. package/dist/utils/PrecacheInstallReportPlugin.d.ts +14 -0
  180. package/dist/utils/PrecacheInstallReportPlugin.d.ts.map +1 -0
  181. package/dist/utils/SerwistError.d.ts +24 -0
  182. package/dist/utils/SerwistError.d.ts.map +1 -0
  183. package/dist/utils/assert.d.ts +11 -0
  184. package/dist/utils/assert.d.ts.map +1 -0
  185. package/dist/utils/cacheMatchIgnoreParams.d.ts +15 -0
  186. package/dist/utils/cacheMatchIgnoreParams.d.ts.map +1 -0
  187. package/dist/utils/cacheNames.d.ts +40 -0
  188. package/dist/utils/cacheNames.d.ts.map +1 -0
  189. package/dist/utils/canConstructReadableStream.d.ts +12 -0
  190. package/dist/utils/canConstructReadableStream.d.ts.map +1 -0
  191. package/dist/utils/canConstructResponseFromBodyStream.d.ts +11 -0
  192. package/dist/utils/canConstructResponseFromBodyStream.d.ts.map +1 -0
  193. package/dist/utils/createCacheKey.d.ts +16 -0
  194. package/dist/utils/createCacheKey.d.ts.map +1 -0
  195. package/dist/utils/deleteOutdatedCaches.d.ts +18 -0
  196. package/dist/utils/deleteOutdatedCaches.d.ts.map +1 -0
  197. package/dist/utils/dontWaitFor.d.ts +7 -0
  198. package/dist/utils/dontWaitFor.d.ts.map +1 -0
  199. package/dist/utils/executeQuotaErrorCallbacks.d.ts +8 -0
  200. package/dist/utils/executeQuotaErrorCallbacks.d.ts.map +1 -0
  201. package/dist/utils/generateURLVariations.d.ts +12 -0
  202. package/dist/utils/generateURLVariations.d.ts.map +1 -0
  203. package/dist/utils/getFriendlyURL.d.ts +3 -0
  204. package/dist/utils/getFriendlyURL.d.ts.map +1 -0
  205. package/dist/utils/logger.d.ts +24 -0
  206. package/dist/utils/logger.d.ts.map +1 -0
  207. package/dist/utils/normalizeHandler.d.ts +10 -0
  208. package/dist/utils/normalizeHandler.d.ts.map +1 -0
  209. package/dist/utils/pluginUtils.d.ts +5 -0
  210. package/dist/utils/pluginUtils.d.ts.map +1 -0
  211. package/dist/utils/printCleanupDetails.d.ts +6 -0
  212. package/dist/utils/printCleanupDetails.d.ts.map +1 -0
  213. package/dist/utils/printInstallDetails.d.ts +7 -0
  214. package/dist/utils/printInstallDetails.d.ts.map +1 -0
  215. package/dist/utils/removeIgnoredSearchParams.d.ts +12 -0
  216. package/dist/utils/removeIgnoredSearchParams.d.ts.map +1 -0
  217. package/dist/utils/resultingClientExists.d.ts +12 -0
  218. package/dist/utils/resultingClientExists.d.ts.map +1 -0
  219. package/dist/utils/timeout.d.ts +10 -0
  220. package/dist/utils/timeout.d.ts.map +1 -0
  221. package/dist/utils/waitUntil.d.ts +11 -0
  222. package/dist/utils/waitUntil.d.ts.map +1 -0
  223. package/dist/utils/welcome.d.ts +2 -0
  224. package/dist/utils/welcome.d.ts.map +1 -0
  225. package/package.json +85 -0
  226. package/src/NavigationRoute.ts +119 -0
  227. package/src/PrecacheRoute.ts +46 -0
  228. package/src/PrecacheStrategy.ts +239 -0
  229. package/src/RegExpRoute.ts +74 -0
  230. package/src/Route.ts +67 -0
  231. package/src/Serwist.ts +920 -0
  232. package/src/cacheNames.ts +39 -0
  233. package/src/cleanupOutdatedCaches.ts +32 -0
  234. package/src/clientsClaim.ts +18 -0
  235. package/src/constants.ts +24 -0
  236. package/src/copyResponse.ts +60 -0
  237. package/src/disableDevLogs.ts +10 -0
  238. package/src/index.internal.ts +33 -0
  239. package/src/index.legacy.ts +66 -0
  240. package/src/index.plugins.ts +95 -0
  241. package/src/index.strategies.ts +26 -0
  242. package/src/index.ts +39 -0
  243. package/src/legacy/PrecacheController.ts +337 -0
  244. package/src/legacy/PrecacheFallbackPlugin.ts +93 -0
  245. package/src/legacy/PrecacheRoute.ts +48 -0
  246. package/src/legacy/Router.ts +484 -0
  247. package/src/legacy/addPlugins.ts +21 -0
  248. package/src/legacy/addRoute.ts +29 -0
  249. package/src/legacy/createHandlerBoundToURL.ts +30 -0
  250. package/src/legacy/fallbacks.ts +94 -0
  251. package/src/legacy/getCacheKeyForURL.ts +32 -0
  252. package/src/legacy/handlePrecaching.ts +86 -0
  253. package/src/legacy/installSerwist.ts +19 -0
  254. package/src/legacy/matchPrecache.ts +26 -0
  255. package/src/legacy/precache.ts +31 -0
  256. package/src/legacy/precacheAndRoute.ts +28 -0
  257. package/src/legacy/registerRoute.ts +27 -0
  258. package/src/legacy/registerRuntimeCaching.ts +17 -0
  259. package/src/legacy/setCatchHandler.ts +21 -0
  260. package/src/legacy/setDefaultHandler.ts +24 -0
  261. package/src/legacy/singletonPrecacheController.ts +53 -0
  262. package/src/legacy/singletonRouter.ts +70 -0
  263. package/src/legacy/unregisterRoute.ts +12 -0
  264. package/src/legacy/utils/PrecacheCacheKeyPlugin.ts +33 -0
  265. package/src/legacy/utils/getCacheKeyForURL.ts +36 -0
  266. package/src/models/messages/messageGenerator.ts +29 -0
  267. package/src/models/messages/messages.ts +233 -0
  268. package/src/models/pluginEvents.ts +17 -0
  269. package/src/models/quotaErrorCallbacks.ts +13 -0
  270. package/src/navigationPreload.ts +68 -0
  271. package/src/parseRoute.ts +78 -0
  272. package/src/plugins/backgroundSync/BackgroundSyncPlugin.ts +38 -0
  273. package/src/plugins/backgroundSync/Queue.ts +440 -0
  274. package/src/plugins/backgroundSync/QueueDb.ts +176 -0
  275. package/src/plugins/backgroundSync/QueueStore.ts +160 -0
  276. package/src/plugins/backgroundSync/StorableRequest.ts +142 -0
  277. package/src/plugins/broadcastUpdate/BroadcastCacheUpdate.ts +161 -0
  278. package/src/plugins/broadcastUpdate/BroadcastUpdatePlugin.ts +42 -0
  279. package/src/plugins/broadcastUpdate/constants.ts +12 -0
  280. package/src/plugins/broadcastUpdate/responsesAreSame.ts +49 -0
  281. package/src/plugins/broadcastUpdate/types.ts +37 -0
  282. package/src/plugins/cacheableResponse/CacheableResponse.ts +144 -0
  283. package/src/plugins/cacheableResponse/CacheableResponsePlugin.ts +45 -0
  284. package/src/plugins/expiration/CacheExpiration.ts +193 -0
  285. package/src/plugins/expiration/ExpirationPlugin.ts +300 -0
  286. package/src/plugins/expiration/models/CacheTimestampsModel.ts +184 -0
  287. package/src/plugins/googleAnalytics/constants.ts +22 -0
  288. package/src/plugins/googleAnalytics/initialize.ts +209 -0
  289. package/src/plugins/precaching/PrecacheFallbackPlugin.ts +83 -0
  290. package/src/plugins/rangeRequests/RangeRequestsPlugin.ts +38 -0
  291. package/src/plugins/rangeRequests/createPartialResponse.ts +93 -0
  292. package/src/plugins/rangeRequests/utils/calculateEffectiveBoundaries.ts +59 -0
  293. package/src/plugins/rangeRequests/utils/parseRangeHeader.ts +55 -0
  294. package/src/registerQuotaErrorCallback.ts +34 -0
  295. package/src/setCacheNameDetails.ts +53 -0
  296. package/src/strategies/CacheFirst.ts +88 -0
  297. package/src/strategies/CacheOnly.ts +59 -0
  298. package/src/strategies/NetworkFirst.ts +229 -0
  299. package/src/strategies/NetworkOnly.ts +98 -0
  300. package/src/strategies/StaleWhileRevalidate.ts +110 -0
  301. package/src/strategies/Strategy.ts +204 -0
  302. package/src/strategies/StrategyHandler.ts +554 -0
  303. package/src/strategies/plugins/cacheOkAndOpaquePlugin.ts +26 -0
  304. package/src/strategies/utils/messages.ts +21 -0
  305. package/src/types.ts +358 -0
  306. package/src/utils/Deferred.ts +33 -0
  307. package/src/utils/PrecacheCacheKeyPlugin.ts +33 -0
  308. package/src/utils/PrecacheInstallReportPlugin.ts +47 -0
  309. package/src/utils/SerwistError.ts +41 -0
  310. package/src/utils/assert.ts +89 -0
  311. package/src/utils/cacheMatchIgnoreParams.ts +54 -0
  312. package/src/utils/cacheNames.ts +87 -0
  313. package/src/utils/canConstructReadableStream.ts +34 -0
  314. package/src/utils/canConstructResponseFromBodyStream.ts +37 -0
  315. package/src/utils/createCacheKey.ts +68 -0
  316. package/src/utils/deleteOutdatedCaches.ts +40 -0
  317. package/src/utils/dontWaitFor.ts +16 -0
  318. package/src/utils/executeQuotaErrorCallbacks.ts +33 -0
  319. package/src/utils/generateURLVariations.ts +55 -0
  320. package/src/utils/getFriendlyURL.ts +16 -0
  321. package/src/utils/logger.ts +95 -0
  322. package/src/utils/normalizeHandler.ts +40 -0
  323. package/src/utils/pluginUtils.ts +15 -0
  324. package/src/utils/printCleanupDetails.ts +38 -0
  325. package/src/utils/printInstallDetails.ts +53 -0
  326. package/src/utils/removeIgnoredSearchParams.ts +29 -0
  327. package/src/utils/resultingClientExists.ts +58 -0
  328. package/src/utils/timeout.ts +19 -0
  329. package/src/utils/waitUntil.ts +21 -0
  330. package/src/utils/welcome.ts +19 -0
@@ -0,0 +1,144 @@
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 { assert } from "../../utils/assert.js";
10
+ import { SerwistError } from "../../utils/SerwistError.js";
11
+ import { getFriendlyURL } from "../../utils/getFriendlyURL.js";
12
+ import { logger } from "../../utils/logger.js";
13
+
14
+ export interface CacheableResponseOptions {
15
+ /**
16
+ * One or more status codes that a `Response` can have to be considered cacheable.
17
+ */
18
+ statuses?: number[];
19
+ /**
20
+ * A mapping of header names and expected values that a `Response` can have and be
21
+ * considered cacheable. If multiple headers are provided, only one needs to be present.
22
+ */
23
+ headers?: HeadersInit;
24
+ }
25
+
26
+ /**
27
+ * Allows you to set up rules determining what status codes and/or headers need
28
+ * to be present in order for a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response)
29
+ * to be considered cacheable.
30
+ */
31
+ export class CacheableResponse {
32
+ private readonly _statuses?: CacheableResponseOptions["statuses"];
33
+ private readonly _headers?: Headers;
34
+
35
+ /**
36
+ * To construct a new CacheableResponse instance you must provide at least
37
+ * one of the `config` properties.
38
+ *
39
+ * If both `statuses` and `headers` are specified, then both conditions must
40
+ * be met for the `Response` to be considered cacheable.
41
+ *
42
+ * @param config
43
+ */
44
+ constructor(config: CacheableResponseOptions = {}) {
45
+ if (process.env.NODE_ENV !== "production") {
46
+ if (!(config.statuses || config.headers)) {
47
+ throw new SerwistError("statuses-or-headers-required", {
48
+ moduleName: "serwist/plugins",
49
+ className: "CacheableResponse",
50
+ funcName: "constructor",
51
+ });
52
+ }
53
+
54
+ if (config.statuses) {
55
+ assert!.isArray(config.statuses, {
56
+ moduleName: "serwist/plugins",
57
+ className: "CacheableResponse",
58
+ funcName: "constructor",
59
+ paramName: "config.statuses",
60
+ });
61
+ }
62
+
63
+ if (config.headers) {
64
+ assert!.isType(config.headers, "object", {
65
+ moduleName: "serwist/plugins",
66
+ className: "CacheableResponse",
67
+ funcName: "constructor",
68
+ paramName: "config.headers",
69
+ });
70
+ }
71
+ }
72
+
73
+ this._statuses = config.statuses;
74
+ if (config.headers) {
75
+ this._headers = new Headers(config.headers);
76
+ }
77
+ }
78
+
79
+ /**
80
+ * Checks a response to see whether it's cacheable or not.
81
+ *
82
+ * @param response The response whose cacheability is being
83
+ * checked.
84
+ * @returns `true` if the `Response` is cacheable, and `false`
85
+ * otherwise.
86
+ */
87
+ isResponseCacheable(response: Response): boolean {
88
+ if (process.env.NODE_ENV !== "production") {
89
+ assert!.isInstance(response, Response, {
90
+ moduleName: "serwist/plugins",
91
+ className: "CacheableResponse",
92
+ funcName: "isResponseCacheable",
93
+ paramName: "response",
94
+ });
95
+ }
96
+
97
+ let cacheable = true;
98
+
99
+ if (this._statuses) {
100
+ cacheable = this._statuses.includes(response.status);
101
+ }
102
+
103
+ if (this._headers && cacheable) {
104
+ for (const [headerName, headerValue] of this._headers.entries()) {
105
+ if (response.headers.get(headerName) !== headerValue) {
106
+ cacheable = false;
107
+ break;
108
+ }
109
+ }
110
+ }
111
+
112
+ if (process.env.NODE_ENV !== "production") {
113
+ if (!cacheable) {
114
+ logger.groupCollapsed(
115
+ `The request for '${getFriendlyURL(response.url)}' returned a response that does not meet the criteria for being cached.`,
116
+ );
117
+
118
+ logger.groupCollapsed("View cacheability criteria here.");
119
+ logger.log(`Cacheable statuses: ${JSON.stringify(this._statuses)}`);
120
+ logger.log(`Cacheable headers: ${JSON.stringify(this._headers, null, 2)}`);
121
+ logger.groupEnd();
122
+
123
+ const logFriendlyHeaders: { [key: string]: string } = {};
124
+ response.headers.forEach((value, key) => {
125
+ logFriendlyHeaders[key] = value;
126
+ });
127
+
128
+ logger.groupCollapsed("View response status and headers here.");
129
+ logger.log(`Response status: ${response.status}`);
130
+ logger.log(`Response headers: ${JSON.stringify(logFriendlyHeaders, null, 2)}`);
131
+ logger.groupEnd();
132
+
133
+ logger.groupCollapsed("View full response details here.");
134
+ logger.log(response.headers);
135
+ logger.log(response);
136
+ logger.groupEnd();
137
+
138
+ logger.groupEnd();
139
+ }
140
+ }
141
+
142
+ return cacheable;
143
+ }
144
+ }
@@ -0,0 +1,45 @@
1
+ /*
2
+ Copyright 2018 Google LLC
3
+
4
+ Use of this source code is governed by an MIT-style
5
+ license that can be found in the LICENSE file or at
6
+ https://opensource.org/licenses/MIT.
7
+ */
8
+
9
+ import type { SerwistPlugin } from "../../types.js";
10
+ import type { CacheableResponseOptions } from "./CacheableResponse.js";
11
+ import { CacheableResponse } from "./CacheableResponse.js";
12
+
13
+ /**
14
+ * A class implementing the `cacheWillUpdate` lifecycle callback. This makes it
15
+ * easier to add in cacheability checks to requests made via Serwist's built-in
16
+ * strategies.
17
+ */
18
+ export class CacheableResponsePlugin implements SerwistPlugin {
19
+ private readonly _cacheableResponse: CacheableResponse;
20
+
21
+ /**
22
+ * To construct a new CacheableResponsePlugin instance you must provide at
23
+ * least one of the `config` properties.
24
+ *
25
+ * If both `statuses` and `headers` are specified, then both conditions must
26
+ * be met for the `Response` to be considered cacheable.
27
+ *
28
+ * @param config
29
+ */
30
+ constructor(config: CacheableResponseOptions) {
31
+ this._cacheableResponse = new CacheableResponse(config);
32
+ }
33
+
34
+ /**
35
+ * @param options
36
+ * @returns
37
+ * @private
38
+ */
39
+ cacheWillUpdate: SerwistPlugin["cacheWillUpdate"] = async ({ response }) => {
40
+ if (this._cacheableResponse.isResponseCacheable(response)) {
41
+ return response;
42
+ }
43
+ return null;
44
+ };
45
+ }
@@ -0,0 +1,193 @@
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 { assert } from "../../utils/assert.js";
10
+ import { SerwistError } from "../../utils/SerwistError.js";
11
+ import { logger } from "../../utils/logger.js";
12
+ import { CacheTimestampsModel } from "./models/CacheTimestampsModel.js";
13
+
14
+ interface CacheExpirationConfig {
15
+ /**
16
+ * The maximum number of entries to cache. Entries used the least will
17
+ * be removed as the maximum is reached.
18
+ */
19
+ maxEntries?: number;
20
+ /**
21
+ * The maximum age of an entry before it's treated as stale and removed.
22
+ */
23
+ maxAgeSeconds?: number;
24
+ /**
25
+ * The [`CacheQueryOptions`](https://developer.mozilla.org/en-US/docs/Web/API/Cache/delete#Parameters)
26
+ * that will be used when calling `delete()` on the cache.
27
+ */
28
+ matchOptions?: CacheQueryOptions;
29
+ }
30
+
31
+ /**
32
+ * Allows you to expires cached responses based on age or maximum number of entries.
33
+ * @see https://serwist.pages.dev/docs/expiration/cache-expiration
34
+ */
35
+ export class CacheExpiration {
36
+ private _isRunning = false;
37
+ private _rerunRequested = false;
38
+ private readonly _maxEntries?: number;
39
+ private readonly _maxAgeSeconds?: number;
40
+ private readonly _matchOptions?: CacheQueryOptions;
41
+ private readonly _cacheName: string;
42
+ private readonly _timestampModel: CacheTimestampsModel;
43
+
44
+ /**
45
+ * To construct a new CacheExpiration instance you must provide at least
46
+ * one of the `config` properties.
47
+ *
48
+ * @param cacheName Name of the cache to apply restrictions to.
49
+ * @param config
50
+ */
51
+ constructor(cacheName: string, config: CacheExpirationConfig = {}) {
52
+ if (process.env.NODE_ENV !== "production") {
53
+ assert!.isType(cacheName, "string", {
54
+ moduleName: "serwist/plugins",
55
+ className: "CacheExpiration",
56
+ funcName: "constructor",
57
+ paramName: "cacheName",
58
+ });
59
+
60
+ if (!(config.maxEntries || config.maxAgeSeconds)) {
61
+ throw new SerwistError("max-entries-or-age-required", {
62
+ moduleName: "serwist/plugins",
63
+ className: "CacheExpiration",
64
+ funcName: "constructor",
65
+ });
66
+ }
67
+
68
+ if (config.maxEntries) {
69
+ assert!.isType(config.maxEntries, "number", {
70
+ moduleName: "serwist/plugins",
71
+ className: "CacheExpiration",
72
+ funcName: "constructor",
73
+ paramName: "config.maxEntries",
74
+ });
75
+ }
76
+
77
+ if (config.maxAgeSeconds) {
78
+ assert!.isType(config.maxAgeSeconds, "number", {
79
+ moduleName: "serwist/plugins",
80
+ className: "CacheExpiration",
81
+ funcName: "constructor",
82
+ paramName: "config.maxAgeSeconds",
83
+ });
84
+ }
85
+ }
86
+
87
+ this._maxEntries = config.maxEntries;
88
+ this._maxAgeSeconds = config.maxAgeSeconds;
89
+ this._matchOptions = config.matchOptions;
90
+ this._cacheName = cacheName;
91
+ this._timestampModel = new CacheTimestampsModel(cacheName);
92
+ }
93
+
94
+ /**
95
+ * Expires entries for the given cache and given criteria.
96
+ */
97
+ async expireEntries(): Promise<void> {
98
+ if (this._isRunning) {
99
+ this._rerunRequested = true;
100
+ return;
101
+ }
102
+ this._isRunning = true;
103
+
104
+ const minTimestamp = this._maxAgeSeconds ? Date.now() - this._maxAgeSeconds * 1000 : 0;
105
+
106
+ const urlsExpired = await this._timestampModel.expireEntries(minTimestamp, this._maxEntries);
107
+
108
+ // Delete URLs from the cache
109
+ const cache = await self.caches.open(this._cacheName);
110
+ for (const url of urlsExpired) {
111
+ await cache.delete(url, this._matchOptions);
112
+ }
113
+
114
+ if (process.env.NODE_ENV !== "production") {
115
+ if (urlsExpired.length > 0) {
116
+ logger.groupCollapsed(
117
+ `Expired ${urlsExpired.length} ` +
118
+ `${urlsExpired.length === 1 ? "entry" : "entries"} and removed ` +
119
+ `${urlsExpired.length === 1 ? "it" : "them"} from the ` +
120
+ `'${this._cacheName}' cache.`,
121
+ );
122
+ logger.log(`Expired the following ${urlsExpired.length === 1 ? "URL" : "URLs"}:`);
123
+ for (const url of urlsExpired) {
124
+ logger.log(` ${url}`);
125
+ }
126
+ logger.groupEnd();
127
+ } else {
128
+ logger.debug("Cache expiration ran and found no entries to remove.");
129
+ }
130
+ }
131
+
132
+ this._isRunning = false;
133
+ if (this._rerunRequested) {
134
+ this._rerunRequested = false;
135
+ void this.expireEntries();
136
+ }
137
+ }
138
+
139
+ /**
140
+ * Updates the timestamp for the given URL, allowing it to be correctly
141
+ * tracked by the class.
142
+ *
143
+ * @param url
144
+ */
145
+ async updateTimestamp(url: string): Promise<void> {
146
+ if (process.env.NODE_ENV !== "production") {
147
+ assert!.isType(url, "string", {
148
+ moduleName: "serwist/plugins",
149
+ className: "CacheExpiration",
150
+ funcName: "updateTimestamp",
151
+ paramName: "url",
152
+ });
153
+ }
154
+
155
+ await this._timestampModel.setTimestamp(url, Date.now());
156
+ }
157
+
158
+ /**
159
+ * Checks if a URL has expired or not before it's used.
160
+ *
161
+ * This looks the timestamp up in IndexedDB and can be slow.
162
+ *
163
+ * Note: This method does not remove an expired entry, call
164
+ * `expireEntries()` to remove such entries instead.
165
+ *
166
+ * @param url
167
+ * @returns
168
+ */
169
+ async isURLExpired(url: string): Promise<boolean> {
170
+ if (!this._maxAgeSeconds) {
171
+ if (process.env.NODE_ENV !== "production") {
172
+ throw new SerwistError("expired-test-without-max-age", {
173
+ methodName: "isURLExpired",
174
+ paramName: "maxAgeSeconds",
175
+ });
176
+ }
177
+ return false;
178
+ }
179
+ const timestamp = await this._timestampModel.getTimestamp(url);
180
+ const expireOlderThan = Date.now() - this._maxAgeSeconds * 1000;
181
+ return timestamp !== undefined ? timestamp < expireOlderThan : true;
182
+ }
183
+
184
+ /**
185
+ * Removes the IndexedDB used to keep track of cache expiration metadata.
186
+ */
187
+ async delete(): Promise<void> {
188
+ // Make sure we don't attempt another rerun if we're called in the middle of
189
+ // a cache expiration.
190
+ this._rerunRequested = false;
191
+ await this._timestampModel.expireEntries(Number.POSITIVE_INFINITY); // Expires all.
192
+ }
193
+ }
@@ -0,0 +1,300 @@
1
+ /*
2
+ Copyright 2018 Google LLC
3
+
4
+ Use of this source code is governed by an MIT-style
5
+ license that can be found in the LICENSE file or at
6
+ https://opensource.org/licenses/MIT.
7
+ */
8
+
9
+ import type { CacheDidUpdateCallbackParam, CachedResponseWillBeUsedCallbackParam, SerwistPlugin } from "../../types.js";
10
+ import { registerQuotaErrorCallback } from "../../registerQuotaErrorCallback.js";
11
+ import { assert } from "../../utils/assert.js";
12
+ import { SerwistError } from "../../utils/SerwistError.js";
13
+ import { getFriendlyURL } from "../../utils/getFriendlyURL.js";
14
+ import { logger } from "../../utils/logger.js";
15
+ import { cacheNames as privateCacheNames } from "../../utils/cacheNames.js";
16
+ import { CacheExpiration } from "./CacheExpiration.js";
17
+
18
+ export interface ExpirationPluginOptions {
19
+ /**
20
+ * The maximum number of entries to cache. Entries used the least will be removed
21
+ * as the maximum is reached.
22
+ */
23
+ maxEntries?: number;
24
+ /**
25
+ * The maximum number of seconds before an entry is treated as stale and removed.
26
+ */
27
+ maxAgeSeconds?: number;
28
+ /**
29
+ * Determines whether `maxAgeSeconds` should be calculated from when an
30
+ * entry was last fetched or when it was last used.
31
+ *
32
+ * @default "last-fetched"
33
+ */
34
+ maxAgeFrom?: "last-fetched" | "last-used";
35
+ /**
36
+ * The [`CacheQueryOptions`](https://developer.mozilla.org/en-US/docs/Web/API/Cache/delete#Parameters)
37
+ * that will be used when calling `delete()` on the cache.
38
+ */
39
+ matchOptions?: CacheQueryOptions;
40
+ /**
41
+ * Whether to opt this cache into automatic deletion if the available storage quota has been exceeded.
42
+ */
43
+ purgeOnQuotaError?: boolean;
44
+ }
45
+
46
+ /**
47
+ * This plugin can be used in a `serwist/strategies` Strategy to regularly enforce a
48
+ * limit on the age and/or the number of cached requests.
49
+ *
50
+ * It can only be used with Strategy instances that have a custom `cacheName` property set.
51
+ * In other words, it can't be used to expire entries in strategies that use the default runtime
52
+ * cache name.
53
+ *
54
+ * Whenever a cached response is used or updated, this plugin will look
55
+ * at the associated cache and remove any old or extra responses.
56
+ *
57
+ * When using `maxAgeSeconds`, responses may be used *once* after expiring
58
+ * because the expiration clean up will not have occurred until *after* the
59
+ * cached response has been used. If the response has a "Date" header, then a lightweight expiration
60
+ * check is performed, and the response will not be used immediately.
61
+ *
62
+ * When using `maxEntries`, the least recently requested entry will be removed
63
+ * from the cache.
64
+ *
65
+ * @see https://serwist.pages.dev/docs/expiration/expiration-plugin
66
+ */
67
+ export class ExpirationPlugin implements SerwistPlugin {
68
+ private readonly _config: ExpirationPluginOptions;
69
+ private _cacheExpirations: Map<string, CacheExpiration>;
70
+
71
+ /**
72
+ * @param config
73
+ */
74
+ constructor(config: ExpirationPluginOptions = {}) {
75
+ if (process.env.NODE_ENV !== "production") {
76
+ if (!(config.maxEntries || config.maxAgeSeconds)) {
77
+ throw new SerwistError("max-entries-or-age-required", {
78
+ moduleName: "serwist/plugins",
79
+ className: "ExpirationPlugin",
80
+ funcName: "constructor",
81
+ });
82
+ }
83
+
84
+ if (config.maxEntries) {
85
+ assert!.isType(config.maxEntries, "number", {
86
+ moduleName: "serwist/plugins",
87
+ className: "ExpirationPlugin",
88
+ funcName: "constructor",
89
+ paramName: "config.maxEntries",
90
+ });
91
+ }
92
+
93
+ if (config.maxAgeSeconds) {
94
+ assert!.isType(config.maxAgeSeconds, "number", {
95
+ moduleName: "serwist/plugins",
96
+ className: "ExpirationPlugin",
97
+ funcName: "constructor",
98
+ paramName: "config.maxAgeSeconds",
99
+ });
100
+ }
101
+
102
+ if (config.maxAgeFrom) {
103
+ assert!.isType(config.maxAgeFrom, "string", {
104
+ moduleName: "serwist/plugins",
105
+ className: "ExpirationPlugin",
106
+ funcName: "constructor",
107
+ paramName: "config.maxAgeFrom",
108
+ });
109
+ }
110
+ }
111
+
112
+ this._config = config;
113
+ this._cacheExpirations = new Map();
114
+
115
+ if (!this._config.maxAgeFrom) {
116
+ this._config.maxAgeFrom = "last-fetched";
117
+ }
118
+
119
+ if (this._config.purgeOnQuotaError) {
120
+ registerQuotaErrorCallback(() => this.deleteCacheAndMetadata());
121
+ }
122
+ }
123
+
124
+ /**
125
+ * A simple helper method to return a CacheExpiration instance for a given
126
+ * cache name.
127
+ *
128
+ * @param cacheName
129
+ * @returns
130
+ * @private
131
+ */
132
+ private _getCacheExpiration(cacheName: string): CacheExpiration {
133
+ if (cacheName === privateCacheNames.getRuntimeName()) {
134
+ throw new SerwistError("expire-custom-caches-only");
135
+ }
136
+
137
+ let cacheExpiration = this._cacheExpirations.get(cacheName);
138
+ if (!cacheExpiration) {
139
+ cacheExpiration = new CacheExpiration(cacheName, this._config);
140
+ this._cacheExpirations.set(cacheName, cacheExpiration);
141
+ }
142
+ return cacheExpiration;
143
+ }
144
+
145
+ /**
146
+ * A "lifecycle" callback that will be triggered automatically by the
147
+ * `serwist/strategies` handlers when a `Response` is about to be returned
148
+ * from a [Cache](https://developer.mozilla.org/en-US/docs/Web/API/Cache) to
149
+ * the handler. It allows the `Response` to be inspected for freshness and
150
+ * prevents it from being used if the `Response`'s `Date` header value is
151
+ * older than the configured `maxAgeSeconds`.
152
+ *
153
+ * @param options
154
+ * @returns `cachedResponse` if it is fresh and `null` if it is stale or
155
+ * not available.
156
+ * @private
157
+ */
158
+ cachedResponseWillBeUsed({ event, cacheName, request, cachedResponse }: CachedResponseWillBeUsedCallbackParam) {
159
+ if (!cachedResponse) {
160
+ return null;
161
+ }
162
+
163
+ const isFresh = this._isResponseDateFresh(cachedResponse);
164
+
165
+ // Expire entries to ensure that even if the expiration date has
166
+ // expired, it'll only be used once.
167
+ const cacheExpiration = this._getCacheExpiration(cacheName);
168
+
169
+ const isMaxAgeFromLastUsed = this._config.maxAgeFrom === "last-used";
170
+
171
+ const done = (async () => {
172
+ // Update the metadata for the request URL to the current timestamp.
173
+ // Only applies if `maxAgeFrom` is `"last-used"`, since the current
174
+ // lifecycle callback is `cachedResponseWillBeUsed`.
175
+ // This needs to be called before `expireEntries()` so as to avoid
176
+ // this URL being marked as expired.
177
+ if (isMaxAgeFromLastUsed) {
178
+ await cacheExpiration.updateTimestamp(request.url);
179
+ }
180
+ await cacheExpiration.expireEntries();
181
+ })();
182
+ try {
183
+ event.waitUntil(done);
184
+ } catch (error) {
185
+ if (process.env.NODE_ENV !== "production") {
186
+ if (event instanceof FetchEvent) {
187
+ logger.warn(`Unable to ensure service worker stays alive when updating cache entry for '${getFriendlyURL(event.request.url)}'.`);
188
+ }
189
+ }
190
+ }
191
+
192
+ return isFresh ? cachedResponse : null;
193
+ }
194
+
195
+ /**
196
+ * @param cachedResponse
197
+ * @returns
198
+ * @private
199
+ */
200
+ private _isResponseDateFresh(cachedResponse: Response): boolean {
201
+ const isMaxAgeFromLastUsed = this._config.maxAgeFrom === "last-used";
202
+ // If `maxAgeFrom` is `"last-used"`, the `Date` header doesn't really
203
+ // matter since it is about when the response was created.
204
+ if (isMaxAgeFromLastUsed) {
205
+ return true;
206
+ }
207
+ const now = Date.now();
208
+ if (!this._config.maxAgeSeconds) {
209
+ return true;
210
+ }
211
+ // Check if the `Date` header will suffice a quick expiration check.
212
+ // See https://github.com/GoogleChromeLabs/sw-toolbox/issues/164 for
213
+ // discussion.
214
+ const dateHeaderTimestamp = this._getDateHeaderTimestamp(cachedResponse);
215
+ if (dateHeaderTimestamp === null) {
216
+ // Unable to parse date, so assume it's fresh.
217
+ return true;
218
+ }
219
+ // If we have a valid headerTime, then our response is fresh if the
220
+ // headerTime plus maxAgeSeconds is greater than the current time.
221
+ return dateHeaderTimestamp >= now - this._config.maxAgeSeconds * 1000;
222
+ }
223
+
224
+ /**
225
+ * Extracts the `Date` header and parse it into an useful value.
226
+ *
227
+ * @param cachedResponse
228
+ * @returns
229
+ * @private
230
+ */
231
+ private _getDateHeaderTimestamp(cachedResponse: Response): number | null {
232
+ if (!cachedResponse.headers.has("date")) {
233
+ return null;
234
+ }
235
+
236
+ const dateHeader = cachedResponse.headers.get("date")!;
237
+ const parsedDate = new Date(dateHeader);
238
+ const headerTime = parsedDate.getTime();
239
+
240
+ // If the `Date` header is invalid for some reason, `parsedDate.getTime()`
241
+ // will return NaN.
242
+ if (Number.isNaN(headerTime)) {
243
+ return null;
244
+ }
245
+
246
+ return headerTime;
247
+ }
248
+
249
+ /**
250
+ * A "lifecycle" callback that will be triggered automatically by the
251
+ * `serwist/strategies` handlers when an entry is added to a cache.
252
+ *
253
+ * @param options
254
+ * @private
255
+ */
256
+ async cacheDidUpdate({ cacheName, request }: CacheDidUpdateCallbackParam) {
257
+ if (process.env.NODE_ENV !== "production") {
258
+ assert!.isType(cacheName, "string", {
259
+ moduleName: "serwist/plugins",
260
+ className: "Plugin",
261
+ funcName: "cacheDidUpdate",
262
+ paramName: "cacheName",
263
+ });
264
+ assert!.isInstance(request, Request, {
265
+ moduleName: "serwist/plugins",
266
+ className: "Plugin",
267
+ funcName: "cacheDidUpdate",
268
+ paramName: "request",
269
+ });
270
+ }
271
+
272
+ const cacheExpiration = this._getCacheExpiration(cacheName);
273
+ await cacheExpiration.updateTimestamp(request.url);
274
+ await cacheExpiration.expireEntries();
275
+ }
276
+
277
+ /**
278
+ * Deletes the underlying `Cache` instance associated with this instance and the metadata
279
+ * from IndexedDB used to keep track of expiration details for each `Cache` instance.
280
+ *
281
+ * When using cache expiration, calling this method is preferable to calling
282
+ * `caches.delete()` directly, since this will ensure that the IndexedDB
283
+ * metadata is also cleanly removed and that open IndexedDB instances are deleted.
284
+ *
285
+ * Note that if you're *not* using cache expiration for a given cache, calling
286
+ * `caches.delete()` and passing in the cache's name should be sufficient.
287
+ * There is no Serwist-specific method needed for cleanup in that case.
288
+ */
289
+ async deleteCacheAndMetadata(): Promise<void> {
290
+ // Do this one at a time instead of all at once via `Promise.all()` to
291
+ // reduce the chance of inconsistency if a promise rejects.
292
+ for (const [cacheName, cacheExpiration] of this._cacheExpirations) {
293
+ await self.caches.delete(cacheName);
294
+ await cacheExpiration.delete();
295
+ }
296
+
297
+ // Reset this._cacheExpirations to its initial state.
298
+ this._cacheExpirations = new Map();
299
+ }
300
+ }