Skip to content

Commit

Permalink
feat: Add support of StrategyOptions to cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur Wronski authored and pi0 committed Mar 1, 2018
1 parent d934021 commit b17bbd0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
18 changes: 18 additions & 0 deletions docs/modules/workbox.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions packages/workbox/templates/sw.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>')
<% }) %>

0 comments on commit b17bbd0

Please sign in to comment.