@serwist/precaching 9.0.0-preview.17 → 9.0.0-preview.19
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/index.d.ts +5 -26
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -155
- package/package.json +5 -20
- package/src/index.ts +5 -39
- package/dist/PrecacheController.d.ts +0 -146
- package/dist/PrecacheController.d.ts.map +0 -1
- package/dist/PrecacheFallbackPlugin.d.ts +0 -54
- package/dist/PrecacheFallbackPlugin.d.ts.map +0 -1
- package/dist/PrecacheRoute.d.ts +0 -20
- package/dist/PrecacheRoute.d.ts.map +0 -1
- package/dist/PrecacheStrategy.d.ts +0 -67
- package/dist/PrecacheStrategy.d.ts.map +0 -1
- package/dist/_types.d.ts +0 -37
- package/dist/_types.d.ts.map +0 -1
- package/dist/addPlugins.d.ts +0 -9
- package/dist/addPlugins.d.ts.map +0 -1
- package/dist/addRoute.d.ts +0 -15
- package/dist/addRoute.d.ts.map +0 -1
- package/dist/chunks/getOrCreatePrecacheController.js +0 -436
- package/dist/cleanupOutdatedCaches.d.ts +0 -6
- package/dist/cleanupOutdatedCaches.d.ts.map +0 -1
- package/dist/createHandlerBoundToURL.d.ts +0 -17
- package/dist/createHandlerBoundToURL.d.ts.map +0 -1
- package/dist/getCacheKeyForURL.d.ts +0 -20
- package/dist/getCacheKeyForURL.d.ts.map +0 -1
- package/dist/index.internal.d.ts +0 -3
- package/dist/index.internal.d.ts.map +0 -1
- package/dist/index.internal.js +0 -4
- package/dist/matchPrecache.d.ts +0 -15
- package/dist/matchPrecache.d.ts.map +0 -1
- package/dist/precache.d.ts +0 -19
- package/dist/precache.d.ts.map +0 -1
- package/dist/precacheAndRoute.d.ts +0 -14
- package/dist/precacheAndRoute.d.ts.map +0 -1
- package/dist/utils/PrecacheCacheKeyPlugin.d.ts +0 -17
- package/dist/utils/PrecacheCacheKeyPlugin.d.ts.map +0 -1
- package/dist/utils/PrecacheInstallReportPlugin.d.ts +0 -15
- package/dist/utils/PrecacheInstallReportPlugin.d.ts.map +0 -1
- package/dist/utils/createCacheKey.d.ts +0 -16
- package/dist/utils/createCacheKey.d.ts.map +0 -1
- package/dist/utils/deleteOutdatedCaches.d.ts +0 -18
- package/dist/utils/deleteOutdatedCaches.d.ts.map +0 -1
- package/dist/utils/generateURLVariations.d.ts +0 -12
- package/dist/utils/generateURLVariations.d.ts.map +0 -1
- package/dist/utils/getCacheKeyForURL.d.ts +0 -14
- package/dist/utils/getCacheKeyForURL.d.ts.map +0 -1
- package/dist/utils/getOrCreatePrecacheController.d.ts +0 -7
- package/dist/utils/getOrCreatePrecacheController.d.ts.map +0 -1
- package/dist/utils/printCleanupDetails.d.ts +0 -6
- package/dist/utils/printCleanupDetails.d.ts.map +0 -1
- package/dist/utils/printInstallDetails.d.ts +0 -7
- package/dist/utils/printInstallDetails.d.ts.map +0 -1
- package/dist/utils/removeIgnoredSearchParams.d.ts +0 -12
- package/dist/utils/removeIgnoredSearchParams.d.ts.map +0 -1
- package/src/PrecacheController.ts +0 -344
- package/src/PrecacheFallbackPlugin.ts +0 -86
- package/src/PrecacheRoute.ts +0 -50
- package/src/PrecacheStrategy.ts +0 -239
- package/src/_types.ts +0 -46
- package/src/addPlugins.ts +0 -23
- package/src/addRoute.ts +0 -31
- package/src/cleanupOutdatedCaches.ts +0 -33
- package/src/createHandlerBoundToURL.ts +0 -30
- package/src/getCacheKeyForURL.ts +0 -33
- package/src/index.internal.ts +0 -3
- package/src/matchPrecache.ts +0 -28
- package/src/precache.ts +0 -31
- package/src/precacheAndRoute.ts +0 -27
- package/src/utils/PrecacheCacheKeyPlugin.ts +0 -36
- package/src/utils/PrecacheInstallReportPlugin.ts +0 -49
- package/src/utils/createCacheKey.ts +0 -68
- package/src/utils/deleteOutdatedCaches.ts +0 -40
- package/src/utils/generateURLVariations.ts +0 -55
- package/src/utils/getCacheKeyForURL.ts +0 -36
- package/src/utils/getOrCreatePrecacheController.ts +0 -22
- package/src/utils/printCleanupDetails.ts +0 -38
- package/src/utils/printInstallDetails.ts +0 -53
- package/src/utils/removeIgnoredSearchParams.ts +0 -29
package/src/PrecacheStrategy.ts
DELETED
|
@@ -1,239 +0,0 @@
|
|
|
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 } from "@serwist/core";
|
|
10
|
-
import { copyResponse } from "@serwist/core";
|
|
11
|
-
import { SerwistError, getFriendlyURL, logger, privateCacheNames } from "@serwist/core/internal";
|
|
12
|
-
import type { StrategyHandler, StrategyOptions } from "@serwist/strategies";
|
|
13
|
-
import { Strategy } from "@serwist/strategies";
|
|
14
|
-
|
|
15
|
-
interface PrecacheStrategyOptions extends StrategyOptions {
|
|
16
|
-
/**
|
|
17
|
-
* Whether to attempt to get the response from the network
|
|
18
|
-
* if there's a precache miss.
|
|
19
|
-
*/
|
|
20
|
-
fallbackToNetwork?: boolean;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* A `@serwist/strategies.Strategy` implementation
|
|
25
|
-
* specifically designed to work with
|
|
26
|
-
* `@serwist/precaching.PrecacheController`
|
|
27
|
-
* to both cache and fetch precached assets.
|
|
28
|
-
*
|
|
29
|
-
* Note: an instance of this class is created automatically when creating a
|
|
30
|
-
* `PrecacheController`; it's generally not necessary to create this yourself.
|
|
31
|
-
*/
|
|
32
|
-
class PrecacheStrategy extends Strategy {
|
|
33
|
-
private readonly _fallbackToNetwork: boolean;
|
|
34
|
-
|
|
35
|
-
static readonly defaultPrecacheCacheabilityPlugin: SerwistPlugin = {
|
|
36
|
-
async cacheWillUpdate({ response }) {
|
|
37
|
-
if (!response || response.status >= 400) {
|
|
38
|
-
return null;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return response;
|
|
42
|
-
},
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
static readonly copyRedirectedCacheableResponsesPlugin: SerwistPlugin = {
|
|
46
|
-
async cacheWillUpdate({ response }) {
|
|
47
|
-
return response.redirected ? await copyResponse(response) : response;
|
|
48
|
-
},
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* @param options
|
|
53
|
-
*/
|
|
54
|
-
constructor(options: PrecacheStrategyOptions = {}) {
|
|
55
|
-
options.cacheName = privateCacheNames.getPrecacheName(options.cacheName);
|
|
56
|
-
super(options);
|
|
57
|
-
|
|
58
|
-
this._fallbackToNetwork = options.fallbackToNetwork === false ? false : true;
|
|
59
|
-
|
|
60
|
-
// Redirected responses cannot be used to satisfy a navigation request, so
|
|
61
|
-
// any redirected response must be "copied" rather than cloned, so the new
|
|
62
|
-
// response doesn't contain the `redirected` flag. See:
|
|
63
|
-
// https://bugs.chromium.org/p/chromium/issues/detail?id=669363&desc=2#c1
|
|
64
|
-
this.plugins.push(PrecacheStrategy.copyRedirectedCacheableResponsesPlugin);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* @private
|
|
69
|
-
* @param request A request to run this strategy for.
|
|
70
|
-
* @param handler The event that triggered the request.
|
|
71
|
-
* @returns
|
|
72
|
-
*/
|
|
73
|
-
async _handle(request: Request, handler: StrategyHandler): Promise<Response> {
|
|
74
|
-
const response = await handler.cacheMatch(request);
|
|
75
|
-
if (response) {
|
|
76
|
-
return response;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// If this is an `install` event for an entry that isn't already cached,
|
|
80
|
-
// then populate the cache.
|
|
81
|
-
if (handler.event && handler.event.type === "install") {
|
|
82
|
-
return await this._handleInstall(request, handler);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// Getting here means something went wrong. An entry that should have been
|
|
86
|
-
// precached wasn't found in the cache.
|
|
87
|
-
return await this._handleFetch(request, handler);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
async _handleFetch(request: Request, handler: StrategyHandler): Promise<Response> {
|
|
91
|
-
let response: Response | undefined = undefined;
|
|
92
|
-
const params = (handler.params || {}) as {
|
|
93
|
-
cacheKey?: string;
|
|
94
|
-
integrity?: string;
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
// Fallback to the network if we're configured to do so.
|
|
98
|
-
if (this._fallbackToNetwork) {
|
|
99
|
-
if (process.env.NODE_ENV !== "production") {
|
|
100
|
-
logger.warn(`The precached response for ${getFriendlyURL(request.url)} in ${this.cacheName} was not found. Falling back to the network.`);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const integrityInManifest = params.integrity;
|
|
104
|
-
const integrityInRequest = request.integrity;
|
|
105
|
-
const noIntegrityConflict = !integrityInRequest || integrityInRequest === integrityInManifest;
|
|
106
|
-
|
|
107
|
-
// Do not add integrity if the original request is no-cors
|
|
108
|
-
// See https://github.com/GoogleChrome/workbox/issues/3096
|
|
109
|
-
response = await handler.fetch(
|
|
110
|
-
new Request(request, {
|
|
111
|
-
integrity: request.mode !== "no-cors" ? integrityInRequest || integrityInManifest : undefined,
|
|
112
|
-
}),
|
|
113
|
-
);
|
|
114
|
-
|
|
115
|
-
// It's only "safe" to repair the cache if we're using SRI to guarantee
|
|
116
|
-
// that the response matches the precache manifest's expectations,
|
|
117
|
-
// and there's either a) no integrity property in the incoming request
|
|
118
|
-
// or b) there is an integrity, and it matches the precache manifest.
|
|
119
|
-
// See https://github.com/GoogleChrome/workbox/issues/2858
|
|
120
|
-
// Also if the original request users no-cors we don't use integrity.
|
|
121
|
-
// See https://github.com/GoogleChrome/workbox/issues/3096
|
|
122
|
-
if (integrityInManifest && noIntegrityConflict && request.mode !== "no-cors") {
|
|
123
|
-
this._useDefaultCacheabilityPluginIfNeeded();
|
|
124
|
-
const wasCached = await handler.cachePut(request, response.clone());
|
|
125
|
-
if (process.env.NODE_ENV !== "production") {
|
|
126
|
-
if (wasCached) {
|
|
127
|
-
logger.log(`A response for ${getFriendlyURL(request.url)} was used to "repair" the precache.`);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
} else {
|
|
132
|
-
// This shouldn't normally happen, but there are edge cases:
|
|
133
|
-
// https://github.com/GoogleChrome/workbox/issues/1441
|
|
134
|
-
throw new SerwistError("missing-precache-entry", {
|
|
135
|
-
cacheName: this.cacheName,
|
|
136
|
-
url: request.url,
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
if (process.env.NODE_ENV !== "production") {
|
|
141
|
-
const cacheKey = params.cacheKey || (await handler.getCacheKey(request, "read"));
|
|
142
|
-
|
|
143
|
-
// Serwist is going to handle the route.
|
|
144
|
-
// print the routing details to the console.
|
|
145
|
-
logger.groupCollapsed(`Precaching is responding to: ${getFriendlyURL(request.url)}`);
|
|
146
|
-
logger.log(`Serving the precached url: ${getFriendlyURL(cacheKey instanceof Request ? cacheKey.url : cacheKey)}`);
|
|
147
|
-
|
|
148
|
-
logger.groupCollapsed("View request details here.");
|
|
149
|
-
logger.log(request);
|
|
150
|
-
logger.groupEnd();
|
|
151
|
-
|
|
152
|
-
logger.groupCollapsed("View response details here.");
|
|
153
|
-
logger.log(response);
|
|
154
|
-
logger.groupEnd();
|
|
155
|
-
|
|
156
|
-
logger.groupEnd();
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
return response;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
async _handleInstall(request: Request, handler: StrategyHandler): Promise<Response> {
|
|
163
|
-
this._useDefaultCacheabilityPluginIfNeeded();
|
|
164
|
-
|
|
165
|
-
const response = await handler.fetch(request);
|
|
166
|
-
|
|
167
|
-
// Make sure we defer cachePut() until after we know the response
|
|
168
|
-
// should be cached; see https://github.com/GoogleChrome/workbox/issues/2737
|
|
169
|
-
const wasCached = await handler.cachePut(request, response.clone());
|
|
170
|
-
if (!wasCached) {
|
|
171
|
-
// Throwing here will lead to the `install` handler failing, which
|
|
172
|
-
// we want to do if *any* of the responses aren't safe to cache.
|
|
173
|
-
throw new SerwistError("bad-precaching-response", {
|
|
174
|
-
url: request.url,
|
|
175
|
-
status: response.status,
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
return response;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* This method is complex, as there a number of things to account for:
|
|
184
|
-
*
|
|
185
|
-
* The `plugins` array can be set at construction, and/or it might be added to
|
|
186
|
-
* to at any time before the strategy is used.
|
|
187
|
-
*
|
|
188
|
-
* At the time the strategy is used (i.e. during an `install` event), there
|
|
189
|
-
* needs to be at least one plugin that implements `cacheWillUpdate` in the
|
|
190
|
-
* array, other than `copyRedirectedCacheableResponsesPlugin`.
|
|
191
|
-
*
|
|
192
|
-
* - If this method is called and there are no suitable `cacheWillUpdate`
|
|
193
|
-
* plugins, we need to add `defaultPrecacheCacheabilityPlugin`.
|
|
194
|
-
*
|
|
195
|
-
* - If this method is called and there is exactly one `cacheWillUpdate`, then
|
|
196
|
-
* we don't have to do anything (this might be a previously added
|
|
197
|
-
* `defaultPrecacheCacheabilityPlugin`, or it might be a custom plugin).
|
|
198
|
-
*
|
|
199
|
-
* - If this method is called and there is more than one `cacheWillUpdate`,
|
|
200
|
-
* then we need to check if one is `defaultPrecacheCacheabilityPlugin`. If so,
|
|
201
|
-
* we need to remove it. (This situation is unlikely, but it could happen if
|
|
202
|
-
* the strategy is used multiple times, the first without a `cacheWillUpdate`,
|
|
203
|
-
* and then later on after manually adding a custom `cacheWillUpdate`.)
|
|
204
|
-
*
|
|
205
|
-
* See https://github.com/GoogleChrome/workbox/issues/2737 for more context.
|
|
206
|
-
*
|
|
207
|
-
* @private
|
|
208
|
-
*/
|
|
209
|
-
_useDefaultCacheabilityPluginIfNeeded(): void {
|
|
210
|
-
let defaultPluginIndex: number | null = null;
|
|
211
|
-
let cacheWillUpdatePluginCount = 0;
|
|
212
|
-
|
|
213
|
-
for (const [index, plugin] of this.plugins.entries()) {
|
|
214
|
-
// Ignore the copy redirected plugin when determining what to do.
|
|
215
|
-
if (plugin === PrecacheStrategy.copyRedirectedCacheableResponsesPlugin) {
|
|
216
|
-
continue;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
// Save the default plugin's index, in case it needs to be removed.
|
|
220
|
-
if (plugin === PrecacheStrategy.defaultPrecacheCacheabilityPlugin) {
|
|
221
|
-
defaultPluginIndex = index;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
if (plugin.cacheWillUpdate) {
|
|
225
|
-
cacheWillUpdatePluginCount++;
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
if (cacheWillUpdatePluginCount === 0) {
|
|
230
|
-
this.plugins.push(PrecacheStrategy.defaultPrecacheCacheabilityPlugin);
|
|
231
|
-
} else if (cacheWillUpdatePluginCount > 1 && defaultPluginIndex !== null) {
|
|
232
|
-
// Only remove the default plugin; multiple custom plugins are allowed.
|
|
233
|
-
this.plugins.splice(defaultPluginIndex, 1);
|
|
234
|
-
}
|
|
235
|
-
// Nothing needs to be done if cacheWillUpdatePluginCount is 1
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
export { PrecacheStrategy };
|
package/src/_types.ts
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2018 Google LLC
|
|
3
|
-
|
|
4
|
-
Use of this source code is governed by an MIT-style
|
|
5
|
-
license that can be found in the LICENSE file or at
|
|
6
|
-
https://opensource.org/licenses/MIT.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
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[];
|
package/src/addPlugins.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2019 Google LLC
|
|
3
|
-
|
|
4
|
-
Use of this source code is governed by an MIT-style
|
|
5
|
-
license that can be found in the LICENSE file or at
|
|
6
|
-
https://opensource.org/licenses/MIT.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import type { SerwistPlugin } from "@serwist/core";
|
|
10
|
-
|
|
11
|
-
import { getOrCreatePrecacheController } from "./utils/getOrCreatePrecacheController.js";
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Adds plugins to the precaching strategy.
|
|
15
|
-
*
|
|
16
|
-
* @param plugins
|
|
17
|
-
*/
|
|
18
|
-
function addPlugins(plugins: SerwistPlugin[]): void {
|
|
19
|
-
const precacheController = getOrCreatePrecacheController();
|
|
20
|
-
precacheController.strategy.plugins.push(...plugins);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export { addPlugins };
|
package/src/addRoute.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
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 "@serwist/routing";
|
|
9
|
-
|
|
10
|
-
import { PrecacheRoute } from "./PrecacheRoute.js";
|
|
11
|
-
import type { PrecacheRouteOptions } from "./_types.js";
|
|
12
|
-
import { getOrCreatePrecacheController } from "./utils/getOrCreatePrecacheController.js";
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Add a `fetch` listener to the service worker that will
|
|
16
|
-
* respond to
|
|
17
|
-
* [network requests](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers#Custom_responses_to_requests)
|
|
18
|
-
* with precached assets.
|
|
19
|
-
*
|
|
20
|
-
* Requests for assets that aren't precached, the `FetchEvent` will not be
|
|
21
|
-
* responded to, allowing the event to fall through to other `fetch` event
|
|
22
|
-
* listeners.
|
|
23
|
-
*
|
|
24
|
-
* @param options See the `@serwist/precaching.PrecacheRoute` options.
|
|
25
|
-
*/
|
|
26
|
-
export const addRoute = (options?: PrecacheRouteOptions): void => {
|
|
27
|
-
const precacheController = getOrCreatePrecacheController();
|
|
28
|
-
|
|
29
|
-
const precacheRoute = new PrecacheRoute(precacheController, options);
|
|
30
|
-
registerRoute(precacheRoute);
|
|
31
|
-
};
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2019 Google LLC
|
|
3
|
-
|
|
4
|
-
Use of this source code is governed by an MIT-style
|
|
5
|
-
license that can be found in the LICENSE file or at
|
|
6
|
-
https://opensource.org/licenses/MIT.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { 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
|
-
};
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2019 Google LLC
|
|
3
|
-
|
|
4
|
-
Use of this source code is governed by an MIT-style
|
|
5
|
-
license that can be found in the LICENSE file or at
|
|
6
|
-
https://opensource.org/licenses/MIT.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import type { RouteHandlerCallback } from "@serwist/core";
|
|
10
|
-
|
|
11
|
-
import { getOrCreatePrecacheController } from "./utils/getOrCreatePrecacheController.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 = getOrCreatePrecacheController();
|
|
29
|
-
return precacheController.createHandlerBoundToURL(url);
|
|
30
|
-
};
|
package/src/getCacheKeyForURL.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2019 Google LLC
|
|
3
|
-
|
|
4
|
-
Use of this source code is governed by an MIT-style
|
|
5
|
-
license that can be found in the LICENSE file or at
|
|
6
|
-
https://opensource.org/licenses/MIT.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { getOrCreatePrecacheController } from "./utils/getOrCreatePrecacheController.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 = getOrCreatePrecacheController();
|
|
30
|
-
return precacheController.getCacheKeyForURL(url);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export { getCacheKeyForURL };
|
package/src/index.internal.ts
DELETED
package/src/matchPrecache.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2019 Google LLC
|
|
3
|
-
|
|
4
|
-
Use of this source code is governed by an MIT-style
|
|
5
|
-
license that can be found in the LICENSE file or at
|
|
6
|
-
https://opensource.org/licenses/MIT.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { getOrCreatePrecacheController } from "./utils/getOrCreatePrecacheController.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
|
-
function matchPrecache(request: string | Request): Promise<Response | undefined> {
|
|
24
|
-
const precacheController = getOrCreatePrecacheController();
|
|
25
|
-
return precacheController.matchPrecache(request);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export { matchPrecache };
|
package/src/precache.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2019 Google LLC
|
|
3
|
-
|
|
4
|
-
Use of this source code is governed by an MIT-style
|
|
5
|
-
license that can be found in the LICENSE file or at
|
|
6
|
-
https://opensource.org/licenses/MIT.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import type { PrecacheEntry } from "./_types.js";
|
|
10
|
-
import { getOrCreatePrecacheController } from "./utils/getOrCreatePrecacheController.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 = getOrCreatePrecacheController();
|
|
30
|
-
precacheController.precache(entries);
|
|
31
|
-
};
|
package/src/precacheAndRoute.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2019 Google LLC
|
|
3
|
-
|
|
4
|
-
Use of this source code is governed by an MIT-style
|
|
5
|
-
license that can be found in the LICENSE file or at
|
|
6
|
-
https://opensource.org/licenses/MIT.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import type { PrecacheEntry, PrecacheRouteOptions } from "./_types.js";
|
|
10
|
-
import { addRoute } from "./addRoute.js";
|
|
11
|
-
import { precache } from "./precache.js";
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* This method will add entries to the precache list and add a route to
|
|
15
|
-
* respond to fetch events.
|
|
16
|
-
*
|
|
17
|
-
* This is a convenience method that will call
|
|
18
|
-
* `@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
|
-
};
|
|
@@ -1,36 +0,0 @@
|
|
|
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 };
|
|
@@ -1,49 +0,0 @@
|
|
|
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 };
|