@serwist/precaching 8.4.3 → 9.0.0-preview.0
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/PrecacheController.d.ts +4 -3
- package/dist/PrecacheController.d.ts.map +1 -0
- package/dist/PrecacheFallbackPlugin.d.ts +1 -0
- package/dist/PrecacheFallbackPlugin.d.ts.map +1 -0
- package/dist/PrecacheRoute.d.ts +1 -0
- package/dist/PrecacheRoute.d.ts.map +1 -0
- package/dist/PrecacheStrategy.d.ts +1 -0
- package/dist/PrecacheStrategy.d.ts.map +1 -0
- package/dist/_types.d.ts +3 -2
- package/dist/_types.d.ts.map +1 -0
- package/dist/addPlugins.d.ts +1 -0
- package/dist/addPlugins.d.ts.map +1 -0
- package/dist/addRoute.d.ts +1 -0
- package/dist/addRoute.d.ts.map +1 -0
- package/dist/cleanupOutdatedCaches.d.ts +1 -0
- package/dist/cleanupOutdatedCaches.d.ts.map +1 -0
- package/dist/createHandlerBoundToURL.d.ts +1 -0
- package/dist/createHandlerBoundToURL.d.ts.map +1 -0
- package/dist/getCacheKeyForURL.d.ts +1 -0
- package/dist/getCacheKeyForURL.d.ts.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/matchPrecache.d.ts +1 -0
- package/dist/matchPrecache.d.ts.map +1 -0
- package/dist/precache.d.ts +2 -1
- package/dist/precache.d.ts.map +1 -0
- package/dist/precacheAndRoute.d.ts +2 -1
- package/dist/precacheAndRoute.d.ts.map +1 -0
- package/dist/utils/PrecacheCacheKeyPlugin.d.ts +1 -0
- package/dist/utils/PrecacheCacheKeyPlugin.d.ts.map +1 -0
- package/dist/utils/PrecacheInstallReportPlugin.d.ts +1 -0
- package/dist/utils/PrecacheInstallReportPlugin.d.ts.map +1 -0
- package/dist/utils/createCacheKey.d.ts +1 -0
- package/dist/utils/createCacheKey.d.ts.map +1 -0
- package/dist/utils/deleteOutdatedCaches.d.ts +1 -0
- package/dist/utils/deleteOutdatedCaches.d.ts.map +1 -0
- package/dist/utils/generateURLVariations.d.ts +1 -0
- package/dist/utils/generateURLVariations.d.ts.map +1 -0
- package/dist/utils/getCacheKeyForURL.d.ts +1 -0
- package/dist/utils/getCacheKeyForURL.d.ts.map +1 -0
- package/dist/utils/getOrCreatePrecacheController.d.ts +1 -0
- package/dist/utils/getOrCreatePrecacheController.d.ts.map +1 -0
- package/dist/utils/printCleanupDetails.d.ts +1 -0
- package/dist/utils/printCleanupDetails.d.ts.map +1 -0
- package/dist/utils/printInstallDetails.d.ts +1 -0
- package/dist/utils/printInstallDetails.d.ts.map +1 -0
- package/dist/utils/removeIgnoredSearchParams.d.ts +1 -0
- package/dist/utils/removeIgnoredSearchParams.d.ts.map +1 -0
- package/package.json +20 -18
- package/src/PrecacheController.ts +331 -0
- package/src/PrecacheFallbackPlugin.ts +61 -0
- package/src/PrecacheRoute.ts +50 -0
- package/src/PrecacheStrategy.ts +239 -0
- package/src/_types.ts +46 -0
- package/src/addPlugins.ts +23 -0
- package/src/addRoute.ts +33 -0
- package/src/cleanupOutdatedCaches.ts +34 -0
- package/src/createHandlerBoundToURL.ts +32 -0
- package/src/getCacheKeyForURL.ts +33 -0
- package/{dist/index.d.cts → src/index.ts} +25 -1
- package/src/matchPrecache.ts +28 -0
- package/src/precache.ts +33 -0
- package/src/precacheAndRoute.ts +29 -0
- package/src/utils/PrecacheCacheKeyPlugin.ts +36 -0
- package/src/utils/PrecacheInstallReportPlugin.ts +49 -0
- package/src/utils/createCacheKey.ts +68 -0
- package/src/utils/deleteOutdatedCaches.ts +42 -0
- package/src/utils/generateURLVariations.ts +55 -0
- package/src/utils/getCacheKeyForURL.ts +36 -0
- package/src/utils/getOrCreatePrecacheController.ts +22 -0
- package/src/utils/printCleanupDetails.ts +38 -0
- package/src/utils/printInstallDetails.ts +53 -0
- package/src/utils/removeIgnoredSearchParams.ts +29 -0
- package/dist/index.cjs +0 -930
package/src/_types.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export interface InstallResult {
|
|
10
|
+
updatedURLs: string[];
|
|
11
|
+
notUpdatedURLs: string[];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface CleanupResult {
|
|
15
|
+
deletedCacheRequests: string[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export declare interface PrecacheEntry {
|
|
19
|
+
integrity?: string;
|
|
20
|
+
url: string;
|
|
21
|
+
revision?: string | null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface PrecacheRouteOptions {
|
|
25
|
+
/**
|
|
26
|
+
* The `directoryIndex` will check cache entries for a URL ending with '/'
|
|
27
|
+
* to see if there is a hit when appending the `directoryIndex` value.
|
|
28
|
+
*/
|
|
29
|
+
directoryIndex?: string | null;
|
|
30
|
+
/**
|
|
31
|
+
* An array of RegExp's to remove search params when looking for a cache match.
|
|
32
|
+
*/
|
|
33
|
+
ignoreURLParametersMatching?: RegExp[];
|
|
34
|
+
/**
|
|
35
|
+
* The `cleanURLs` option will check the cache for the URL with a `.html` added
|
|
36
|
+
* to the end of the end.
|
|
37
|
+
*/
|
|
38
|
+
cleanURLs?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* This is a function that should take a URL and return an array of
|
|
41
|
+
* alternative URLs that should be checked for precache matches.
|
|
42
|
+
*/
|
|
43
|
+
urlManipulation?: urlManipulation;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export type urlManipulation = ({ url }: { url: URL }) => URL[];
|
|
@@ -0,0 +1,23 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
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
|
+
function addRoute(options?: PrecacheRouteOptions): void {
|
|
27
|
+
const precacheController = getOrCreatePrecacheController();
|
|
28
|
+
|
|
29
|
+
const precacheRoute = new PrecacheRoute(precacheController, options);
|
|
30
|
+
registerRoute(precacheRoute);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export { addRoute };
|
|
@@ -0,0 +1,34 @@
|
|
|
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
|
+
/**
|
|
14
|
+
* Adds an `activate` event listener which will clean up incompatible
|
|
15
|
+
* precaches that were created by older versions of Serwist.
|
|
16
|
+
*/
|
|
17
|
+
function cleanupOutdatedCaches(): void {
|
|
18
|
+
// See https://github.com/Microsoft/TypeScript/issues/28357#issuecomment-436484705
|
|
19
|
+
self.addEventListener("activate", ((event: ExtendableEvent) => {
|
|
20
|
+
const cacheName = privateCacheNames.getPrecacheName();
|
|
21
|
+
|
|
22
|
+
event.waitUntil(
|
|
23
|
+
deleteOutdatedCaches(cacheName).then((cachesDeleted) => {
|
|
24
|
+
if (process.env.NODE_ENV !== "production") {
|
|
25
|
+
if (cachesDeleted.length > 0) {
|
|
26
|
+
logger.log("The following out-of-date precaches were cleaned up " + "automatically:", cachesDeleted);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}),
|
|
30
|
+
);
|
|
31
|
+
}) as EventListener);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export { cleanupOutdatedCaches };
|
|
@@ -0,0 +1,32 @@
|
|
|
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
|
+
function createHandlerBoundToURL(url: string): RouteHandlerCallback {
|
|
28
|
+
const precacheController = getOrCreatePrecacheController();
|
|
29
|
+
return precacheController.createHandlerBoundToURL(url);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export { createHandlerBoundToURL };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2019 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { 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 };
|
|
@@ -1,3 +1,11 @@
|
|
|
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
|
+
|
|
1
9
|
import { PrecacheController } from "./PrecacheController.js";
|
|
2
10
|
import { PrecacheFallbackPlugin } from "./PrecacheFallbackPlugin.js";
|
|
3
11
|
import { PrecacheRoute } from "./PrecacheRoute.js";
|
|
@@ -10,6 +18,7 @@ import { getCacheKeyForURL } from "./getCacheKeyForURL.js";
|
|
|
10
18
|
import { matchPrecache } from "./matchPrecache.js";
|
|
11
19
|
import { precache } from "./precache.js";
|
|
12
20
|
import { precacheAndRoute } from "./precacheAndRoute.js";
|
|
21
|
+
|
|
13
22
|
/**
|
|
14
23
|
* Most consumers of this module will want to use the
|
|
15
24
|
* `@serwist/precaching.precacheAndRoute`
|
|
@@ -20,5 +29,20 @@ import { precacheAndRoute } from "./precacheAndRoute.js";
|
|
|
20
29
|
* `@serwist/precaching.PrecacheController`
|
|
21
30
|
* interface.
|
|
22
31
|
*/
|
|
23
|
-
|
|
32
|
+
|
|
33
|
+
export {
|
|
34
|
+
addPlugins,
|
|
35
|
+
addRoute,
|
|
36
|
+
cleanupOutdatedCaches,
|
|
37
|
+
createHandlerBoundToURL,
|
|
38
|
+
getCacheKeyForURL,
|
|
39
|
+
matchPrecache,
|
|
40
|
+
precache,
|
|
41
|
+
precacheAndRoute,
|
|
42
|
+
PrecacheController,
|
|
43
|
+
PrecacheFallbackPlugin,
|
|
44
|
+
PrecacheRoute,
|
|
45
|
+
PrecacheStrategy,
|
|
46
|
+
};
|
|
47
|
+
|
|
24
48
|
export * from "./_types.js";
|
|
@@ -0,0 +1,28 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2019 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import 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
|
+
function precache(entries: (PrecacheEntry | string)[]): void {
|
|
29
|
+
const precacheController = getOrCreatePrecacheController();
|
|
30
|
+
precacheController.precache(entries);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export { precache };
|
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
function precacheAndRoute(entries: (PrecacheEntry | string)[], options?: PrecacheRouteOptions): void {
|
|
25
|
+
precache(entries);
|
|
26
|
+
addRoute(options);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export { precacheAndRoute };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { SerwistPlugin, SerwistPluginCallbackParam } from "@serwist/core";
|
|
10
|
+
|
|
11
|
+
import type { PrecacheController } from "../PrecacheController.js";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A plugin, designed to be used with PrecacheController, to translate URLs into
|
|
15
|
+
* the corresponding cache key, based on the current revision info.
|
|
16
|
+
*
|
|
17
|
+
* @private
|
|
18
|
+
*/
|
|
19
|
+
class PrecacheCacheKeyPlugin implements SerwistPlugin {
|
|
20
|
+
private readonly _precacheController: PrecacheController;
|
|
21
|
+
|
|
22
|
+
constructor({ precacheController }: { precacheController: PrecacheController }) {
|
|
23
|
+
this._precacheController = precacheController;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
cacheKeyWillBeUsed: SerwistPlugin["cacheKeyWillBeUsed"] = async ({ request, params }: SerwistPluginCallbackParam["cacheKeyWillBeUsed"]) => {
|
|
27
|
+
// Params is type any, can't change right now.
|
|
28
|
+
/* eslint-disable */
|
|
29
|
+
const cacheKey = params?.cacheKey || this._precacheController.getCacheKeyForURL(request.url);
|
|
30
|
+
/* eslint-enable */
|
|
31
|
+
|
|
32
|
+
return cacheKey ? new Request(cacheKey, { headers: request.headers }) : request;
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export { PrecacheCacheKeyPlugin };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { SerwistPlugin, SerwistPluginCallbackParam } from "@serwist/core";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A plugin, designed to be used with PrecacheController, to determine the
|
|
13
|
+
* of assets that were updated (or not updated) during the install event.
|
|
14
|
+
*
|
|
15
|
+
* @private
|
|
16
|
+
*/
|
|
17
|
+
class PrecacheInstallReportPlugin implements SerwistPlugin {
|
|
18
|
+
updatedURLs: string[] = [];
|
|
19
|
+
notUpdatedURLs: string[] = [];
|
|
20
|
+
|
|
21
|
+
handlerWillStart: SerwistPlugin["handlerWillStart"] = async ({ request, state }: SerwistPluginCallbackParam["handlerWillStart"]) => {
|
|
22
|
+
// TODO: `state` should never be undefined...
|
|
23
|
+
if (state) {
|
|
24
|
+
state.originalRequest = request;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
cachedResponseWillBeUsed: SerwistPlugin["cachedResponseWillBeUsed"] = async ({
|
|
29
|
+
event,
|
|
30
|
+
state,
|
|
31
|
+
cachedResponse,
|
|
32
|
+
}: SerwistPluginCallbackParam["cachedResponseWillBeUsed"]) => {
|
|
33
|
+
if (event.type === "install") {
|
|
34
|
+
if (state?.originalRequest && state.originalRequest instanceof Request) {
|
|
35
|
+
// TODO: `state` should never be undefined...
|
|
36
|
+
const url = state.originalRequest.url;
|
|
37
|
+
|
|
38
|
+
if (cachedResponse) {
|
|
39
|
+
this.notUpdatedURLs.push(url);
|
|
40
|
+
} else {
|
|
41
|
+
this.updatedURLs.push(url);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return cachedResponse;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export { PrecacheInstallReportPlugin };
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { SerwistError } from "@serwist/core/internal";
|
|
10
|
+
|
|
11
|
+
import type { PrecacheEntry } from "../_types.js";
|
|
12
|
+
|
|
13
|
+
interface CacheKey {
|
|
14
|
+
cacheKey: string;
|
|
15
|
+
url: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Name of the search parameter used to store revision info.
|
|
19
|
+
const REVISION_SEARCH_PARAM = "__WB_REVISION__";
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Converts a manifest entry into a versioned URL suitable for precaching.
|
|
23
|
+
*
|
|
24
|
+
* @param entry
|
|
25
|
+
* @returns A URL with versioning info.
|
|
26
|
+
*
|
|
27
|
+
* @private
|
|
28
|
+
*/
|
|
29
|
+
export function createCacheKey(entry: PrecacheEntry | string): CacheKey {
|
|
30
|
+
if (!entry) {
|
|
31
|
+
throw new SerwistError("add-to-cache-list-unexpected-type", { entry });
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// If a precache manifest entry is a string, it's assumed to be a versioned
|
|
35
|
+
// URL, like '/app.abcd1234.js'. Return as-is.
|
|
36
|
+
if (typeof entry === "string") {
|
|
37
|
+
const urlObject = new URL(entry, location.href);
|
|
38
|
+
return {
|
|
39
|
+
cacheKey: urlObject.href,
|
|
40
|
+
url: urlObject.href,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const { revision, url } = entry;
|
|
45
|
+
if (!url) {
|
|
46
|
+
throw new SerwistError("add-to-cache-list-unexpected-type", { entry });
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// If there's just a URL and no revision, then it's also assumed to be a
|
|
50
|
+
// versioned URL.
|
|
51
|
+
if (!revision) {
|
|
52
|
+
const urlObject = new URL(url, location.href);
|
|
53
|
+
return {
|
|
54
|
+
cacheKey: urlObject.href,
|
|
55
|
+
url: urlObject.href,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Otherwise, construct a properly versioned URL using the custom Serwist
|
|
60
|
+
// search parameter along with the revision info.
|
|
61
|
+
const cacheKeyURL = new URL(url, location.href);
|
|
62
|
+
const originalURL = new URL(url, location.href);
|
|
63
|
+
cacheKeyURL.searchParams.set(REVISION_SEARCH_PARAM, revision);
|
|
64
|
+
return {
|
|
65
|
+
cacheKey: cacheKeyURL.href,
|
|
66
|
+
url: originalURL.href,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
// Give TypeScript the correct global.
|
|
10
|
+
declare let self: ServiceWorkerGlobalScope;
|
|
11
|
+
|
|
12
|
+
const SUBSTRING_TO_FIND = "-precache-";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Cleans up incompatible precaches that were created by older versions of
|
|
16
|
+
* Serwist, by a service worker registered under the current scope.
|
|
17
|
+
*
|
|
18
|
+
* This is meant to be called as part of the `activate` event.
|
|
19
|
+
*
|
|
20
|
+
* This should be safe to use as long as you don't include `substringToFind`
|
|
21
|
+
* (defaulting to `-precache-`) in your non-precache cache names.
|
|
22
|
+
*
|
|
23
|
+
* @param currentPrecacheName The cache name currently in use for
|
|
24
|
+
* precaching. This cache won't be deleted.
|
|
25
|
+
* @param substringToFind Cache names which include this
|
|
26
|
+
* substring will be deleted (excluding `currentPrecacheName`).
|
|
27
|
+
* @returns A list of all the cache names that were deleted.
|
|
28
|
+
* @private
|
|
29
|
+
*/
|
|
30
|
+
const deleteOutdatedCaches = async (currentPrecacheName: string, substringToFind: string = SUBSTRING_TO_FIND): Promise<string[]> => {
|
|
31
|
+
const cacheNames = await self.caches.keys();
|
|
32
|
+
|
|
33
|
+
const cacheNamesToDelete = cacheNames.filter((cacheName) => {
|
|
34
|
+
return cacheName.includes(substringToFind) && cacheName.includes(self.registration.scope) && cacheName !== currentPrecacheName;
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
await Promise.all(cacheNamesToDelete.map((cacheName) => self.caches.delete(cacheName)));
|
|
38
|
+
|
|
39
|
+
return cacheNamesToDelete;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export { deleteOutdatedCaches };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2019 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { PrecacheRouteOptions } from "../_types.js";
|
|
10
|
+
import { removeIgnoredSearchParams } from "./removeIgnoredSearchParams.js";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Generator function that yields possible variations on the original URL to
|
|
14
|
+
* check, one at a time.
|
|
15
|
+
*
|
|
16
|
+
* @param url
|
|
17
|
+
* @param options
|
|
18
|
+
*
|
|
19
|
+
* @private
|
|
20
|
+
*/
|
|
21
|
+
export function* generateURLVariations(
|
|
22
|
+
url: string,
|
|
23
|
+
{
|
|
24
|
+
ignoreURLParametersMatching = [/^utm_/, /^fbclid$/],
|
|
25
|
+
directoryIndex = "index.html",
|
|
26
|
+
cleanURLs = true,
|
|
27
|
+
urlManipulation,
|
|
28
|
+
}: PrecacheRouteOptions = {},
|
|
29
|
+
): Generator<string, void, unknown> {
|
|
30
|
+
const urlObject = new URL(url, location.href);
|
|
31
|
+
urlObject.hash = "";
|
|
32
|
+
yield urlObject.href;
|
|
33
|
+
|
|
34
|
+
const urlWithoutIgnoredParams = removeIgnoredSearchParams(urlObject, ignoreURLParametersMatching);
|
|
35
|
+
yield urlWithoutIgnoredParams.href;
|
|
36
|
+
|
|
37
|
+
if (directoryIndex && urlWithoutIgnoredParams.pathname.endsWith("/")) {
|
|
38
|
+
const directoryURL = new URL(urlWithoutIgnoredParams.href);
|
|
39
|
+
directoryURL.pathname += directoryIndex;
|
|
40
|
+
yield directoryURL.href;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (cleanURLs) {
|
|
44
|
+
const cleanURL = new URL(urlWithoutIgnoredParams.href);
|
|
45
|
+
cleanURL.pathname += ".html";
|
|
46
|
+
yield cleanURL.href;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (urlManipulation) {
|
|
50
|
+
const additionalURLs = urlManipulation({ url: urlObject });
|
|
51
|
+
for (const urlToAttempt of additionalURLs) {
|
|
52
|
+
yield urlToAttempt.href;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2019 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { PrecacheRouteOptions } from "../_types.js";
|
|
10
|
+
import { generateURLVariations } from "./generateURLVariations.js";
|
|
11
|
+
import { getOrCreatePrecacheController } from "./getOrCreatePrecacheController.js";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* This function will take the request URL and manipulate it based on the
|
|
15
|
+
* configuration options.
|
|
16
|
+
*
|
|
17
|
+
* @param url
|
|
18
|
+
* @param options
|
|
19
|
+
* @returns Returns the URL in the cache that matches the request,
|
|
20
|
+
* if possible.
|
|
21
|
+
*
|
|
22
|
+
* @private
|
|
23
|
+
*/
|
|
24
|
+
export const getCacheKeyForURL = (url: string, options: PrecacheRouteOptions): string | undefined => {
|
|
25
|
+
const precacheController = getOrCreatePrecacheController();
|
|
26
|
+
|
|
27
|
+
const urlsToCacheKeys = precacheController.getURLsToCacheKeys();
|
|
28
|
+
for (const possibleURL of generateURLVariations(url, options)) {
|
|
29
|
+
const possibleCacheKey = urlsToCacheKeys.get(possibleURL);
|
|
30
|
+
if (possibleCacheKey) {
|
|
31
|
+
return possibleCacheKey;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return undefined;
|
|
36
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2019 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { PrecacheController } from "../PrecacheController.js";
|
|
10
|
+
|
|
11
|
+
let precacheController: PrecacheController | undefined;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @returns
|
|
15
|
+
* @private
|
|
16
|
+
*/
|
|
17
|
+
export const getOrCreatePrecacheController = (): PrecacheController => {
|
|
18
|
+
if (!precacheController) {
|
|
19
|
+
precacheController = new PrecacheController();
|
|
20
|
+
}
|
|
21
|
+
return precacheController;
|
|
22
|
+
};
|