From b17bbd04dc67778004392da91841092e55847d37 Mon Sep 17 00:00:00 2001 From: Artur Wronski Date: Thu, 1 Mar 2018 15:41:37 +0100 Subject: [PATCH] feat: Add support of StrategyOptions to cache --- docs/modules/workbox.md | 18 ++++++++++++++++++ packages/workbox/templates/sw.template.js | 6 ++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/docs/modules/workbox.md b/docs/modules/workbox.md index 035addc9..744ddbc3 100644 --- a/docs/modules/workbox.md +++ b/docs/modules/workbox.md @@ -47,6 +47,24 @@ workbox: { } ``` +#### Adding custom cache [StrategyOption](https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-sw.Strategies#.StrategyOptions) +```js +workbox: { + runtimeCaching: [ + { + urlPattern: 'https://my-cdn.com/posts/.*', + strategyOptions: { + cacheName: 'our-cache', + cacheExpiration: { + maxEntries: 10, + maxAgeSeconds: 300 + } + } + } + ] +} +``` + ### Adding custom service worker Create `static/custom-sw.js` file: diff --git a/packages/workbox/templates/sw.template.js b/packages/workbox/templates/sw.template.js index c9bced95..5f98af89 100644 --- a/packages/workbox/templates/sw.template.js +++ b/packages/workbox/templates/sw.template.js @@ -4,6 +4,8 @@ const workboxSW = new self.WorkboxSW(<%= JSON.stringify(options.wbOptions, null, workboxSW.precache([]) -<% options.runtimeCaching.forEach(r => { %> -workboxSW.router.registerRoute(new RegExp('<%= r.urlPattern %>'), workboxSW.strategies.<%= r.handler %>({}), '<%= r.method %>') +<% options.runtimeCaching.forEach(r => { + const strategy = JSON.stringify(r.strategyOptions || {}) + %> +workboxSW.router.registerRoute(new RegExp('<%= r.urlPattern %>'), workboxSW.strategies.<%= r.handler %>(<%= strategy %>), '<%= r.method %>') <% }) %>