0.2.0 - Mid migration

This commit is contained in:
Daniel Mason 2022-04-25 14:47:15 +12:00
parent 139e6a915e
commit 7e38fdbd7d
42393 changed files with 5358157 additions and 62 deletions

View file

@ -0,0 +1,36 @@
import { urlManipulation } from '../_types.js';
import '../_version.js';
export interface FetchListenerOptions {
directoryIndex?: string;
ignoreURLParametersMatching?: RegExp[];
cleanURLs?: boolean;
urlManipulation?: urlManipulation;
}
/**
* Adds a `fetch` listener to the service worker that will
* respond to
* [network requests]{@link https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers#Custom_responses_to_requests}
* with precached assets.
*
* Requests for assets that aren't precached, the `FetchEvent` will not be
* responded to, allowing the event to fall through to other `fetch` event
* listeners.
*
* NOTE: when called more than once this method will replace the previously set
* configuration options. Calling it more than once is not recommended outside
* of tests.
*
* @private
* @param {Object} [options]
* @param {string} [options.directoryIndex=index.html] The `directoryIndex` will
* check cache entries for a URLs ending with '/' to see if there is a hit when
* appending the `directoryIndex` value.
* @param {Array<RegExp>} [options.ignoreURLParametersMatching=[/^utm_/]] An
* array of regex's to remove search params when looking for a cache match.
* @param {boolean} [options.cleanURLs=true] The `cleanURLs` option will
* check the cache for the URL with a `.html` added to the end of the end.
* @param {workbox.precaching~urlManipulation} [options.urlManipulation]
* This is a function that should take a URL and return an array of
* alternative URLs that should be checked for precache matches.
*/
export declare const addFetchListener: ({ ignoreURLParametersMatching, directoryIndex, cleanURLs, urlManipulation, }?: FetchListenerOptions) => void;

View file

@ -0,0 +1,91 @@
/*
Copyright 2019 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import { cacheNames } from 'workbox-core/_private/cacheNames.js';
import { getFriendlyURL } from 'workbox-core/_private/getFriendlyURL.js';
import { logger } from 'workbox-core/_private/logger.js';
import { getCacheKeyForURL } from './getCacheKeyForURL.js';
import '../_version.js';
/**
* Adds a `fetch` listener to the service worker that will
* respond to
* [network requests]{@link https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers#Custom_responses_to_requests}
* with precached assets.
*
* Requests for assets that aren't precached, the `FetchEvent` will not be
* responded to, allowing the event to fall through to other `fetch` event
* listeners.
*
* NOTE: when called more than once this method will replace the previously set
* configuration options. Calling it more than once is not recommended outside
* of tests.
*
* @private
* @param {Object} [options]
* @param {string} [options.directoryIndex=index.html] The `directoryIndex` will
* check cache entries for a URLs ending with '/' to see if there is a hit when
* appending the `directoryIndex` value.
* @param {Array<RegExp>} [options.ignoreURLParametersMatching=[/^utm_/]] An
* array of regex's to remove search params when looking for a cache match.
* @param {boolean} [options.cleanURLs=true] The `cleanURLs` option will
* check the cache for the URL with a `.html` added to the end of the end.
* @param {workbox.precaching~urlManipulation} [options.urlManipulation]
* This is a function that should take a URL and return an array of
* alternative URLs that should be checked for precache matches.
*/
export const addFetchListener = ({ ignoreURLParametersMatching = [/^utm_/], directoryIndex = 'index.html', cleanURLs = true, urlManipulation, } = {}) => {
const cacheName = cacheNames.getPrecacheName();
// See https://github.com/Microsoft/TypeScript/issues/28357#issuecomment-436484705
self.addEventListener('fetch', ((event) => {
const precachedURL = getCacheKeyForURL(event.request.url, {
cleanURLs,
directoryIndex,
ignoreURLParametersMatching,
urlManipulation,
});
if (!precachedURL) {
if (process.env.NODE_ENV !== 'production') {
logger.debug(`Precaching did not find a match for ` +
getFriendlyURL(event.request.url));
}
return;
}
let responsePromise = self.caches.open(cacheName).then((cache) => {
return cache.match(precachedURL);
}).then((cachedResponse) => {
if (cachedResponse) {
return cachedResponse;
}
// Fall back to the network if we don't have a cached response
// (perhaps due to manual cache cleanup).
if (process.env.NODE_ENV !== 'production') {
logger.warn(`The precached response for ` +
`${getFriendlyURL(precachedURL)} in ${cacheName} was not found. ` +
`Falling back to the network instead.`);
}
return fetch(precachedURL);
});
if (process.env.NODE_ENV !== 'production') {
responsePromise = responsePromise.then((response) => {
// Workbox is going to handle the route.
// print the routing details to the console.
logger.groupCollapsed(`Precaching is responding to: ` +
getFriendlyURL(event.request.url));
logger.log(`Serving the precached url: ${precachedURL}`);
logger.groupCollapsed(`View request details here.`);
logger.log(event.request);
logger.groupEnd();
logger.groupCollapsed(`View response details here.`);
logger.log(response);
logger.groupEnd();
logger.groupEnd();
return response;
});
}
event.respondWith(responsePromise);
}));
};

