Skip to content
This repository has been archived by the owner on Aug 10, 2022. It is now read-only.

Commit

Permalink
Add Request.destination recipe (#6811)
Browse files Browse the repository at this point in the history
This is a follow-up on GoogleChrome/workbox#1606.
  • Loading branch information
tomayac authored and petele committed Nov 9, 2018
1 parent b7682c9 commit 48d749f
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/content/en/tools/workbox/guides/common-recipes.md
Expand Up @@ -2,7 +2,7 @@ project_path: /web/tools/workbox/_project.yaml
book_path: /web/tools/workbox/_book.yaml
description: Common recipes to use with Workbox.

{# wf_updated_on: 2018-10-11 #}
{# wf_updated_on: 2018-11-07 #}
{# wf_published_on: 2017-11-15 #}
{# wf_blink_components: N/A #}

Expand Down Expand Up @@ -181,6 +181,36 @@ workbox.routing.registerRoute(
);
```

## Cache Resources Based on Resource Type

You can use the
[`RequestDestination`](https://developer.mozilla.org/en-US/docs/Web/API/RequestDestination)
enumerate type of the
[destination](https://medium.com/dev-channel/service-worker-caching-strategies-based-on-request-types-57411dd7652c)
of the request to determine a strategy.
For example, when the target is `<audio>` data:

```javascript
workbox.routing.registerRoute(
// Custom `matchCallback` function
(context) => {
if (context.event.request.destination === 'audio') {
return true;
}
return null;
},
workbox.strategies.cacheFirst({
cacheName: 'audio',
plugins: [
new workbox.expiration.Plugin({
maxEntries: 60,
maxAgeSeconds: 30 * 24 * 60 * 60, // 30 Days
}),
],
})
);
```

## Access Caches from Your Web App's Code

The [Cache Storage
Expand Down

0 comments on commit 48d749f

Please sign in to comment.