@serwist/precaching 9.0.0-preview.17 → 9.0.0-preview.18
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 +3 -17
- 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
|
@@ -1,68 +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
|
-
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
|
-
}
|
|
@@ -1,40 +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
|
-
// Give TypeScript the correct global.
|
|
10
|
-
declare let self: ServiceWorkerGlobalScope;
|
|
11
|
-
|
|
12
|
-
const SUBSTRING_TO_FIND = "-precache-";
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Cleans up incompatible precaches that were created by older versions of
|
|
16
|
-
* Serwist, by a service worker registered under the current scope.
|
|
17
|
-
*
|
|
18
|
-
* This is meant to be called as part of the `activate` event.
|
|
19
|
-
*
|
|
20
|
-
* This should be safe to use as long as you don't include `substringToFind`
|
|
21
|
-
* (defaulting to `-precache-`) in your non-precache cache names.
|
|
22
|
-
*
|
|
23
|
-
* @param currentPrecacheName The cache name currently in use for
|
|
24
|
-
* precaching. This cache won't be deleted.
|
|
25
|
-
* @param substringToFind Cache names which include this
|
|
26
|
-
* substring will be deleted (excluding `currentPrecacheName`).
|
|
27
|
-
* @returns A list of all the cache names that were deleted.
|
|
28
|
-
* @private
|
|
29
|
-
*/
|
|
30
|
-
export const deleteOutdatedCaches = async (currentPrecacheName: string, substringToFind: string = SUBSTRING_TO_FIND): Promise<string[]> => {
|
|
31
|
-
const cacheNames = await self.caches.keys();
|
|
32
|
-
|
|
33
|
-
const cacheNamesToDelete = cacheNames.filter((cacheName) => {
|
|
34
|
-
return cacheName.includes(substringToFind) && cacheName.includes(self.registration.scope) && cacheName !== currentPrecacheName;
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
await Promise.all(cacheNamesToDelete.map((cacheName) => self.caches.delete(cacheName)));
|
|
38
|
-
|
|
39
|
-
return cacheNamesToDelete;
|
|
40
|
-
};
|
|
@@ -1,55 +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 { 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
|
-
}
|
|
@@ -1,36 +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 { 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
|
-
};
|
|
@@ -1,22 +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 { 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
|
-
};
|
|
@@ -1,38 +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
|
-
import { logger } from "@serwist/core/internal";
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* @param groupTitle
|
|
13
|
-
* @param deletedURLs
|
|
14
|
-
*
|
|
15
|
-
* @private
|
|
16
|
-
*/
|
|
17
|
-
const logGroup = (groupTitle: string, deletedURLs: string[]) => {
|
|
18
|
-
logger.groupCollapsed(groupTitle);
|
|
19
|
-
|
|
20
|
-
for (const url of deletedURLs) {
|
|
21
|
-
logger.log(url);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
logger.groupEnd();
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* @param deletedURLs
|
|
29
|
-
* @private
|
|
30
|
-
*/
|
|
31
|
-
export function printCleanupDetails(deletedURLs: string[]): void {
|
|
32
|
-
const deletionCount = deletedURLs.length;
|
|
33
|
-
if (deletionCount > 0) {
|
|
34
|
-
logger.groupCollapsed(`During precaching cleanup, ${deletionCount} cached request${deletionCount === 1 ? " was" : "s were"} deleted.`);
|
|
35
|
-
logGroup("Deleted Cache Requests", deletedURLs);
|
|
36
|
-
logger.groupEnd();
|
|
37
|
-
}
|
|
38
|
-
}
|
|
@@ -1,53 +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
|
-
import { logger } from "@serwist/core/internal";
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* @param groupTitle
|
|
13
|
-
* @param urls
|
|
14
|
-
*
|
|
15
|
-
* @private
|
|
16
|
-
*/
|
|
17
|
-
function _nestedGroup(groupTitle: string, urls: string[]): void {
|
|
18
|
-
if (urls.length === 0) {
|
|
19
|
-
return;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
logger.groupCollapsed(groupTitle);
|
|
23
|
-
|
|
24
|
-
for (const url of urls) {
|
|
25
|
-
logger.log(url);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
logger.groupEnd();
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* @param urlsToPrecache
|
|
33
|
-
* @param urlsAlreadyPrecached
|
|
34
|
-
* @private
|
|
35
|
-
*/
|
|
36
|
-
export function printInstallDetails(urlsToPrecache: string[], urlsAlreadyPrecached: string[]): void {
|
|
37
|
-
const precachedCount = urlsToPrecache.length;
|
|
38
|
-
const alreadyPrecachedCount = urlsAlreadyPrecached.length;
|
|
39
|
-
|
|
40
|
-
if (precachedCount || alreadyPrecachedCount) {
|
|
41
|
-
let message = `Precaching ${precachedCount} file${precachedCount === 1 ? "" : "s"}.`;
|
|
42
|
-
|
|
43
|
-
if (alreadyPrecachedCount > 0) {
|
|
44
|
-
message += ` ${alreadyPrecachedCount} ` + `file${alreadyPrecachedCount === 1 ? " is" : "s are"} already cached.`;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
logger.groupCollapsed(message);
|
|
48
|
-
|
|
49
|
-
_nestedGroup("View newly precached URLs.", urlsToPrecache);
|
|
50
|
-
_nestedGroup("View previously precached URLs.", urlsAlreadyPrecached);
|
|
51
|
-
logger.groupEnd();
|
|
52
|
-
}
|
|
53
|
-
}
|
|
@@ -1,29 +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
|
-
/**
|
|
10
|
-
* Removes any URL search parameters that should be ignored.
|
|
11
|
-
*
|
|
12
|
-
* @param urlObject The original URL.
|
|
13
|
-
* @param ignoreURLParametersMatching RegExps to test against
|
|
14
|
-
* each search parameter name. Matches mean that the search parameter should be
|
|
15
|
-
* ignored.
|
|
16
|
-
* @returns The URL with any ignored search parameters removed.
|
|
17
|
-
* @private
|
|
18
|
-
*/
|
|
19
|
-
export function removeIgnoredSearchParams(urlObject: URL, ignoreURLParametersMatching: RegExp[] = []): URL {
|
|
20
|
-
// Convert the iterable into an array at the start of the loop to make sure
|
|
21
|
-
// deletion doesn't mess up iteration.
|
|
22
|
-
for (const paramName of [...urlObject.searchParams.keys()]) {
|
|
23
|
-
if (ignoreURLParametersMatching.some((regExp) => regExp.test(paramName))) {
|
|
24
|
-
urlObject.searchParams.delete(paramName);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return urlObject;
|
|
29
|
-
}
|