View file

@ -0,0 +1 @@
export * from './addFetchListener.js';

View file

@ -0,0 +1,17 @@
import { PrecacheEntry } from '../_types.js';
import '../_version.js';
interface CacheKey {
cacheKey: string;
url: string;
}
/**
* Converts a manifest entry into a versioned URL suitable for precaching.
*
* @param {Object|string} entry
* @return {string} A URL with versioning info.
*
* @private
* @memberof module:workbox-precaching
*/
export declare function createCacheKey(entry: PrecacheEntry | string): CacheKey;
export {};

View file

@ -0,0 +1,56 @@
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import { WorkboxError } from 'workbox-core/_private/WorkboxError.js';
import '../_version.js';
// Name of the search parameter used to store revision info.
const REVISION_SEARCH_PARAM = '__WB_REVISION__';
/**
* Converts a manifest entry into a versioned URL suitable for precaching.
*
* @param {Object|string} entry
* @return {string} A URL with versioning info.
*
* @private
* @memberof module:workbox-precaching
*/
export function createCacheKey(entry) {
if (!entry) {
throw new WorkboxError('add-to-cache-list-unexpected-type', { entry });
}
// If a precache manifest entry is a string, it's assumed to be a versioned
// URL, like '/app.abcd1234.js'. Return as-is.
if (typeof entry === 'string') {
const urlObject = new URL(entry, location.href);
return {
cacheKey: urlObject.href,
url: urlObject.href,
};
}
const { revision, url } = entry;
if (!url) {
throw new WorkboxError('add-to-cache-list-unexpected-type', { entry });
}
// If there's just a URL and no revision, then it's also assumed to be a
// versioned URL.
if (!revision) {
const urlObject = new URL(url, location.href);
return {
cacheKey: urlObject.href,
url: urlObject.href,
};
}
// Otherwise, construct a properly versioned URL using the custom Workbox
// search parameter along with the revision info.
const cacheKeyURL = new URL(url, location.href);
const originalURL = new URL(url, location.href);
cacheKeyURL.searchParams.set(REVISION_SEARCH_PARAM, revision);
return {
cacheKey: cacheKeyURL.href,
url: originalURL.href,
};
}

View file

@ -0,0 +1 @@
export * from './createCacheKey.js';

View file

@ -0,0 +1,21 @@
import '../_version.js';
/**
* Cleans up incompatible precaches that were created by older versions of
* Workbox, by a service worker registered under the current scope.
*
* This is meant to be called as part of the `activate` event.
*
* This should be safe to use as long as you don't include `substringToFind`
* (defaulting to `-precache-`) in your non-precache cache names.
*
* @param {string} currentPrecacheName The cache name currently in use for
* precaching. This cache won't be deleted.
* @param {string} [substringToFind='-precache-'] Cache names which include this
* substring will be deleted (excluding `currentPrecacheName`).
* @return {Array<string>} A list of all the cache names that were deleted.
*
* @private
* @memberof module:workbox-precaching
*/
declare const deleteOutdatedCaches: (currentPrecacheName: string, substringToFind?: string) => Promise<string[]>;
export { deleteOutdatedCaches };

View file

@ -0,0 +1,38 @@
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import '../_version.js';
const SUBSTRING_TO_FIND = '-precache-';
/**
* Cleans up incompatible precaches that were created by older versions of
* Workbox, by a service worker registered under the current scope.
*
* This is meant to be called as part of the `activate` event.
*
* This should be safe to use as long as you don't include `substringToFind`
* (defaulting to `-precache-`) in your non-precache cache names.
*
* @param {string} currentPrecacheName The cache name currently in use for
* precaching. This cache won't be deleted.
* @param {string} [substringToFind='-precache-'] Cache names which include this
* substring will be deleted (excluding `currentPrecacheName`).
* @return {Array<string>} A list of all the cache names that were deleted.
*
* @private
* @memberof module:workbox-precaching
*/
const deleteOutdatedCaches = async (currentPrecacheName, substringToFind = SUBSTRING_TO_FIND) => {
const cacheNames = await self.caches.keys();
const cacheNamesToDelete = cacheNames.filter((cacheName) => {
return cacheName.includes(substringToFind) &&
cacheName.includes(self.registration.scope) &&
cacheName !== currentPrecacheName;
});
await Promise.all(cacheNamesToDelete.map((cacheName) => self.caches.delete(cacheName)));
return cacheNamesToDelete;
};
export { deleteOutdatedCaches };

