mirror of
https://github.com/hanzoai/workbox.git
synced 2026-09-24 17:04:35 +00:00
Ts workbox precaching (#2098)
* Move and rename files to .ts and update imports * Add type definitions
This commit is contained in:
committed by
Jeffrey Posnick
parent
b4246132aa
commit
2f4e628345
@@ -31,4 +31,5 @@ packages/workbox-cacheable-response/**/*.mjs
|
||||
packages/workbox-core/**/*.mjs
|
||||
packages/workbox-expiration/**/*.mjs
|
||||
packages/workbox-navigation-preload/**/*.mjs
|
||||
packages/workbox-precaching/**/*.mjs
|
||||
packages/workbox-window/**/*.mjs
|
||||
|
||||
Vendored
+19
-2
@@ -14,8 +14,9 @@ interface IDBObjectStore {
|
||||
// DOM
|
||||
// ------------------------------------------------------------------------- //
|
||||
|
||||
// Copied from lib.dom.iterable.d.ts for codes that don't include DOM typings:
|
||||
// https://github.com/microsoft/TypeScript/blob/00bf32ca3967b07e8663d0cd2b3e2bbf572da88b/lib/lib.dom.iterable.d.ts#L103-L117
|
||||
// Copied from lib.dom.iterable.d.ts for code that doesn't include DOM typings:
|
||||
// https://github.com/microsoft/TypeScript/blob/00bf32ca3967b07e8663d0cd2b3e2bbf572da88b/lib/lib.dom.iterable.d.ts
|
||||
|
||||
interface Headers {
|
||||
[Symbol.iterator](): IterableIterator<[string, string]>;
|
||||
/**
|
||||
@@ -31,3 +32,19 @@ interface Headers {
|
||||
*/
|
||||
values(): IterableIterator<string>;
|
||||
}
|
||||
|
||||
interface URLSearchParams {
|
||||
[Symbol.iterator](): IterableIterator<[string, string]>;
|
||||
/**
|
||||
* Returns an array of key, value pairs for every entry in the search params.
|
||||
*/
|
||||
entries(): IterableIterator<[string, string]>;
|
||||
/**
|
||||
* Returns a list of keys in the search params.
|
||||
*/
|
||||
keys(): IterableIterator<string>;
|
||||
/**
|
||||
* Returns a list of values in the search params.
|
||||
*/
|
||||
values(): IterableIterator<string>;
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ interface MatchWrapperOptions {
|
||||
cacheName: string,
|
||||
request: Request,
|
||||
event?: Event,
|
||||
plugins?: [],
|
||||
plugins?: WorkboxPlugin[],
|
||||
matchOptions?: {},
|
||||
}
|
||||
|
||||
@@ -219,7 +219,7 @@ const _isResponseSafeToCache = async ({
|
||||
request: Request,
|
||||
response: Response,
|
||||
event?: Event,
|
||||
plugins?: [],
|
||||
plugins?: WorkboxPlugin[],
|
||||
}) => {
|
||||
let responseToCache: Response|undefined = response;
|
||||
let pluginsUsed = false;
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
import '../_version.js';
|
||||
|
||||
|
||||
export interface WorkboxPlugin {
|
||||
cacheDidUpdate?: ({
|
||||
cacheName,
|
||||
@@ -24,11 +25,14 @@ export interface WorkboxPlugin {
|
||||
}) => Promise<void>;
|
||||
cacheKeyWillBeUsed?: Function;
|
||||
cacheWillUpdate?: ({
|
||||
response
|
||||
response,
|
||||
request,
|
||||
event,
|
||||
}: {
|
||||
response: Response
|
||||
response: Response,
|
||||
request?: Request,
|
||||
event?: ExtendableEvent,
|
||||
}) => Promise<Response | null>;
|
||||
|
||||
cachedResponseWillBeUsed?: ({
|
||||
cacheName,
|
||||
request,
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
try{self['workbox:precaching:4.3.1']&&_()}catch(e){}// eslint-disable-line
|
||||
@@ -22,8 +22,9 @@
|
||||
"browserNamespace": "workbox.precaching",
|
||||
"packageType": "browser"
|
||||
},
|
||||
"main": "build/workbox-precaching.prod.js",
|
||||
"main": "index.js",
|
||||
"module": "index.mjs",
|
||||
"types": "index.d.ts",
|
||||
"dependencies": {
|
||||
"workbox-core": "^4.3.1"
|
||||
}
|
||||
|
||||
+36
-22
@@ -6,18 +6,20 @@
|
||||
https://opensource.org/licenses/MIT.
|
||||
*/
|
||||
|
||||
import {assert} from 'workbox-core/_private/assert.mjs';
|
||||
import {cacheNames} from 'workbox-core/_private/cacheNames.mjs';
|
||||
import {cacheWrapper} from 'workbox-core/_private/cacheWrapper.mjs';
|
||||
import {fetchWrapper} from 'workbox-core/_private/fetchWrapper.mjs';
|
||||
import {WorkboxError} from 'workbox-core/_private/WorkboxError.mjs';
|
||||
import {assert} from 'workbox-core/_private/assert.js';
|
||||
import {cacheNames} from 'workbox-core/_private/cacheNames.js';
|
||||
import {cacheWrapper} from 'workbox-core/_private/cacheWrapper.js';
|
||||
import {fetchWrapper} from 'workbox-core/_private/fetchWrapper.js';
|
||||
import {WorkboxError} from 'workbox-core/_private/WorkboxError.js';
|
||||
import {WorkboxPlugin} from 'workbox-core/utils/pluginUtils.js';
|
||||
|
||||
import {cleanRedirect} from './utils/cleanRedirect.mjs';
|
||||
import {createCacheKey} from './utils/createCacheKey.mjs';
|
||||
import {printCleanupDetails} from './utils/printCleanupDetails.mjs';
|
||||
import {printInstallDetails} from './utils/printInstallDetails.mjs';
|
||||
import {cleanRedirect} from './utils/cleanRedirect.js';
|
||||
import {createCacheKey} from './utils/createCacheKey.js';
|
||||
import {printCleanupDetails} from './utils/printCleanupDetails.js';
|
||||
import {printInstallDetails} from './utils/printInstallDetails.js';
|
||||
|
||||
import './_version.mjs';
|
||||
import './_version.js';
|
||||
import { PrecacheEntry } from './_types.js';
|
||||
|
||||
|
||||
/**
|
||||
@@ -26,13 +28,16 @@ import './_version.mjs';
|
||||
* @memberof module:workbox-precaching
|
||||
*/
|
||||
class PrecacheController {
|
||||
private _cacheName: string;
|
||||
private _urlsToCacheKeys: Map<string, string>;
|
||||
|
||||
/**
|
||||
* Create a new PrecacheController.
|
||||
*
|
||||
* @param {string} [cacheName] An optional name for the cache, to override
|
||||
* the default precache name.
|
||||
*/
|
||||
constructor(cacheName) {
|
||||
constructor(cacheName?: string) {
|
||||
this._cacheName = cacheNames.getPrecacheName(cacheName);
|
||||
this._urlsToCacheKeys = new Map();
|
||||
}
|
||||
@@ -45,9 +50,9 @@ class PrecacheController {
|
||||
* Array<module:workbox-precaching.PrecacheController.PrecacheEntry|string>
|
||||
* } entries Array of entries to precache.
|
||||
*/
|
||||
addToCacheList(entries) {
|
||||
addToCacheList(entries: Array<PrecacheEntry|string>) {
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
assert.isArray(entries, {
|
||||
assert!.isArray(entries, {
|
||||
moduleName: 'workbox-precaching',
|
||||
className: 'PrecacheController',
|
||||
funcName: 'addToCacheList',
|
||||
@@ -78,10 +83,13 @@ class PrecacheController {
|
||||
* and caching during install.
|
||||
* @return {Promise<workbox.precaching.InstallResult>}
|
||||
*/
|
||||
async install({event, plugins} = {}) {
|
||||
async install({event, plugins}: {
|
||||
event?: ExtendableEvent,
|
||||
plugins?: WorkboxPlugin[],
|
||||
} = {}) {
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
if (plugins) {
|
||||
assert.isArray(plugins, {
|
||||
assert!.isArray(plugins, {
|
||||
moduleName: 'workbox-precaching',
|
||||
className: 'PrecacheController',
|
||||
funcName: 'install',
|
||||
@@ -163,7 +171,11 @@ class PrecacheController {
|
||||
* @param {Array<Object>} [options.plugins] An array of plugins to apply to
|
||||
* fetch and caching.
|
||||
*/
|
||||
async _addURLToCache({url, event, plugins}) {
|
||||
async _addURLToCache({url, event, plugins}: {
|
||||
url: string,
|
||||
event?: ExtendableEvent,
|
||||
plugins?: WorkboxPlugin[],
|
||||
}) {
|
||||
const request = new Request(url, {credentials: 'same-origin'});
|
||||
let response = await fetchWrapper.fetch({
|
||||
event,
|
||||
@@ -174,16 +186,18 @@ class PrecacheController {
|
||||
// Allow developers to override the default logic about what is and isn't
|
||||
// valid by passing in a plugin implementing cacheWillUpdate(), e.g.
|
||||
// a workbox.cacheableResponse.Plugin instance.
|
||||
let cacheWillUpdateCallback;
|
||||
let cacheWillUpdatePlugin;
|
||||
for (const plugin of (plugins || [])) {
|
||||
if ('cacheWillUpdate' in plugin) {
|
||||
cacheWillUpdateCallback = plugin.cacheWillUpdate.bind(plugin);
|
||||
cacheWillUpdatePlugin = plugin;
|
||||
}
|
||||
}
|
||||
|
||||
const isValidResponse = cacheWillUpdateCallback ?
|
||||
const isValidResponse = cacheWillUpdatePlugin ?
|
||||
// Use a callback if provided. It returns a truthy value if valid.
|
||||
cacheWillUpdateCallback({event, request, response}) :
|
||||
// NOTE: invoke the method on the plugin instance so the `this` context
|
||||
// is correct.
|
||||
cacheWillUpdatePlugin.cacheWillUpdate!({event, request, response}) :
|
||||
// Otherwise, default to considering any response status under 400 valid.
|
||||
// This includes, by default, considering opaque responses valid.
|
||||
response.status < 400;
|
||||
@@ -242,8 +256,8 @@ class PrecacheController {
|
||||
* @return {string} The versioned URL that corresponds to a cache key
|
||||
* for the original URL, or undefined if that URL isn't precached.
|
||||
*/
|
||||
getCacheKeyForURL(url) {
|
||||
const urlObject = new URL(url, location);
|
||||
getCacheKeyForURL(url: string) {
|
||||
const urlObject = new URL(url, location.href);
|
||||
return this._urlsToCacheKeys.get(urlObject.href);
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
https://opensource.org/licenses/MIT.
|
||||
*/
|
||||
|
||||
import './_version.mjs';
|
||||
import './_version.js';
|
||||
|
||||
/**
|
||||
* @typedef {Object} InstallResult
|
||||
@@ -17,6 +17,10 @@ import './_version.mjs';
|
||||
*
|
||||
* @memberof workbox.precaching
|
||||
*/
|
||||
export interface InstallResult {
|
||||
updatedURLs: string[];
|
||||
notUpdatedURLs: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* @typedef {Object} CleanupResult
|
||||
@@ -25,6 +29,9 @@ import './_version.mjs';
|
||||
*
|
||||
* @memberof workbox.precaching
|
||||
*/
|
||||
export interface CleanupResult {
|
||||
deletedCacheRequests: string[],
|
||||
}
|
||||
|
||||
/**
|
||||
* @typedef {Object} PrecacheEntry
|
||||
@@ -33,6 +40,10 @@ import './_version.mjs';
|
||||
*
|
||||
* @memberof workbox.precaching
|
||||
*/
|
||||
export interface PrecacheEntry {
|
||||
url: string;
|
||||
revision?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* The "urlManipulation" callback can be used to determine if there are any
|
||||
@@ -50,3 +61,4 @@ import './_version.mjs';
|
||||
*
|
||||
* @memberof workbox.precaching
|
||||
*/
|
||||
export type urlManipulation = ({url}: {url: URL}) => URL[];
|
||||
@@ -0,0 +1,2 @@
|
||||
// @ts-ignore
|
||||
try{self['workbox:precaching:4.3.1']&&_()}catch(e){}
|
||||
+4
-3
@@ -6,8 +6,9 @@
|
||||
https://opensource.org/licenses/MIT.
|
||||
*/
|
||||
|
||||
import {precachePlugins} from './utils/precachePlugins.mjs';
|
||||
import './_version.mjs';
|
||||
import {WorkboxPlugin} from 'workbox-core/utils/pluginUtils.js';
|
||||
import {precachePlugins} from './utils/precachePlugins.js';
|
||||
import './_version.js';
|
||||
|
||||
|
||||
/**
|
||||
@@ -17,7 +18,7 @@ import './_version.mjs';
|
||||
*
|
||||
* @alias workbox.precaching.addPlugins
|
||||
*/
|
||||
const addPlugins = (newPlugins) => {
|
||||
const addPlugins = (newPlugins: WorkboxPlugin[]) => {
|
||||
precachePlugins.add(newPlugins);
|
||||
};
|
||||
|
||||
+3
-3
@@ -6,8 +6,8 @@
|
||||
https://opensource.org/licenses/MIT.
|
||||
*/
|
||||
|
||||
import {addFetchListener} from './utils/addFetchListener.mjs';
|
||||
import './_version.mjs';
|
||||
import {addFetchListener, FetchListenerOptions} from './utils/addFetchListener.js';
|
||||
import './_version.js';
|
||||
|
||||
|
||||
let listenerAdded = false;
|
||||
@@ -36,7 +36,7 @@ let listenerAdded = false;
|
||||
*
|
||||
* @alias workbox.precaching.addRoute
|
||||
*/
|
||||
export const addRoute = (options) => {
|
||||
export const addRoute = (options: FetchListenerOptions) => {
|
||||
if (!listenerAdded) {
|
||||
addFetchListener(options);
|
||||
listenerAdded = true;
|
||||
+5
-5
@@ -6,10 +6,10 @@
|
||||
https://opensource.org/licenses/MIT.
|
||||
*/
|
||||
|
||||
import {cacheNames} from 'workbox-core/_private/cacheNames.mjs';
|
||||
import {logger} from 'workbox-core/_private/logger.mjs';
|
||||
import {deleteOutdatedCaches} from './utils/deleteOutdatedCaches.mjs';
|
||||
import './_version.mjs';
|
||||
import {cacheNames} from 'workbox-core/_private/cacheNames.js';
|
||||
import {logger} from 'workbox-core/_private/logger.js';
|
||||
import {deleteOutdatedCaches} from './utils/deleteOutdatedCaches.js';
|
||||
import './_version.js';
|
||||
|
||||
|
||||
/**
|
||||
@@ -19,7 +19,7 @@ import './_version.mjs';
|
||||
* @alias workbox.precaching.cleanupOutdatedCaches
|
||||
*/
|
||||
export const cleanupOutdatedCaches = () => {
|
||||
addEventListener('activate', (event) => {
|
||||
addEventListener('activate', (event: ExtendableEvent) => {
|
||||
const cacheName = cacheNames.getPrecacheName();
|
||||
|
||||
event.waitUntil(deleteOutdatedCaches(cacheName).then((cachesDeleted) => {
|
||||
+3
-4
@@ -6,9 +6,8 @@
|
||||
https://opensource.org/licenses/MIT.
|
||||
*/
|
||||
|
||||
import {getOrCreatePrecacheController}
|
||||
from './utils/getOrCreatePrecacheController.mjs';
|
||||
import './_version.mjs';
|
||||
import {getOrCreatePrecacheController} from './utils/getOrCreatePrecacheController.js';
|
||||
import './_version.js';
|
||||
|
||||
|
||||
/**
|
||||
@@ -30,7 +29,7 @@ import './_version.mjs';
|
||||
*
|
||||
* @alias workbox.precaching.getCacheKeyForURL
|
||||
*/
|
||||
export const getCacheKeyForURL = (url) => {
|
||||
export const getCacheKeyForURL = (url: string) => {
|
||||
const precacheController = getOrCreatePrecacheController();
|
||||
return precacheController.getCacheKeyForURL(url);
|
||||
};
|
||||
@@ -6,19 +6,19 @@
|
||||
https://opensource.org/licenses/MIT.
|
||||
*/
|
||||
|
||||
import {assert} from 'workbox-core/_private/assert.mjs';
|
||||
import {addPlugins} from './addPlugins.mjs';
|
||||
import {addRoute} from './addRoute.mjs';
|
||||
import {cleanupOutdatedCaches} from './cleanupOutdatedCaches.mjs';
|
||||
import {getCacheKeyForURL} from './getCacheKeyForURL.mjs';
|
||||
import {precache} from './precache.mjs';
|
||||
import {precacheAndRoute} from './precacheAndRoute.mjs';
|
||||
import {PrecacheController} from './PrecacheController.mjs';
|
||||
import './_version.mjs';
|
||||
import {assert} from 'workbox-core/_private/assert.js';
|
||||
import {addPlugins} from './addPlugins.js';
|
||||
import {addRoute} from './addRoute.js';
|
||||
import {cleanupOutdatedCaches} from './cleanupOutdatedCaches.js';
|
||||
import {getCacheKeyForURL} from './getCacheKeyForURL.js';
|
||||
import {precache} from './precache.js';
|
||||
import {precacheAndRoute} from './precacheAndRoute.js';
|
||||
import {PrecacheController} from './PrecacheController.js';
|
||||
import './_version.js';
|
||||
|
||||
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
assert.isSWEnv('workbox-precaching');
|
||||
assert!.isSWEnv('workbox-precaching');
|
||||
}
|
||||
|
||||
/**
|
||||
+10
-10
@@ -6,19 +6,20 @@
|
||||
https://opensource.org/licenses/MIT.
|
||||
*/
|
||||
|
||||
import {logger} from 'workbox-core/_private/logger.mjs';
|
||||
import {getOrCreatePrecacheController} from './utils/getOrCreatePrecacheController.mjs';
|
||||
import {precachePlugins} from './utils/precachePlugins.mjs';
|
||||
import './_version.mjs';
|
||||
import {logger} from 'workbox-core/_private/logger.js';
|
||||
import {getOrCreatePrecacheController} from './utils/getOrCreatePrecacheController.js';
|
||||
import {precachePlugins} from './utils/precachePlugins.js';
|
||||
import {PrecacheEntry} from './_types.js';
|
||||
import './_version.js';
|
||||
|
||||
|
||||
const installListener = (event) => {
|
||||
const installListener = (event: ExtendableEvent) => {
|
||||
const precacheController = getOrCreatePrecacheController();
|
||||
const plugins = precachePlugins.get();
|
||||
|
||||
event.waitUntil(
|
||||
precacheController.install({event, plugins})
|
||||
.catch((error) => {
|
||||
.catch((error: Error) => {
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
logger.error(`Service worker installation failed. It will ` +
|
||||
`be retried automatically during the next navigation.`);
|
||||
@@ -29,11 +30,10 @@ const installListener = (event) => {
|
||||
);
|
||||
};
|
||||
|
||||
const activateListener = (event) => {
|
||||
const activateListener = (event: ExtendableEvent) => {
|
||||
const precacheController = getOrCreatePrecacheController();
|
||||
const plugins = precachePlugins.get();
|
||||
|
||||
event.waitUntil(precacheController.activate({event, plugins}));
|
||||
event.waitUntil(precacheController.activate());
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -55,7 +55,7 @@ const activateListener = (event) => {
|
||||
*
|
||||
* @alias workbox.precaching.precache
|
||||
*/
|
||||
export const precache = (entries) => {
|
||||
export const precache = (entries: Array<PrecacheEntry|string>) => {
|
||||
const precacheController = getOrCreatePrecacheController();
|
||||
precacheController.addToCacheList(entries);
|
||||
|
||||
+7
-4
@@ -6,9 +6,11 @@
|
||||
https://opensource.org/licenses/MIT.
|
||||
*/
|
||||
|
||||
import {addRoute} from './addRoute.mjs';
|
||||
import {precache} from './precache.mjs';
|
||||
import './_version.mjs';
|
||||
import {FetchListenerOptions} from './utils/addFetchListener.js';
|
||||
import {addRoute} from './addRoute.js';
|
||||
import {precache} from './precache.js';
|
||||
import {PrecacheEntry} from './_types.js';
|
||||
import './_version.js';
|
||||
|
||||
|
||||
/**
|
||||
@@ -25,7 +27,8 @@ import './_version.mjs';
|
||||
*
|
||||
* @alias workbox.precaching.precacheAndRoute
|
||||
*/
|
||||
export const precacheAndRoute = (entries, options) => {
|
||||
export const precacheAndRoute =
|
||||
(entries: Array<PrecacheEntry|string>, options: FetchListenerOptions) => {
|
||||
precache(entries);
|
||||
addRoute(options);
|
||||
};
|
||||
+16
-8
@@ -6,13 +6,21 @@
|
||||
https://opensource.org/licenses/MIT.
|
||||
*/
|
||||
|
||||
import {cacheNames} from 'workbox-core/_private/cacheNames.mjs';
|
||||
import {getFriendlyURL} from 'workbox-core/_private/getFriendlyURL.mjs';
|
||||
import {logger} from 'workbox-core/_private/logger.mjs';
|
||||
import {getCacheKeyForURL} from './getCacheKeyForURL.mjs';
|
||||
import '../_version.mjs';
|
||||
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 {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
|
||||
@@ -44,11 +52,11 @@ export const addFetchListener = ({
|
||||
ignoreURLParametersMatching = [/^utm_/],
|
||||
directoryIndex = 'index.html',
|
||||
cleanURLs = true,
|
||||
urlManipulation = null,
|
||||
} = {}) => {
|
||||
urlManipulation,
|
||||
}: FetchListenerOptions = {}) => {
|
||||
const cacheName = cacheNames.getPrecacheName();
|
||||
|
||||
addEventListener('fetch', (event) => {
|
||||
addEventListener('fetch', (event: FetchEvent) => {
|
||||
const precachedURL = getCacheKeyForURL(event.request.url, {
|
||||
cleanURLs,
|
||||
directoryIndex,
|
||||
+3
-3
@@ -6,7 +6,7 @@
|
||||
https://opensource.org/licenses/MIT.
|
||||
*/
|
||||
|
||||
import '../_version.mjs';
|
||||
import '../_version.js';
|
||||
|
||||
/**
|
||||
* @param {Response} response
|
||||
@@ -15,14 +15,14 @@ import '../_version.mjs';
|
||||
* @private
|
||||
* @memberof module:workbox-precaching
|
||||
*/
|
||||
export async function cleanRedirect(response) {
|
||||
export async function cleanRedirect(response: Response) {
|
||||
const clonedResponse = response.clone();
|
||||
|
||||
// Not all browsers support the Response.body stream, so fall back
|
||||
// to reading the entire body into memory as a blob.
|
||||
const bodyPromise = 'body' in clonedResponse ?
|
||||
Promise.resolve(clonedResponse.body) :
|
||||
clonedResponse.blob();
|
||||
clonedResponse!.blob();
|
||||
|
||||
const body = await bodyPromise;
|
||||
|
||||
+14
-8
@@ -6,9 +6,15 @@
|
||||
https://opensource.org/licenses/MIT.
|
||||
*/
|
||||
|
||||
import {WorkboxError} from 'workbox-core/_private/WorkboxError.mjs';
|
||||
import {WorkboxError} from 'workbox-core/_private/WorkboxError.js';
|
||||
import {PrecacheEntry} from '../_types.js';
|
||||
import '../_version.js';
|
||||
|
||||
import '../_version.mjs';
|
||||
|
||||
interface CacheKey {
|
||||
cacheKey: string;
|
||||
url: string;
|
||||
}
|
||||
|
||||
// Name of the search parameter used to store revision info.
|
||||
const REVISION_SEARCH_PARAM = '__WB_REVISION__';
|
||||
@@ -16,13 +22,13 @@ const REVISION_SEARCH_PARAM = '__WB_REVISION__';
|
||||
/**
|
||||
* Converts a manifest entry into a versioned URL suitable for precaching.
|
||||
*
|
||||
* @param {Object} entry
|
||||
* @param {Object|string} entry
|
||||
* @return {string} A URL with versioning info.
|
||||
*
|
||||
* @private
|
||||
* @memberof module:workbox-precaching
|
||||
*/
|
||||
export function createCacheKey(entry) {
|
||||
export function createCacheKey(entry: PrecacheEntry | string): CacheKey {
|
||||
if (!entry) {
|
||||
throw new WorkboxError('add-to-cache-list-unexpected-type', {entry});
|
||||
}
|
||||
@@ -30,7 +36,7 @@ export function createCacheKey(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);
|
||||
const urlObject = new URL(entry, location.href);
|
||||
return {
|
||||
cacheKey: urlObject.href,
|
||||
url: urlObject.href,
|
||||
@@ -45,7 +51,7 @@ export function createCacheKey(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);
|
||||
const urlObject = new URL(url, location.href);
|
||||
return {
|
||||
cacheKey: urlObject.href,
|
||||
url: urlObject.href,
|
||||
@@ -54,8 +60,8 @@ export function createCacheKey(entry) {
|
||||
|
||||
// Otherwise, construct a properly versioned URL using the custom Workbox
|
||||
// search parameter along with the revision info.
|
||||
const originalURL = new URL(url, location);
|
||||
const cacheKeyURL = new URL(url, location);
|
||||
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,
|
||||
+9
-5
@@ -6,7 +6,11 @@
|
||||
https://opensource.org/licenses/MIT.
|
||||
*/
|
||||
|
||||
import '../_version.mjs';
|
||||
import '../_version.js';
|
||||
|
||||
|
||||
// Give TypeScript the correct global.
|
||||
declare var self: ServiceWorkerGlobalScope;
|
||||
|
||||
const SUBSTRING_TO_FIND = '-precache-';
|
||||
|
||||
@@ -29,14 +33,14 @@ const SUBSTRING_TO_FIND = '-precache-';
|
||||
* @memberof module:workbox-precaching
|
||||
*/
|
||||
const deleteOutdatedCaches = async (
|
||||
currentPrecacheName,
|
||||
substringToFind = SUBSTRING_TO_FIND) => {
|
||||
currentPrecacheName: string,
|
||||
substringToFind:string = SUBSTRING_TO_FIND) => {
|
||||
const cacheNames = await caches.keys();
|
||||
|
||||
const cacheNamesToDelete = cacheNames.filter((cacheName) => {
|
||||
return cacheName.includes(substringToFind) &&
|
||||
cacheName.includes(self.registration.scope) &&
|
||||
cacheName !== currentPrecacheName;
|
||||
cacheName.includes(self.registration.scope) &&
|
||||
cacheName !== currentPrecacheName;
|
||||
});
|
||||
|
||||
await Promise.all(
|
||||
+10
-9
@@ -6,9 +6,10 @@
|
||||
https://opensource.org/licenses/MIT.
|
||||
*/
|
||||
|
||||
import {removeIgnoredSearchParams} from './removeIgnoredSearchParams.mjs';
|
||||
import {FetchListenerOptions} from './addFetchListener.js';
|
||||
import {removeIgnoredSearchParams} from './removeIgnoredSearchParams.js';
|
||||
import '../_version.js';
|
||||
|
||||
import '../_version.mjs';
|
||||
|
||||
/**
|
||||
* Generator function that yields possible variations on the original URL to
|
||||
@@ -20,28 +21,28 @@ import '../_version.mjs';
|
||||
* @private
|
||||
* @memberof module:workbox-precaching
|
||||
*/
|
||||
export function* generateURLVariations(url, {
|
||||
export function* generateURLVariations(url: string, {
|
||||
ignoreURLParametersMatching,
|
||||
directoryIndex,
|
||||
cleanURLs,
|
||||
urlManipulation,
|
||||
} = {}) {
|
||||
const urlObject = new URL(url, location);
|
||||
}: FetchListenerOptions = {}) {
|
||||
const urlObject = new URL(url, location.href);
|
||||
urlObject.hash = '';
|
||||
yield urlObject.href;
|
||||
|
||||
const urlWithoutIgnoredParams = removeIgnoredSearchParams(
|
||||
urlObject, ignoreURLParametersMatching);
|
||||
const urlWithoutIgnoredParams =
|
||||
removeIgnoredSearchParams(urlObject, ignoreURLParametersMatching);
|
||||
yield urlWithoutIgnoredParams.href;
|
||||
|
||||
if (directoryIndex && urlWithoutIgnoredParams.pathname.endsWith('/')) {
|
||||
const directoryURL = new URL(urlWithoutIgnoredParams);
|
||||
const directoryURL = new URL(urlWithoutIgnoredParams.href);
|
||||
directoryURL.pathname += directoryIndex;
|
||||
yield directoryURL.href;
|
||||
}
|
||||
|
||||
if (cleanURLs) {
|
||||
const cleanURL = new URL(urlWithoutIgnoredParams);
|
||||
const cleanURL = new URL(urlWithoutIgnoredParams.href);
|
||||
cleanURL.pathname += '.html';
|
||||
yield cleanURL.href;
|
||||
}
|
||||
+6
-5
@@ -6,10 +6,10 @@
|
||||
https://opensource.org/licenses/MIT.
|
||||
*/
|
||||
|
||||
import {getOrCreatePrecacheController}
|
||||
from './getOrCreatePrecacheController.mjs';
|
||||
import {generateURLVariations} from './generateURLVariations.mjs';
|
||||
import '../_version.mjs';
|
||||
import {FetchListenerOptions} from './addFetchListener.js';
|
||||
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
|
||||
@@ -22,7 +22,8 @@ import '../_version.mjs';
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
export const getCacheKeyForURL = (url, options) => {
|
||||
export const getCacheKeyForURL =
|
||||
(url: string, options: FetchListenerOptions): string | void => {
|
||||
const precacheController = getOrCreatePrecacheController();
|
||||
|
||||
const urlsToCacheKeys = precacheController.getURLsToCacheKeys();
|
||||
+3
-3
@@ -6,11 +6,11 @@
|
||||
https://opensource.org/licenses/MIT.
|
||||
*/
|
||||
|
||||
import {PrecacheController} from '../PrecacheController.mjs';
|
||||
import '../_version.mjs';
|
||||
import {PrecacheController} from '../PrecacheController.js';
|
||||
import '../_version.js';
|
||||
|
||||
|
||||
let precacheController;
|
||||
let precacheController: PrecacheController | undefined;
|
||||
|
||||
/**
|
||||
* @return {PrecacheController}
|
||||
+4
-3
@@ -6,10 +6,11 @@
|
||||
https://opensource.org/licenses/MIT.
|
||||
*/
|
||||
|
||||
import '../_version.mjs';
|
||||
import {WorkboxPlugin} from 'workbox-core/utils/pluginUtils.js';
|
||||
import '../_version.js';
|
||||
|
||||
|
||||
const plugins = [];
|
||||
const plugins: WorkboxPlugin[] = [];
|
||||
|
||||
export const precachePlugins = {
|
||||
/*
|
||||
@@ -24,7 +25,7 @@ export const precachePlugins = {
|
||||
* @param {Array} newPlugins
|
||||
* @private
|
||||
*/
|
||||
add(newPlugins) {
|
||||
add(newPlugins: WorkboxPlugin[]) {
|
||||
plugins.push(...newPlugins);
|
||||
},
|
||||
};
|
||||
+10
-4
@@ -6,11 +6,17 @@
|
||||
https://opensource.org/licenses/MIT.
|
||||
*/
|
||||
|
||||
import {logger} from 'workbox-core/_private/logger.mjs';
|
||||
import {logger} from 'workbox-core/_private/logger.js';
|
||||
import '../_version.js';
|
||||
|
||||
import '../_version.mjs';
|
||||
|
||||
const logGroup = (groupTitle, deletedURLs) => {
|
||||
/**
|
||||
* @param {string} groupTitle
|
||||
* @param {Array<string>} deletedURLs
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
const logGroup = (groupTitle: string, deletedURLs: string[]) => {
|
||||
logger.groupCollapsed(groupTitle);
|
||||
|
||||
for (const url of deletedURLs) {
|
||||
@@ -26,7 +32,7 @@ const logGroup = (groupTitle, deletedURLs) => {
|
||||
* @private
|
||||
* @memberof module:workbox-precaching
|
||||
*/
|
||||
export function printCleanupDetails(deletedURLs) {
|
||||
export function printCleanupDetails(deletedURLs: string[]) {
|
||||
const deletionCount = deletedURLs.length;
|
||||
if (deletionCount > 0) {
|
||||
logger.groupCollapsed(`During precaching cleanup, ` +
|
||||
+4
-4
@@ -6,9 +6,9 @@
|
||||
https://opensource.org/licenses/MIT.
|
||||
*/
|
||||
|
||||
import {logger} from 'workbox-core/_private/logger.mjs';
|
||||
import {logger} from 'workbox-core/_private/logger.js';
|
||||
import '../_version.js';
|
||||
|
||||
import '../_version.mjs';
|
||||
|
||||
/**
|
||||
* @param {string} groupTitle
|
||||
@@ -16,7 +16,7 @@ import '../_version.mjs';
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
function _nestedGroup(groupTitle, urls) {
|
||||
function _nestedGroup(groupTitle: string, urls: string[]) {
|
||||
if (urls.length === 0) {
|
||||
return;
|
||||
}
|
||||
@@ -37,7 +37,7 @@ function _nestedGroup(groupTitle, urls) {
|
||||
* @private
|
||||
* @memberof module:workbox-precaching
|
||||
*/
|
||||
export function printInstallDetails(urlsToPrecache, urlsAlreadyPrecached) {
|
||||
export function printInstallDetails(urlsToPrecache: string[], urlsAlreadyPrecached: string[]) {
|
||||
const precachedCount = urlsToPrecache.length;
|
||||
const alreadyPrecachedCount = urlsAlreadyPrecached.length;
|
||||
|
||||
+3
-3
@@ -6,7 +6,7 @@
|
||||
https://opensource.org/licenses/MIT.
|
||||
*/
|
||||
|
||||
import '../_version.mjs';
|
||||
import '../_version.js';
|
||||
|
||||
/**
|
||||
* Removes any URL search parameters that should be ignored.
|
||||
@@ -20,8 +20,8 @@ import '../_version.mjs';
|
||||
* @private
|
||||
* @memberof module:workbox-precaching
|
||||
*/
|
||||
export function removeIgnoredSearchParams(urlObject,
|
||||
ignoreURLParametersMatching) {
|
||||
export function removeIgnoredSearchParams(
|
||||
urlObject: URL, ignoreURLParametersMatching: RegExp[] = []): URL {
|
||||
// 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()]) {
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"extends": "../../tsconfig",
|
||||
"compilerOptions": {
|
||||
"outDir": "./",
|
||||
"rootDir": "./src"
|
||||
},
|
||||
"include": [
|
||||
"src/**/*.ts"
|
||||
]
|
||||
}
|
||||
@@ -29,9 +29,8 @@ describe(`addPlugins()`, function() {
|
||||
sandbox.restore();
|
||||
});
|
||||
|
||||
it(`should add plugins during install and activate`, async function() {
|
||||
it(`should add plugins during install`, async function() {
|
||||
sandbox.spy(PrecacheController.prototype, 'install');
|
||||
sandbox.spy(PrecacheController.prototype, 'activate');
|
||||
|
||||
const precacheArgs = ['/'];
|
||||
const plugin1 = {name: 'plugin1'};
|
||||
@@ -48,13 +47,5 @@ describe(`addPlugins()`, function() {
|
||||
plugin1,
|
||||
plugin2,
|
||||
]);
|
||||
|
||||
await dispatchAndWaitUntilDone(new ExtendableEvent('activate'));
|
||||
|
||||
expect(PrecacheController.prototype.activate.callCount).to.equal(1);
|
||||
expect(PrecacheController.prototype.activate.args[0][0].plugins).to.deep.equal([
|
||||
plugin1,
|
||||
plugin2,
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
{ "path": "./packages/workbox-core/" },
|
||||
{ "path": "./packages/workbox-expiration/" },
|
||||
{ "path": "./packages/workbox-navigation-preload/" },
|
||||
{ "path": "./packages/workbox-precaching/" },
|
||||
{ "path": "./packages/workbox-window/" }
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user