-
Notifications
You must be signed in to change notification settings - Fork 831
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/recipes precache and normalize (#2718)
* Add warmStrategyCache recipe Takes an array of items and a Workbox Strategy and warms the strategy’s cache with those items at install. Useful for getting specific items into cache but allowing them to be updated outside of the Service Worker install cycle * Add warmCache and plugins options Give users of page/image/resource recipes the ability to warm those caches using warmStrategyCache functionality. Also allows them to include plugins if desired (for instance, to normalize URLs coming into the cache) * Remove offlineFallback dependency on precache While precache will still work, and that will be tried first, the files will be saved in a workbox-offline-fallbacks cache and the cache will try to be retrieved from there if precache fails. * Add missing newline * Export warmStrategyCache * Update packages/workbox-recipes/src/warmStrategyCache.ts Co-authored-by: Jeffrey Posnick <jeffy@google.com> * Rename 'warm' and 'paths' Improves clarity of each, pre PR review request * Update dependencies to use 'urls' instead of 'paths' Co-authored-by: Jeffrey Posnick <jeffy@google.com>
- Loading branch information
1 parent
b5d7244
commit 3151c3a
Showing
6 changed files
with
128 additions
and
36 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
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
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
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
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
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,31 @@ | ||
import { Strategy } from 'workbox-strategies/src/Strategy'; | ||
|
||
import './_version.js'; | ||
|
||
export interface WarmStrategyCacheOptions { | ||
urls: Array<string>; | ||
strategy: Strategy; | ||
} | ||
|
||
// Give TypeScript the correct global. | ||
declare let self: ServiceWorkerGlobalScope; | ||
|
||
/** | ||
* @memberof module:workbox-recipes | ||
* @param {Object} options | ||
* @param {string[]} options.urls Paths to warm the strategy's cache with | ||
* @param {Strategy} options.strategy Strategy to use | ||
*/ | ||
function warmStrategyCache(options: WarmStrategyCacheOptions): void { | ||
self.addEventListener('install', event => { | ||
const done = options.urls.map(path => options.strategy.handleAll({ | ||
event, | ||
request: new Request(path), | ||
})[1]); | ||
|
||
event.waitUntil(Promise.all(done)); | ||
}); | ||
} | ||
|
||
export {warmStrategyCache}; |