View file

@ -0,0 +1 @@
export * from './deleteOutdatedCaches.js';

View file

@ -0,0 +1,13 @@
import { FetchListenerOptions } from './addFetchListener.js';
import '../_version.js';
/**
* Generator function that yields possible variations on the original URL to
* check, one at a time.
*
* @param {string} url
* @param {Object} options
*
* @private
* @memberof module:workbox-precaching
*/
export declare function generateURLVariations(url: string, { ignoreURLParametersMatching, directoryIndex, cleanURLs, urlManipulation, }?: FetchListenerOptions): Generator<string, void, unknown>;

View file

@ -0,0 +1,42 @@
/*
Copyright 2019 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import { removeIgnoredSearchParams } from './removeIgnoredSearchParams.js';
import '../_version.js';
/**
* Generator function that yields possible variations on the original URL to
* check, one at a time.
*
* @param {string} url
* @param {Object} options
*
* @private
* @memberof module:workbox-precaching
*/
export function* generateURLVariations(url, { ignoreURLParametersMatching, directoryIndex, cleanURLs, urlManipulation, } = {}) {
const urlObject = new URL(url, location.href);
urlObject.hash = '';
yield urlObject.href;
const urlWithoutIgnoredParams = removeIgnoredSearchParams(urlObject, ignoreURLParametersMatching);
yield urlWithoutIgnoredParams.href;
if (directoryIndex && urlWithoutIgnoredParams.pathname.endsWith('/')) {
const directoryURL = new URL(urlWithoutIgnoredParams.href);
directoryURL.pathname += directoryIndex;
yield directoryURL.href;
}
if (cleanURLs) {
const cleanURL = new URL(urlWithoutIgnoredParams.href);
cleanURL.pathname += '.html';
yield cleanURL.href;
}
if (urlManipulation) {
const additionalURLs = urlManipulation({ url: urlObject });
for (const urlToAttempt of additionalURLs) {
yield urlToAttempt.href;
}
}
}

View file

@ -0,0 +1 @@
export * from './generateURLVariations.js';

View file

@ -0,0 +1,14 @@
import { FetchListenerOptions } from './addFetchListener.js';
import '../_version.js';
/**
* This function will take the request URL and manipulate it based on the
* configuration options.
*
* @param {string} url
* @param {Object} options
* @return {string} Returns the URL in the cache that matches the request,
* if possible.
*
* @private
*/
export declare const getCacheKeyForURL: (url: string, options: FetchListenerOptions) => string | void;

View file

@ -0,0 +1,31 @@
/*
Copyright 2019 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import { getOrCreatePrecacheController } from './getOrCreatePrecacheController.js';
import { generateURLVariations } from './generateURLVariations.js';
import '../_version.js';
/**
* This function will take the request URL and manipulate it based on the
* configuration options.
*
* @param {string} url
* @param {Object} options
* @return {string} Returns the URL in the cache that matches the request,
* if possible.
*
* @private
*/
export const getCacheKeyForURL = (url, options) => {
const precacheController = getOrCreatePrecacheController();
const urlsToCacheKeys = precacheController.getURLsToCacheKeys();
for (const possibleURL of generateURLVariations(url, options)) {
const possibleCacheKey = urlsToCacheKeys.get(possibleURL);
if (possibleCacheKey) {
return possibleCacheKey;
}
}
};

View file

@ -0,0 +1 @@
export * from './getCacheKeyForURL.js';

View file

@ -0,0 +1,7 @@
import { PrecacheController } from '../PrecacheController.js';
import '../_version.js';
/**
* @return {PrecacheController}
* @private
*/
export declare const getOrCreatePrecacheController: () => PrecacheController;

View file

@ -0,0 +1,20 @@
/*
Copyright 2019 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import { PrecacheController } from '../PrecacheController.js';
import '../_version.js';
let precacheController;
/**
* @return {PrecacheController}
* @private
*/
export const getOrCreatePrecacheController = () => {
if (!precacheController) {
precacheController = new PrecacheController();
}
return precacheController;
};

View file

@ -0,0 +1 @@
export * from './getOrCreatePrecacheController.js';

View file

@ -0,0 +1,6 @@
import { WorkboxPlugin } from 'workbox-core/types.js';
import '../_version.js';
export declare const precachePlugins: {
get(): WorkboxPlugin[];
add(newPlugins: WorkboxPlugin[]): void;
};

View file

@ -0,0 +1,25 @@
/*
Copyright 2019 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import '../_version.js';
const plugins = [];
export const precachePlugins = {
/*
* @return {Array}
* @private
*/
get() {
return plugins;
},
/*
* @param {Array} newPlugins
* @private
*/
add(newPlugins) {
plugins.push(...newPlugins);
},
};

