Skip to content

Commit

Permalink
Precaching updates for v6 (#2639)
Browse files Browse the repository at this point in the history
* Initial precaching updates for v6

* Address review feedback
  • Loading branch information
philipwalton committed Oct 5, 2020
1 parent bb21e82 commit a6751b9
Show file tree
Hide file tree
Showing 41 changed files with 1,438 additions and 2,136 deletions.
4 changes: 4 additions & 0 deletions packages/workbox-core/src/_private.ts
Expand Up @@ -9,6 +9,7 @@
// We either expose defaults or we expose every named export.
import {assert} from './_private/assert.js';
import {cacheNames} from './_private/cacheNames.js';
import {cacheMatchIgnoreParams} from './_private/cacheMatchIgnoreParams.js';
import {canConstructReadableStream} from './_private/canConstructReadableStream.js';
import {canConstructResponseFromBodyStream} from './_private/canConstructResponseFromBodyStream.js';
import {dontWaitFor} from './_private/dontWaitFor.js';
Expand All @@ -20,12 +21,14 @@ import {getFriendlyURL} from './_private/getFriendlyURL.js';
import {logger} from './_private/logger.js';
import {resultingClientExists} from './_private/resultingClientExists.js';
import {timeout} from './_private/timeout.js';
import {waitUntil} from './_private/waitUntil.js';
import {WorkboxError} from './_private/WorkboxError.js';

import './_version.js';

export {
assert,
cacheMatchIgnoreParams,
cacheNames,
canConstructReadableStream,
canConstructResponseFromBodyStream,
Expand All @@ -38,5 +41,6 @@ export {
logger,
resultingClientExists,
timeout,
waitUntil,
WorkboxError,
};
58 changes: 58 additions & 0 deletions packages/workbox-core/src/_private/cacheMatchIgnoreParams.ts
@@ -0,0 +1,58 @@
/*
Copyright 2020 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';


function stripParams(fullURL: string, ignoreParams: string[]) {
const strippedURL = new URL(fullURL);
for (const param of ignoreParams) {
strippedURL.searchParams.delete(param);
}
return strippedURL.href;
}


/**
* Matches an item in the cache, ignoring specific URL params. This is similar
* to the `ignoreSearch` option, but it allows you to ignore just specific
* params (while continuing to match on the others).
*
* @private
* @param {Cache} cache
* @param {Request} request
* @param {Object} matchOptions
* @param {Array<string>} ignoreParams
* @return {Promise<Response|undefined>}
*/
async function cacheMatchIgnoreParams(
cache: Cache,
request: Request,
ignoreParams: string[],
matchOptions?: CacheQueryOptions,
): Promise<Response | undefined> {
const strippedRequestURL = stripParams(request.url, ignoreParams);

// If the request doesn't include any ignored params, match as normal.
if (request.url === strippedRequestURL) {
return cache.match(request, matchOptions);
}

// Otherwise, match by comparing keys
const keysOptions = {...matchOptions, ignoreSearch: true};
const cacheKeys = await cache.keys(request, keysOptions);

for (const cacheKey of cacheKeys) {
const strippedCacheKeyURL = stripParams(cacheKey.url, ignoreParams);
if (strippedRequestURL === strippedCacheKeyURL) {
return cache.match(cacheKey, matchOptions);
}
}
return;
}

export {cacheMatchIgnoreParams};
26 changes: 26 additions & 0 deletions packages/workbox-core/src/_private/waitUntil.ts
@@ -0,0 +1,26 @@
/*
Copyright 2020 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';


/**
* A utility method that makes it easier to use `event.waitUntil` with
* async functions and return the result.
*
* @param {ExtendableEvent} event
* @param {Function} asyncFn
* @return {Function}
* @private
*/
function waitUntil(event: ExtendableEvent, asyncFn: () => Promise<any>) {
const returnPromise = asyncFn();
event.waitUntil(returnPromise);
return returnPromise;
}

export {waitUntil};

0 comments on commit a6751b9

Please sign in to comment.