Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial precaching updates for v6 * Address review feedback
- Loading branch information
1 parent
bb21e82
commit a6751b9
Showing
41 changed files
with
1,438 additions
and
2,136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
packages/workbox-core/src/_private/cacheMatchIgnoreParams.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}; |
Oops, something went wrong.