View file

@ -0,0 +1 @@
export * from './precachePlugins.js';

View file

@ -0,0 +1,8 @@
import '../_version.js';
/**
* @param {Array<string>} deletedURLs
*
* @private
* @memberof module:workbox-precaching
*/
export declare function printCleanupDetails(deletedURLs: string[]): void;

View file

@ -0,0 +1,38 @@
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import { logger } from 'workbox-core/_private/logger.js';
import '../_version.js';
/**
* @param {string} groupTitle
* @param {Array<string>} deletedURLs
*
* @private
*/
const logGroup = (groupTitle, deletedURLs) => {
logger.groupCollapsed(groupTitle);
for (const url of deletedURLs) {
logger.log(url);
}
logger.groupEnd();
};
/**
* @param {Array<string>} deletedURLs
*
* @private
* @memberof module:workbox-precaching
*/
export function printCleanupDetails(deletedURLs) {
const deletionCount = deletedURLs.length;
if (deletionCount > 0) {
logger.groupCollapsed(`During precaching cleanup, ` +
`${deletionCount} cached ` +
`request${deletionCount === 1 ? ' was' : 's were'} deleted.`);
logGroup('Deleted Cache Requests', deletedURLs);
logger.groupEnd();
}
}

View file

@ -0,0 +1 @@
export * from './printCleanupDetails.js';

View file

@ -0,0 +1,9 @@
import '../_version.js';
/**
* @param {Array<string>} urlsToPrecache
* @param {Array<string>} urlsAlreadyPrecached
*
* @private
* @memberof module:workbox-precaching
*/
export declare function printInstallDetails(urlsToPrecache: string[], urlsAlreadyPrecached: string[]): void;

View file

@ -0,0 +1,47 @@
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import { logger } from 'workbox-core/_private/logger.js';
import '../_version.js';
/**
* @param {string} groupTitle
* @param {Array<string>} urls
*
* @private
*/
function _nestedGroup(groupTitle, urls) {
if (urls.length === 0) {
return;
}
logger.groupCollapsed(groupTitle);
for (const url of urls) {
logger.log(url);
}
logger.groupEnd();
}
/**
* @param {Array<string>} urlsToPrecache
* @param {Array<string>} urlsAlreadyPrecached
*
* @private
* @memberof module:workbox-precaching
*/
export function printInstallDetails(urlsToPrecache, urlsAlreadyPrecached) {
const precachedCount = urlsToPrecache.length;
const alreadyPrecachedCount = urlsAlreadyPrecached.length;
if (precachedCount || alreadyPrecachedCount) {
let message = `Precaching ${precachedCount} file${precachedCount === 1 ? '' : 's'}.`;
if (alreadyPrecachedCount > 0) {
message += ` ${alreadyPrecachedCount} ` +
`file${alreadyPrecachedCount === 1 ? ' is' : 's are'} already cached.`;
}
logger.groupCollapsed(message);
_nestedGroup(`View newly precached URLs.`, urlsToPrecache);
_nestedGroup(`View previously precached URLs.`, urlsAlreadyPrecached);
logger.groupEnd();
}
}

View file

@ -0,0 +1 @@
export * from './printInstallDetails.js';

View file

@ -0,0 +1,14 @@
import '../_version.js';
/**
* Removes any URL search parameters that should be ignored.
*
* @param {URL} urlObject The original URL.
* @param {Array<RegExp>} ignoreURLParametersMatching RegExps to test against
* each search parameter name. Matches mean that the search parameter should be
* ignored.
* @return {URL} The URL with any ignored search parameters removed.
*
* @private
* @memberof module:workbox-precaching
*/
export declare function removeIgnoredSearchParams(urlObject: URL, ignoreURLParametersMatching?: RegExp[]): URL;

View file

@ -0,0 +1,30 @@
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import '../_version.js';
/**
* Removes any URL search parameters that should be ignored.
*
* @param {URL} urlObject The original URL.
* @param {Array<RegExp>} ignoreURLParametersMatching RegExps to test against
* each search parameter name. Matches mean that the search parameter should be
* ignored.
* @return {URL} The URL with any ignored search parameters removed.
*
* @private
* @memberof module:workbox-precaching
*/
export function removeIgnoredSearchParams(urlObject, ignoreURLParametersMatching = []) {
// Convert the iterable into an array at the start of the loop to make sure
// deletion doesn't mess up iteration.
for (const paramName of [...urlObject.searchParams.keys()]) {
if (ignoreURLParametersMatching.some((regExp) => regExp.test(paramName))) {
urlObject.searchParams.delete(paramName);
}
}
return urlObject;
}

View file

@ -0,0 +1 @@
export * from './removeIgnoredSearchParams.js';