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.
- package/dist/NavigationRoute.d.ts.map +1 -1
- package/dist/RegExpRoute.d.ts.map +1 -1
- package/dist/Route.d.ts.map +1 -1
- package/dist/Serwist.d.ts +75 -53
- package/dist/Serwist.d.ts.map +1 -1
- package/dist/cacheNames.d.ts.map +1 -1
- package/dist/chunks/waitUntil.js +49 -42
- package/dist/copyResponse.d.ts.map +1 -1
- package/dist/index.d.ts +11 -36
- package/dist/index.d.ts.map +1 -1
- package/dist/index.internal.d.ts +16 -16
- package/dist/index.internal.d.ts.map +1 -1
- package/dist/index.internal.js +25 -3
- package/dist/index.js +1918 -248
- package/dist/lib/backgroundSync/BackgroundSyncPlugin.d.ts +2 -2
- package/dist/lib/backgroundSync/BackgroundSyncPlugin.d.ts.map +1 -1
- package/dist/lib/backgroundSync/StorableRequest.d.ts.map +1 -1
- package/dist/lib/backgroundSync/index.d.ts +6 -0
- package/dist/lib/backgroundSync/index.d.ts.map +1 -0
- package/dist/lib/broadcastUpdate/BroadcastCacheUpdate.d.ts.map +1 -1
- package/dist/lib/broadcastUpdate/BroadcastUpdatePlugin.d.ts +2 -2
- package/dist/lib/broadcastUpdate/BroadcastUpdatePlugin.d.ts.map +1 -1
- package/dist/lib/broadcastUpdate/index.d.ts +6 -0
- package/dist/lib/broadcastUpdate/index.d.ts.map +1 -0
- package/dist/lib/broadcastUpdate/responsesAreSame.d.ts.map +1 -1
- package/dist/lib/cacheableResponse/CacheableResponsePlugin.d.ts +3 -3
- package/dist/lib/cacheableResponse/CacheableResponsePlugin.d.ts.map +1 -1
- package/dist/lib/cacheableResponse/index.d.ts +4 -0
- package/dist/lib/cacheableResponse/index.d.ts.map +1 -0
- package/dist/{legacy/utils → lib/controllers/PrecacheController}/PrecacheCacheKeyPlugin.d.ts +4 -4
- package/dist/lib/controllers/PrecacheController/PrecacheCacheKeyPlugin.d.ts.map +1 -0
- package/dist/{legacy → lib/controllers/PrecacheController}/PrecacheController.d.ts +77 -64
- package/dist/lib/controllers/PrecacheController/PrecacheController.d.ts.map +1 -0
- package/dist/lib/controllers/PrecacheController/PrecacheInstallReportPlugin.d.ts +14 -0
- package/dist/lib/controllers/PrecacheController/PrecacheInstallReportPlugin.d.ts.map +1 -0
- package/dist/{PrecacheRoute.d.ts → lib/controllers/PrecacheController/PrecacheRoute.d.ts} +4 -5
- package/dist/lib/controllers/PrecacheController/PrecacheRoute.d.ts.map +1 -0
- package/dist/lib/{strategies → controllers/PrecacheController}/PrecacheStrategy.d.ts +6 -6
- package/dist/lib/controllers/PrecacheController/PrecacheStrategy.d.ts.map +1 -0
- package/dist/lib/controllers/PrecacheController/parsePrecacheOptions.d.ts +25 -0
- package/dist/lib/controllers/PrecacheController/parsePrecacheOptions.d.ts.map +1 -0
- package/dist/lib/controllers/RuntimeCacheController.d.ts +70 -0
- package/dist/lib/controllers/RuntimeCacheController.d.ts.map +1 -0
- package/dist/lib/controllers/index.d.ts +4 -0
- package/dist/lib/controllers/index.d.ts.map +1 -0
- package/dist/lib/expiration/ExpirationPlugin.d.ts +2 -2
- package/dist/lib/expiration/ExpirationPlugin.d.ts.map +1 -1
- package/dist/lib/expiration/index.d.ts +4 -0
- package/dist/lib/expiration/index.d.ts.map +1 -0
- package/dist/lib/googleAnalytics/index.d.ts +3 -0
- package/dist/lib/googleAnalytics/index.d.ts.map +1 -0
- package/dist/lib/googleAnalytics/initializeGoogleAnalytics.d.ts.map +1 -1
- package/dist/lib/precaching/PrecacheFallbackPlugin.d.ts +15 -9
- package/dist/lib/precaching/PrecacheFallbackPlugin.d.ts.map +1 -1
- package/dist/lib/precaching/index.d.ts +3 -0
- package/dist/lib/precaching/index.d.ts.map +1 -0
- package/dist/lib/rangeRequests/RangeRequestsPlugin.d.ts +3 -3
- package/dist/lib/rangeRequests/RangeRequestsPlugin.d.ts.map +1 -1
- package/dist/lib/rangeRequests/createPartialResponse.d.ts.map +1 -1
- package/dist/lib/rangeRequests/index.d.ts +3 -0
- package/dist/lib/rangeRequests/index.d.ts.map +1 -0
- package/dist/lib/rangeRequests/utils/calculateEffectiveBoundaries.d.ts.map +1 -1
- package/dist/lib/rangeRequests/utils/parseRangeHeader.d.ts.map +1 -1
- package/dist/lib/strategies/Strategy.d.ts +3 -3
- package/dist/lib/strategies/Strategy.d.ts.map +1 -1
- package/dist/lib/strategies/StrategyHandler.d.ts +4 -4
- package/dist/lib/strategies/StrategyHandler.d.ts.map +1 -1
- package/dist/lib/strategies/index.d.ts +11 -0
- package/dist/lib/strategies/index.d.ts.map +1 -0
- package/dist/lib/strategies/plugins/cacheOkAndOpaquePlugin.d.ts +2 -2
- package/dist/lib/strategies/plugins/cacheOkAndOpaquePlugin.d.ts.map +1 -1
- package/dist/navigationPreload.d.ts.map +1 -1
- package/dist/registerQuotaErrorCallback.d.ts.map +1 -1
- package/dist/setCacheNameDetails.d.ts +1 -1
- package/dist/setCacheNameDetails.d.ts.map +1 -1
- package/dist/types.d.ts +35 -64
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/SerwistError.d.ts +1 -1
- package/dist/utils/SerwistError.d.ts.map +1 -1
- package/dist/utils/cleanupOutdatedCaches.d.ts.map +1 -1
- package/dist/utils/createCacheKey.d.ts.map +1 -1
- package/dist/utils/deleteOutdatedCaches.d.ts.map +1 -1
- package/dist/utils/getFriendlyURL.d.ts.map +1 -1
- package/dist/utils/normalizeHandler.d.ts.map +1 -1
- package/dist/utils/parseRoute.d.ts.map +1 -1
- package/dist/utils/pluginUtils.d.ts +2 -2
- package/dist/utils/pluginUtils.d.ts.map +1 -1
- package/dist/utils/printCleanupDetails.d.ts.map +1 -1
- package/dist/utils/printInstallDetails.d.ts.map +1 -1
- package/dist/utils/removeIgnoredSearchParams.d.ts.map +1 -1
- package/dist/utils/waitUntil.d.ts.map +1 -1
- package/package.json +29 -13
- package/src/NavigationRoute.ts +2 -2
- package/src/RegExpRoute.ts +2 -2
- package/src/Route.ts +2 -2
- package/src/Serwist.ts +170 -271
- package/src/cacheNames.ts +1 -1
- package/src/copyResponse.ts +2 -2
- package/src/index.internal.ts +16 -16
- package/src/index.ts +68 -93
- package/src/lib/backgroundSync/BackgroundSyncPlugin.ts +2 -2
- package/src/lib/backgroundSync/BackgroundSyncQueue.ts +4 -4
- package/src/lib/backgroundSync/BackgroundSyncQueueStore.ts +1 -1
- package/src/lib/backgroundSync/StorableRequest.ts +1 -1
- package/src/lib/backgroundSync/index.ts +5 -0
- package/src/lib/broadcastUpdate/BroadcastCacheUpdate.ts +4 -4
- package/src/lib/broadcastUpdate/BroadcastUpdatePlugin.ts +2 -2
- package/src/lib/broadcastUpdate/index.ts +5 -0
- package/src/lib/broadcastUpdate/responsesAreSame.ts +2 -2
- package/src/lib/cacheableResponse/CacheableResponse.ts +4 -4
- package/src/lib/cacheableResponse/CacheableResponsePlugin.ts +3 -3
- package/src/lib/cacheableResponse/index.ts +3 -0
- package/src/{legacy/utils → lib/controllers/PrecacheController}/PrecacheCacheKeyPlugin.ts +5 -6
- package/src/lib/controllers/PrecacheController/PrecacheController.ts +328 -0
- package/src/{utils → lib/controllers/PrecacheController}/PrecacheInstallReportPlugin.ts +5 -5
- package/src/{PrecacheRoute.ts → lib/controllers/PrecacheController/PrecacheRoute.ts} +11 -12
- package/src/lib/{strategies → controllers/PrecacheController}/PrecacheStrategy.ts +13 -13
- package/src/lib/controllers/PrecacheController/parsePrecacheOptions.ts +46 -0
- package/src/lib/controllers/RuntimeCacheController.ts +119 -0
- package/src/lib/controllers/index.ts +3 -0
- package/src/lib/expiration/CacheExpiration.ts +3 -3
- package/src/lib/expiration/ExpirationPlugin.ts +8 -8
- package/src/lib/expiration/index.ts +3 -0
- package/src/lib/googleAnalytics/index.ts +2 -0
- package/src/lib/googleAnalytics/initializeGoogleAnalytics.ts +5 -5
- package/src/lib/precaching/PrecacheFallbackPlugin.ts +23 -12
- package/src/lib/precaching/index.ts +2 -0
- package/src/lib/rangeRequests/RangeRequestsPlugin.ts +3 -3
- package/src/lib/rangeRequests/createPartialResponse.ts +3 -3
- package/src/lib/rangeRequests/index.ts +2 -0
- package/src/lib/rangeRequests/utils/calculateEffectiveBoundaries.ts +2 -2
- package/src/lib/rangeRequests/utils/parseRangeHeader.ts +2 -2
- package/src/lib/strategies/CacheFirst.ts +3 -3
- package/src/lib/strategies/CacheOnly.ts +3 -3
- package/src/lib/strategies/NetworkFirst.ts +3 -3
- package/src/lib/strategies/NetworkOnly.ts +4 -4
- package/src/lib/strategies/StaleWhileRevalidate.ts +3 -3
- package/src/lib/strategies/Strategy.ts +7 -7
- package/src/lib/strategies/StrategyHandler.ts +18 -18
- package/src/lib/strategies/index.ts +10 -0
- package/src/lib/strategies/plugins/cacheOkAndOpaquePlugin.ts +2 -2
- package/src/lib/strategies/utils/messages.ts +2 -2
- package/src/models/messages/messages.ts +3 -3
- package/src/navigationPreload.ts +1 -1
- package/src/registerQuotaErrorCallback.ts +2 -2
- package/src/setCacheNameDetails.ts +4 -4
- package/src/types.ts +56 -68
- package/src/utils/SerwistError.ts +2 -2
- package/src/utils/cacheNames.ts +1 -1
- package/src/utils/executeQuotaErrorCallbacks.ts +1 -1
- package/src/utils/pluginUtils.ts +2 -2
- package/dist/PrecacheRoute.d.ts.map +0 -1
- package/dist/chunks/printInstallDetails.js +0 -1601
- package/dist/chunks/resultingClientExists.js +0 -32
- package/dist/index.legacy.d.ts +0 -28
- package/dist/index.legacy.d.ts.map +0 -1
- package/dist/index.legacy.js +0 -790
- package/dist/legacy/PrecacheController.d.ts.map +0 -1
- package/dist/legacy/PrecacheFallbackPlugin.d.ts +0 -61
- package/dist/legacy/PrecacheFallbackPlugin.d.ts.map +0 -1
- package/dist/legacy/PrecacheRoute.d.ts +0 -19
- package/dist/legacy/PrecacheRoute.d.ts.map +0 -1
- package/dist/legacy/Router.d.ts +0 -151
- package/dist/legacy/Router.d.ts.map +0 -1
- package/dist/legacy/addPlugins.d.ts +0 -9
- package/dist/legacy/addPlugins.d.ts.map +0 -1
- package/dist/legacy/addRoute.d.ts +0 -14
- package/dist/legacy/addRoute.d.ts.map +0 -1
- package/dist/legacy/constants.d.ts +0 -10
- package/dist/legacy/constants.d.ts.map +0 -1
- package/dist/legacy/createHandlerBoundToURL.d.ts +0 -17
- package/dist/legacy/createHandlerBoundToURL.d.ts.map +0 -1
- package/dist/legacy/fallbacks.d.ts +0 -59
- package/dist/legacy/fallbacks.d.ts.map +0 -1
- package/dist/legacy/getCacheKeyForURL.d.ts +0 -20
- package/dist/legacy/getCacheKeyForURL.d.ts.map +0 -1
- package/dist/legacy/handlePrecaching.d.ts +0 -54
- package/dist/legacy/handlePrecaching.d.ts.map +0 -1
- package/dist/legacy/initializeGoogleAnalytics.d.ts +0 -38
- package/dist/legacy/initializeGoogleAnalytics.d.ts.map +0 -1
- package/dist/legacy/installSerwist.d.ts +0 -81
- package/dist/legacy/installSerwist.d.ts.map +0 -1
- package/dist/legacy/matchPrecache.d.ts +0 -15
- package/dist/legacy/matchPrecache.d.ts.map +0 -1
- package/dist/legacy/precache.d.ts +0 -20
- package/dist/legacy/precache.d.ts.map +0 -1
- package/dist/legacy/precacheAndRoute.d.ts +0 -14
- package/dist/legacy/precacheAndRoute.d.ts.map +0 -1
- package/dist/legacy/registerRoute.d.ts +0 -16
- package/dist/legacy/registerRoute.d.ts.map +0 -1
- package/dist/legacy/registerRuntimeCaching.d.ts +0 -11
- package/dist/legacy/registerRuntimeCaching.d.ts.map +0 -1
- package/dist/legacy/setCatchHandler.d.ts +0 -10
- package/dist/legacy/setCatchHandler.d.ts.map +0 -1
- package/dist/legacy/setDefaultHandler.d.ts +0 -13
- package/dist/legacy/setDefaultHandler.d.ts.map +0 -1
- package/dist/legacy/singletonPrecacheController.d.ts +0 -34
- package/dist/legacy/singletonPrecacheController.d.ts.map +0 -1
- package/dist/legacy/singletonRouter.d.ts +0 -41
- package/dist/legacy/singletonRouter.d.ts.map +0 -1
- package/dist/legacy/unregisterRoute.d.ts +0 -9
- package/dist/legacy/unregisterRoute.d.ts.map +0 -1
- package/dist/legacy/utils/PrecacheCacheKeyPlugin.d.ts.map +0 -1
- package/dist/legacy/utils/getCacheKeyForURL.d.ts +0 -14
- package/dist/legacy/utils/getCacheKeyForURL.d.ts.map +0 -1
- package/dist/lib/strategies/PrecacheStrategy.d.ts.map +0 -1
- package/dist/utils/PrecacheCacheKeyPlugin.d.ts +0 -16
- package/dist/utils/PrecacheCacheKeyPlugin.d.ts.map +0 -1
- package/dist/utils/PrecacheInstallReportPlugin.d.ts +0 -14
- package/dist/utils/PrecacheInstallReportPlugin.d.ts.map +0 -1
- package/dist/utils/parsePrecacheOptions.d.ts +0 -26
- package/dist/utils/parsePrecacheOptions.d.ts.map +0 -1
- package/src/index.legacy.ts +0 -62
- package/src/legacy/PrecacheController.ts +0 -337
- package/src/legacy/PrecacheFallbackPlugin.ts +0 -92
- package/src/legacy/PrecacheRoute.ts +0 -48
- package/src/legacy/Router.ts +0 -484
- package/src/legacy/addPlugins.ts +0 -21
- package/src/legacy/addRoute.ts +0 -27
- package/src/legacy/constants.ts +0 -22
- package/src/legacy/createHandlerBoundToURL.ts +0 -30
- package/src/legacy/fallbacks.ts +0 -94
- package/src/legacy/getCacheKeyForURL.ts +0 -32
- package/src/legacy/handlePrecaching.ts +0 -86
- package/src/legacy/initializeGoogleAnalytics.ts +0 -218
- package/src/legacy/installSerwist.ts +0 -170
- package/src/legacy/matchPrecache.ts +0 -27
- package/src/legacy/precache.ts +0 -33
- package/src/legacy/precacheAndRoute.ts +0 -27
- package/src/legacy/registerRoute.ts +0 -28
- package/src/legacy/registerRuntimeCaching.ts +0 -17
- package/src/legacy/setCatchHandler.ts +0 -21
- package/src/legacy/setDefaultHandler.ts +0 -24
- package/src/legacy/singletonPrecacheController.ts +0 -53
- package/src/legacy/singletonRouter.ts +0 -70
- package/src/legacy/unregisterRoute.ts +0 -13
- package/src/legacy/utils/getCacheKeyForURL.ts +0 -36
- package/src/utils/PrecacheCacheKeyPlugin.ts +0 -33
- package/src/utils/parsePrecacheOptions.ts +0 -47
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import { SerwistError } from "#utils/SerwistError.js";
|
|
2
|
+
import { assert } from "#utils/assert.js";
|
|
3
|
+
import { createCacheKey } from "#utils/createCacheKey.js";
|
|
4
|
+
import { logger } from "#utils/logger.js";
|
|
5
|
+
import { printCleanupDetails } from "#utils/printCleanupDetails.js";
|
|
6
|
+
import { printInstallDetails } from "#utils/printInstallDetails.js";
|
|
7
|
+
import { parallel } from "@serwist/utils";
|
|
8
|
+
import { NavigationRoute } from "../../../NavigationRoute.js";
|
|
9
|
+
import type { Serwist } from "../../../Serwist.js";
|
|
10
|
+
import type { Controller, RouteHandlerCallback, UrlManipulation } from "../../../types.js";
|
|
11
|
+
import type { Strategy } from "../../strategies/Strategy.js";
|
|
12
|
+
import { PrecacheInstallReportPlugin } from "./PrecacheInstallReportPlugin.js";
|
|
13
|
+
import { PrecacheRoute } from "./PrecacheRoute.js";
|
|
14
|
+
import { PrecacheStrategy } from "./PrecacheStrategy.js";
|
|
15
|
+
import type { PrecacheStrategyOptions } from "./PrecacheStrategy.js";
|
|
16
|
+
import { parsePrecacheOptions } from "./parsePrecacheOptions.js";
|
|
17
|
+
|
|
18
|
+
export interface PrecacheEntry {
|
|
19
|
+
integrity?: string;
|
|
20
|
+
url: string;
|
|
21
|
+
revision?: string | null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface PrecacheRouteOptions {
|
|
25
|
+
/**
|
|
26
|
+
* Tells Serwist to check the precache for an entry whose URL is the request URL appended
|
|
27
|
+
* with the specified value. Only applies if the request URL ends with "/".
|
|
28
|
+
*
|
|
29
|
+
* @default "index.html"
|
|
30
|
+
*/
|
|
31
|
+
directoryIndex?: string | null;
|
|
32
|
+
/**
|
|
33
|
+
* An array of `RegExp` objects matching search params that should be removed when looking
|
|
34
|
+
* for a precache match.
|
|
35
|
+
*/
|
|
36
|
+
ignoreURLParametersMatching?: RegExp[];
|
|
37
|
+
/**
|
|
38
|
+
* Tells Serwist to check the precache for an entry whose URL is the request URL appended
|
|
39
|
+
* with ".html".
|
|
40
|
+
*
|
|
41
|
+
* @default true
|
|
42
|
+
*/
|
|
43
|
+
cleanURLs?: boolean;
|
|
44
|
+
/**
|
|
45
|
+
* A function that should take a URL and return an array of alternative URLs that should
|
|
46
|
+
* be checked for precache matches.
|
|
47
|
+
*/
|
|
48
|
+
urlManipulation?: UrlManipulation;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface PrecacheControllerOptions {
|
|
52
|
+
/**
|
|
53
|
+
* The cache to use for precaching.
|
|
54
|
+
*/
|
|
55
|
+
cacheName?: string;
|
|
56
|
+
/**
|
|
57
|
+
* Whether outdated caches should be removed.
|
|
58
|
+
*
|
|
59
|
+
* @default false
|
|
60
|
+
*/
|
|
61
|
+
cleanupOutdatedCaches?: boolean;
|
|
62
|
+
/**
|
|
63
|
+
* The number of precache requests that should be made concurrently.
|
|
64
|
+
*
|
|
65
|
+
* @default 10
|
|
66
|
+
*/
|
|
67
|
+
concurrency?: number;
|
|
68
|
+
/**
|
|
69
|
+
* An URL that should point to a HTML file with which navigation requests for URLs that aren't
|
|
70
|
+
* precached will be fulfilled.
|
|
71
|
+
*/
|
|
72
|
+
navigateFallback?: string;
|
|
73
|
+
/**
|
|
74
|
+
* URLs that should be allowed to use the `navigateFallback` handler.
|
|
75
|
+
*/
|
|
76
|
+
navigateFallbackAllowlist?: RegExp[];
|
|
77
|
+
/**
|
|
78
|
+
* URLs that should not be allowed to use the `navigateFallback` handler. This takes precedence
|
|
79
|
+
* over `navigateFallbackAllowlist`.
|
|
80
|
+
*/
|
|
81
|
+
navigateFallbackDenylist?: RegExp[];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface PrecacheOptions extends PrecacheStrategyOptions, PrecacheRouteOptions, PrecacheControllerOptions {}
|
|
85
|
+
|
|
86
|
+
export class PrecacheController implements Controller {
|
|
87
|
+
private readonly _urlsToCacheKeys: Map<string, string> = new Map();
|
|
88
|
+
private readonly _urlsToCacheModes: Map<string, "reload" | "default" | "no-store" | "no-cache" | "force-cache" | "only-if-cached"> = new Map();
|
|
89
|
+
private readonly _cacheKeysToIntegrities: Map<string, string> = new Map();
|
|
90
|
+
private readonly _strategy: Strategy;
|
|
91
|
+
private _options: PrecacheControllerOptions;
|
|
92
|
+
private _routeOptions: PrecacheRouteOptions;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Create a new PrecacheController.
|
|
96
|
+
*
|
|
97
|
+
* @param options
|
|
98
|
+
*/
|
|
99
|
+
constructor(entries: (PrecacheEntry | string)[], precacheOptions?: PrecacheOptions) {
|
|
100
|
+
const { strategyOptions, routeOptions, controllerOptions } = parsePrecacheOptions(this, precacheOptions);
|
|
101
|
+
this.addToCacheList(entries);
|
|
102
|
+
this._strategy = new PrecacheStrategy(strategyOptions);
|
|
103
|
+
this._options = controllerOptions;
|
|
104
|
+
this._routeOptions = routeOptions;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* The strategy created by this controller and
|
|
109
|
+
* used to cache assets and respond to `fetch` events.
|
|
110
|
+
*/
|
|
111
|
+
get strategy(): Strategy {
|
|
112
|
+
return this._strategy;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Adds items to the cache list, removing duplicates and ensuring the information is valid.
|
|
117
|
+
*
|
|
118
|
+
* @param entries Array of entries to precache.
|
|
119
|
+
*/
|
|
120
|
+
addToCacheList(entries: (PrecacheEntry | string)[]): void {
|
|
121
|
+
if (process.env.NODE_ENV !== "production") {
|
|
122
|
+
assert!.isArray(entries, {
|
|
123
|
+
moduleName: "serwist",
|
|
124
|
+
className: "PrecacheController",
|
|
125
|
+
funcName: "addEntries",
|
|
126
|
+
paramName: "entries",
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const urlsToWarnAbout: string[] = [];
|
|
131
|
+
for (const entry of entries) {
|
|
132
|
+
// See https://github.com/GoogleChrome/workbox/issues/2259
|
|
133
|
+
if (typeof entry === "string") {
|
|
134
|
+
urlsToWarnAbout.push(entry);
|
|
135
|
+
} else if (entry && !entry.integrity && entry.revision === undefined) {
|
|
136
|
+
urlsToWarnAbout.push(entry.url);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const { cacheKey, url } = createCacheKey(entry);
|
|
140
|
+
const cacheMode = typeof entry !== "string" && entry.revision ? "reload" : "default";
|
|
141
|
+
|
|
142
|
+
if (this._urlsToCacheKeys.has(url) && this._urlsToCacheKeys.get(url) !== cacheKey) {
|
|
143
|
+
throw new SerwistError("add-to-cache-list-conflicting-entries", {
|
|
144
|
+
firstEntry: this._urlsToCacheKeys.get(url),
|
|
145
|
+
secondEntry: cacheKey,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (typeof entry !== "string" && entry.integrity) {
|
|
150
|
+
if (this._cacheKeysToIntegrities.has(cacheKey) && this._cacheKeysToIntegrities.get(cacheKey) !== entry.integrity) {
|
|
151
|
+
throw new SerwistError("add-to-cache-list-conflicting-integrities", {
|
|
152
|
+
url,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
this._cacheKeysToIntegrities.set(cacheKey, entry.integrity);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
this._urlsToCacheKeys.set(url, cacheKey);
|
|
159
|
+
this._urlsToCacheModes.set(url, cacheMode);
|
|
160
|
+
|
|
161
|
+
if (urlsToWarnAbout.length > 0) {
|
|
162
|
+
const warningMessage = `Serwist is precaching URLs without revision info: ${urlsToWarnAbout.join(
|
|
163
|
+
", ",
|
|
164
|
+
)}\nThis is generally NOT safe, as you risk serving outdated assets.`;
|
|
165
|
+
if (process.env.NODE_ENV === "production") {
|
|
166
|
+
// Use console directly to display this warning without bloating
|
|
167
|
+
// bundle sizes by pulling in all of the logger codebase in prod.
|
|
168
|
+
console.warn(warningMessage);
|
|
169
|
+
} else {
|
|
170
|
+
logger.warn(warningMessage);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
init({ serwist }: { serwist: Serwist }): void {
|
|
177
|
+
serwist.registerRoute(new PrecacheRoute(this, this._routeOptions));
|
|
178
|
+
|
|
179
|
+
if (this._options.navigateFallback) {
|
|
180
|
+
serwist.registerRoute(
|
|
181
|
+
new NavigationRoute(this.createHandlerBoundToUrl(this._options.navigateFallback), {
|
|
182
|
+
allowlist: this._options.navigateFallbackAllowlist,
|
|
183
|
+
denylist: this._options.navigateFallbackDenylist,
|
|
184
|
+
}),
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
async install({ event }: { event: ExtendableEvent }): Promise<void> {
|
|
190
|
+
const installReportPlugin = new PrecacheInstallReportPlugin();
|
|
191
|
+
this._strategy.plugins.push(installReportPlugin);
|
|
192
|
+
await parallel(this._options.concurrency!, Array.from(this._urlsToCacheKeys.entries()), async ([url, cacheKey]): Promise<void> => {
|
|
193
|
+
const integrity = this._cacheKeysToIntegrities.get(cacheKey);
|
|
194
|
+
const cacheMode = this._urlsToCacheModes.get(url);
|
|
195
|
+
|
|
196
|
+
const request = new Request(url, {
|
|
197
|
+
integrity,
|
|
198
|
+
cache: cacheMode,
|
|
199
|
+
credentials: "same-origin",
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
await Promise.all(
|
|
203
|
+
this._strategy.handleAll({
|
|
204
|
+
event,
|
|
205
|
+
request,
|
|
206
|
+
url: new URL(request.url),
|
|
207
|
+
params: { cacheKey },
|
|
208
|
+
}),
|
|
209
|
+
);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
const { updatedURLs, notUpdatedURLs } = installReportPlugin;
|
|
213
|
+
|
|
214
|
+
if (process.env.NODE_ENV !== "production") {
|
|
215
|
+
printInstallDetails(updatedURLs, notUpdatedURLs);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
async activate(): Promise<void> {
|
|
220
|
+
const cache = await self.caches.open(this._strategy.cacheName);
|
|
221
|
+
const currentlyCachedRequests = await cache.keys();
|
|
222
|
+
const expectedCacheKeys = new Set(this._urlsToCacheKeys.values());
|
|
223
|
+
|
|
224
|
+
const deletedCacheRequests: string[] = [];
|
|
225
|
+
|
|
226
|
+
for (const request of currentlyCachedRequests) {
|
|
227
|
+
if (!expectedCacheKeys.has(request.url)) {
|
|
228
|
+
await cache.delete(request);
|
|
229
|
+
deletedCacheRequests.push(request.url);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (process.env.NODE_ENV !== "production") {
|
|
234
|
+
printCleanupDetails(deletedCacheRequests);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Returns a mapping of a precached URL to the corresponding cache key, taking
|
|
240
|
+
* into account the revision information for the URL.
|
|
241
|
+
*
|
|
242
|
+
* @returns A URL to cache key mapping.
|
|
243
|
+
*/
|
|
244
|
+
getUrlsToPrecacheKeys(): Map<string, string> {
|
|
245
|
+
return this._urlsToCacheKeys;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Returns a list of all the URLs that have been precached by the current
|
|
250
|
+
* service worker.
|
|
251
|
+
*
|
|
252
|
+
* @returns The precached URLs.
|
|
253
|
+
*/
|
|
254
|
+
getPrecachedUrls(): string[] {
|
|
255
|
+
return [...this._urlsToCacheKeys.keys()];
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Returns the cache key used for storing a given URL. If that URL is
|
|
260
|
+
* unversioned, like "/index.html", then the cache key will be the original
|
|
261
|
+
* URL with a search parameter appended to it.
|
|
262
|
+
*
|
|
263
|
+
* @param url A URL whose cache key you want to look up.
|
|
264
|
+
* @returns The versioned URL that corresponds to a cache key
|
|
265
|
+
* for the original URL, or undefined if that URL isn't precached.
|
|
266
|
+
*/
|
|
267
|
+
getPrecacheKeyForUrl(url: string): string | undefined {
|
|
268
|
+
const urlObject = new URL(url, location.href);
|
|
269
|
+
return this._urlsToCacheKeys.get(urlObject.href);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* @param url A cache key whose SRI you want to look up.
|
|
274
|
+
* @returns The subresource integrity associated with the cache key,
|
|
275
|
+
* or undefined if it's not set.
|
|
276
|
+
*/
|
|
277
|
+
getIntegrityForPrecacheKey(cacheKey: string): string | undefined {
|
|
278
|
+
return this._cacheKeysToIntegrities.get(cacheKey);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* This acts as a drop-in replacement for
|
|
283
|
+
* [`cache.match()`](https://developer.mozilla.org/en-US/docs/Web/API/Cache/match)
|
|
284
|
+
* with the following differences:
|
|
285
|
+
*
|
|
286
|
+
* - It knows what the name of the precache is, and only checks in that cache.
|
|
287
|
+
* - It allows you to pass in an "original" URL without versioning parameters,
|
|
288
|
+
* and it will automatically look up the correct cache key for the currently
|
|
289
|
+
* active revision of that URL.
|
|
290
|
+
*
|
|
291
|
+
* E.g., `matchPrecache('index.html')` will find the correct precached
|
|
292
|
+
* response for the currently active service worker, even if the actual cache
|
|
293
|
+
* key is `'/index.html?__WB_REVISION__=1234abcd'`.
|
|
294
|
+
*
|
|
295
|
+
* @param request The key (without revisioning parameters)
|
|
296
|
+
* to look up in the precache.
|
|
297
|
+
* @returns
|
|
298
|
+
*/
|
|
299
|
+
async matchPrecache(request: string | Request): Promise<Response | undefined> {
|
|
300
|
+
const url = request instanceof Request ? request.url : request;
|
|
301
|
+
const cacheKey = this.getPrecacheKeyForUrl(url);
|
|
302
|
+
if (cacheKey) {
|
|
303
|
+
const cache = await self.caches.open(this._strategy.cacheName);
|
|
304
|
+
return cache.match(cacheKey);
|
|
305
|
+
}
|
|
306
|
+
return undefined;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Returns a function that looks up `url` in the precache (taking into
|
|
311
|
+
* account revision information), and returns the corresponding `Response`.
|
|
312
|
+
*
|
|
313
|
+
* @param url The precached URL which will be used to lookup the response.
|
|
314
|
+
* @return
|
|
315
|
+
*/
|
|
316
|
+
createHandlerBoundToUrl(url: string): RouteHandlerCallback {
|
|
317
|
+
const cacheKey = this.getPrecacheKeyForUrl(url);
|
|
318
|
+
if (!cacheKey) {
|
|
319
|
+
throw new SerwistError("non-precached-url", { url });
|
|
320
|
+
}
|
|
321
|
+
return (options) => {
|
|
322
|
+
options.request = new Request(url);
|
|
323
|
+
options.params = { cacheKey, ...options.params };
|
|
324
|
+
|
|
325
|
+
return this._strategy.handle(options);
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
}
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
https://opensource.org/licenses/MIT.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import type {
|
|
9
|
+
import type { StrategyPlugin, StrategyPluginCallbackParam } from "../../../types.js";
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* A plugin designed to determine the number of assets that were updated (or not updated)
|
|
@@ -14,22 +14,22 @@ import type { SerwistPlugin, SerwistPluginCallbackParam } from "../types.js";
|
|
|
14
14
|
*
|
|
15
15
|
* @private
|
|
16
16
|
*/
|
|
17
|
-
export class PrecacheInstallReportPlugin implements
|
|
17
|
+
export class PrecacheInstallReportPlugin implements StrategyPlugin {
|
|
18
18
|
updatedURLs: string[] = [];
|
|
19
19
|
notUpdatedURLs: string[] = [];
|
|
20
20
|
|
|
21
|
-
handlerWillStart:
|
|
21
|
+
handlerWillStart: StrategyPlugin["handlerWillStart"] = async ({ request, state }: StrategyPluginCallbackParam["handlerWillStart"]) => {
|
|
22
22
|
// TODO: `state` should never be undefined...
|
|
23
23
|
if (state) {
|
|
24
24
|
state.originalRequest = request;
|
|
25
25
|
}
|
|
26
26
|
};
|
|
27
27
|
|
|
28
|
-
cachedResponseWillBeUsed:
|
|
28
|
+
cachedResponseWillBeUsed: StrategyPlugin["cachedResponseWillBeUsed"] = async ({
|
|
29
29
|
event,
|
|
30
30
|
state,
|
|
31
31
|
cachedResponse,
|
|
32
|
-
}:
|
|
32
|
+
}: StrategyPluginCallbackParam["cachedResponseWillBeUsed"]) => {
|
|
33
33
|
if (event.type === "install") {
|
|
34
34
|
if (state?.originalRequest && state.originalRequest instanceof Request) {
|
|
35
35
|
// TODO: `state` should never be undefined...
|
|
@@ -6,13 +6,12 @@
|
|
|
6
6
|
https://opensource.org/licenses/MIT.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import {
|
|
10
|
-
import
|
|
11
|
-
import
|
|
12
|
-
import
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import { logger } from "./utils/logger.js";
|
|
9
|
+
import { generateURLVariations } from "#utils/generateURLVariations.js";
|
|
10
|
+
import { getFriendlyURL } from "#utils/getFriendlyURL.js";
|
|
11
|
+
import { logger } from "#utils/logger.js";
|
|
12
|
+
import { Route } from "../../../Route.js";
|
|
13
|
+
import type { RouteMatchCallback, RouteMatchCallbackOptions } from "../../../types.js";
|
|
14
|
+
import type { PrecacheController, PrecacheRouteOptions } from "./PrecacheController.js";
|
|
16
15
|
|
|
17
16
|
/**
|
|
18
17
|
* A subclass of {@linkcode Route} that takes a {@linkcode Serwist} instance and uses it to match
|
|
@@ -20,17 +19,17 @@ import { logger } from "./utils/logger.js";
|
|
|
20
19
|
*/
|
|
21
20
|
export class PrecacheRoute extends Route {
|
|
22
21
|
/**
|
|
23
|
-
* @param
|
|
22
|
+
* @param controller A {@linkcode PrecacheController} instance.
|
|
24
23
|
* @param options Options to control how requests are matched
|
|
25
24
|
* against the list of precached URLs.
|
|
26
25
|
*/
|
|
27
|
-
constructor(
|
|
26
|
+
constructor(controller: PrecacheController, options?: PrecacheRouteOptions) {
|
|
28
27
|
const match: RouteMatchCallback = ({ request }: RouteMatchCallbackOptions) => {
|
|
29
|
-
const urlsToCacheKeys =
|
|
28
|
+
const urlsToCacheKeys = controller.getUrlsToPrecacheKeys();
|
|
30
29
|
for (const possibleURL of generateURLVariations(request.url, options)) {
|
|
31
30
|
const cacheKey = urlsToCacheKeys.get(possibleURL);
|
|
32
31
|
if (cacheKey) {
|
|
33
|
-
const integrity =
|
|
32
|
+
const integrity = controller.getIntegrityForPrecacheKey(cacheKey);
|
|
34
33
|
return { cacheKey, integrity };
|
|
35
34
|
}
|
|
36
35
|
}
|
|
@@ -40,6 +39,6 @@ export class PrecacheRoute extends Route {
|
|
|
40
39
|
return;
|
|
41
40
|
};
|
|
42
41
|
|
|
43
|
-
super(match,
|
|
42
|
+
super(match, controller.strategy);
|
|
44
43
|
}
|
|
45
44
|
}
|
|
@@ -6,16 +6,16 @@
|
|
|
6
6
|
https://opensource.org/licenses/MIT.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import {
|
|
10
|
-
import
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import type {
|
|
16
|
-
import type { StrategyOptions } from "
|
|
17
|
-
import { Strategy } from "
|
|
18
|
-
import type { StrategyHandler } from "
|
|
9
|
+
import { SerwistError } from "#utils/SerwistError.js";
|
|
10
|
+
import { cacheNames as privateCacheNames } from "#utils/cacheNames.js";
|
|
11
|
+
import { getFriendlyURL } from "#utils/getFriendlyURL.js";
|
|
12
|
+
import { logger } from "#utils/logger.js";
|
|
13
|
+
import type { Serwist } from "../../../Serwist.js";
|
|
14
|
+
import { copyResponse } from "../../../copyResponse.js";
|
|
15
|
+
import type { StrategyPlugin } from "../../../types.js";
|
|
16
|
+
import type { StrategyOptions } from "../../strategies/Strategy.js";
|
|
17
|
+
import { Strategy } from "../../strategies/Strategy.js";
|
|
18
|
+
import type { StrategyHandler } from "../../strategies/StrategyHandler.js";
|
|
19
19
|
|
|
20
20
|
export interface PrecacheStrategyOptions extends StrategyOptions {
|
|
21
21
|
/**
|
|
@@ -39,7 +39,7 @@ export interface PrecacheStrategyOptions extends StrategyOptions {
|
|
|
39
39
|
export class PrecacheStrategy extends Strategy {
|
|
40
40
|
private readonly _fallbackToNetwork: boolean;
|
|
41
41
|
|
|
42
|
-
static readonly defaultPrecacheCacheabilityPlugin:
|
|
42
|
+
static readonly defaultPrecacheCacheabilityPlugin: StrategyPlugin = {
|
|
43
43
|
async cacheWillUpdate({ response }) {
|
|
44
44
|
if (!response || response.status >= 400) {
|
|
45
45
|
return null;
|
|
@@ -49,7 +49,7 @@ export class PrecacheStrategy extends Strategy {
|
|
|
49
49
|
},
|
|
50
50
|
};
|
|
51
51
|
|
|
52
|
-
static readonly copyRedirectedCacheableResponsesPlugin:
|
|
52
|
+
static readonly copyRedirectedCacheableResponsesPlugin: StrategyPlugin = {
|
|
53
53
|
async cacheWillUpdate({ response }) {
|
|
54
54
|
return response.redirected ? await copyResponse(response) : response;
|
|
55
55
|
},
|
|
@@ -63,7 +63,7 @@ export class PrecacheStrategy extends Strategy {
|
|
|
63
63
|
|
|
64
64
|
super(options);
|
|
65
65
|
|
|
66
|
-
this._fallbackToNetwork = options.fallbackToNetwork
|
|
66
|
+
this._fallbackToNetwork = options.fallbackToNetwork !== false;
|
|
67
67
|
|
|
68
68
|
// Redirected responses cannot be used to satisfy a navigation request, so
|
|
69
69
|
// any redirected response must be "copied" rather than cloned, so the new
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { privateCacheNames } from "#index.internal";
|
|
2
|
+
import type { Require } from "@serwist/utils";
|
|
3
|
+
import { PrecacheCacheKeyPlugin } from "./PrecacheCacheKeyPlugin.js";
|
|
4
|
+
import type { PrecacheController, PrecacheControllerOptions, PrecacheOptions, PrecacheRouteOptions } from "./PrecacheController.js";
|
|
5
|
+
import type { PrecacheStrategyOptions } from "./PrecacheStrategy.js";
|
|
6
|
+
|
|
7
|
+
export const parsePrecacheOptions = (
|
|
8
|
+
controller: PrecacheController,
|
|
9
|
+
{
|
|
10
|
+
cacheName,
|
|
11
|
+
plugins,
|
|
12
|
+
fetchOptions,
|
|
13
|
+
matchOptions,
|
|
14
|
+
fallbackToNetwork,
|
|
15
|
+
directoryIndex,
|
|
16
|
+
ignoreURLParametersMatching,
|
|
17
|
+
cleanURLs,
|
|
18
|
+
urlManipulation,
|
|
19
|
+
cleanupOutdatedCaches,
|
|
20
|
+
concurrency,
|
|
21
|
+
navigateFallback,
|
|
22
|
+
navigateFallbackAllowlist,
|
|
23
|
+
navigateFallbackDenylist,
|
|
24
|
+
}: PrecacheOptions = {},
|
|
25
|
+
) => ({
|
|
26
|
+
strategyOptions: {
|
|
27
|
+
cacheName: privateCacheNames.getPrecacheName(cacheName),
|
|
28
|
+
plugins: [...(plugins ?? []), new PrecacheCacheKeyPlugin({ precacheController: controller })],
|
|
29
|
+
fetchOptions,
|
|
30
|
+
matchOptions,
|
|
31
|
+
fallbackToNetwork,
|
|
32
|
+
} satisfies Require<PrecacheStrategyOptions, "cacheName" | "plugins">,
|
|
33
|
+
routeOptions: {
|
|
34
|
+
directoryIndex,
|
|
35
|
+
ignoreURLParametersMatching,
|
|
36
|
+
cleanURLs,
|
|
37
|
+
urlManipulation,
|
|
38
|
+
} satisfies PrecacheRouteOptions,
|
|
39
|
+
controllerOptions: {
|
|
40
|
+
cleanupOutdatedCaches,
|
|
41
|
+
concurrency: concurrency ?? 10,
|
|
42
|
+
navigateFallback,
|
|
43
|
+
navigateFallbackAllowlist,
|
|
44
|
+
navigateFallbackDenylist,
|
|
45
|
+
} satisfies Require<PrecacheControllerOptions, "concurrency">,
|
|
46
|
+
});
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { parallel } from "@serwist/utils";
|
|
2
|
+
import type { Serwist } from "../../Serwist.js";
|
|
3
|
+
import type { Controller, RuntimeCaching } from "../../types.js";
|
|
4
|
+
import { type PrecacheFallbackEntry, PrecacheFallbackPlugin } from "../precaching/PrecacheFallbackPlugin.js";
|
|
5
|
+
import { Strategy } from "../strategies/Strategy.js";
|
|
6
|
+
|
|
7
|
+
export interface WarmRuntimeCacheEntry {
|
|
8
|
+
integrity?: string;
|
|
9
|
+
url: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface WarmRuntimeCacheOptions {
|
|
13
|
+
concurrency?: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface FallbackEntry extends PrecacheFallbackEntry {}
|
|
17
|
+
|
|
18
|
+
export interface FallbacksOptions {
|
|
19
|
+
/**
|
|
20
|
+
* A list of fallback entries.
|
|
21
|
+
*/
|
|
22
|
+
entries: FallbackEntry[];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface RuntimeCacheControllerOptions {
|
|
26
|
+
/**
|
|
27
|
+
* URLs that should be cached as the service worker is installed.
|
|
28
|
+
*/
|
|
29
|
+
warmEntries?: (WarmRuntimeCacheEntry | string | Request)[];
|
|
30
|
+
/**
|
|
31
|
+
* Options for `warmEntries`.
|
|
32
|
+
*/
|
|
33
|
+
warmOptions?: WarmRuntimeCacheOptions;
|
|
34
|
+
/**
|
|
35
|
+
* Precaches routes so that they can be used as a fallback when
|
|
36
|
+
* a {@linkcode Strategy} fails to generate a response.
|
|
37
|
+
*
|
|
38
|
+
* Note: This option mutates `runtimeCaching`. It also expects the URLs
|
|
39
|
+
* defined in `entries` to have been precached beforehand.
|
|
40
|
+
*/
|
|
41
|
+
fallbacks?: FallbacksOptions;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export class RuntimeCacheController implements Controller {
|
|
45
|
+
_entries: RuntimeCaching[];
|
|
46
|
+
_options: RuntimeCacheControllerOptions;
|
|
47
|
+
constructor(entries: RuntimeCaching[], options: RuntimeCacheControllerOptions = {}) {
|
|
48
|
+
this._entries = entries;
|
|
49
|
+
this._options = options;
|
|
50
|
+
this.init = this.init.bind(this);
|
|
51
|
+
this.install = this.install.bind(this);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
init({ serwist }: { serwist: Serwist }): void {
|
|
55
|
+
if (this._options.fallbacks !== undefined) {
|
|
56
|
+
const fallbackPlugin = new PrecacheFallbackPlugin({
|
|
57
|
+
fallbackUrls: this._options.fallbacks.entries,
|
|
58
|
+
precacheController: serwist.precache,
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
this._entries.forEach((cacheEntry) => {
|
|
62
|
+
if (
|
|
63
|
+
cacheEntry.handler instanceof Strategy &&
|
|
64
|
+
// This also filters entries with `PrecacheFallbackPlugin` as it also has `handlerDidError`.
|
|
65
|
+
!cacheEntry.handler.plugins.some((plugin) => "handlerDidError" in plugin)
|
|
66
|
+
) {
|
|
67
|
+
cacheEntry.handler.plugins.push(fallbackPlugin);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
for (const entry of this._entries) {
|
|
72
|
+
serwist.registerCapture(entry.matcher, entry.handler, entry.method);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async install({ event, serwist }: { event: ExtendableEvent; serwist: Serwist }) {
|
|
77
|
+
const concurrency = this._options.warmOptions?.concurrency ?? 10;
|
|
78
|
+
if (this._options.warmEntries) {
|
|
79
|
+
await parallel(concurrency, this._options.warmEntries, async (entry) => {
|
|
80
|
+
const request =
|
|
81
|
+
entry instanceof Request
|
|
82
|
+
? entry
|
|
83
|
+
: new Request(typeof entry === "string" ? entry : entry.url, {
|
|
84
|
+
integrity: typeof entry !== "string" ? entry.integrity : undefined,
|
|
85
|
+
credentials: "same-origin",
|
|
86
|
+
});
|
|
87
|
+
await serwist.handleRequest({
|
|
88
|
+
request,
|
|
89
|
+
event,
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Any assets you wish to cache ahead of time which can't be revisioned
|
|
97
|
+
* should be cached with this method. All assets are cached on install
|
|
98
|
+
* regardless of whether an older version of the request is in the cache.
|
|
99
|
+
*
|
|
100
|
+
* The input can be a string or a [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request).
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```js
|
|
104
|
+
* // For unrevisioned assets that should always be downloaded
|
|
105
|
+
* // with every service worker update, use this method.
|
|
106
|
+
* serwist.warmRuntimeCache([
|
|
107
|
+
* "/scripts/main.js',
|
|
108
|
+
* "/images/default-avater.png",
|
|
109
|
+
* new Request("/images/logo.png"),
|
|
110
|
+
* new Request("/api/data.json"),
|
|
111
|
+
* ]);
|
|
112
|
+
* ```
|
|
113
|
+
* @param entries A set of urls to cache when the service worker is installed.
|
|
114
|
+
*/
|
|
115
|
+
warmRuntimeCache(entries: (WarmRuntimeCacheEntry | string | Request)[]): void {
|
|
116
|
+
if (!this._options.warmEntries) this._options.warmEntries = [];
|
|
117
|
+
this._options.warmEntries.push(...entries);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -6,9 +6,9 @@
|
|
|
6
6
|
https://opensource.org/licenses/MIT.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import { SerwistError } from "
|
|
10
|
-
import { assert } from "
|
|
11
|
-
import { logger } from "
|
|
9
|
+
import { SerwistError } from "#utils/SerwistError.js";
|
|
10
|
+
import { assert } from "#utils/assert.js";
|
|
11
|
+
import { logger } from "#utils/logger.js";
|
|
12
12
|
import { CacheTimestampsModel } from "./models/CacheTimestampsModel.js";
|
|
13
13
|
|
|
14
14
|
interface CacheExpirationConfig {
|