Skip to content

Instantly share code, notes, and snippets.

@shagamemnon
Last active November 5, 2020 05:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shagamemnon/a2429bc007eb365e452320440e79b298 to your computer and use it in GitHub Desktop.
Save shagamemnon/a2429bc007eb365e452320440e79b298 to your computer and use it in GitHub Desktop.
# Cache API Docs + Example
- https://developers.cloudflare.com/workers/about/using-cache/
- https://developers.cloudflare.com/workers/reference/apis/cache
```js
addEventListener('fetch', event => {
event.respondWith(handleRequest(event))
})
async function handleRequest(request) {
let request = event.request
let cache = caches.default
let response = await cache.match(request)
if (response) {
return response
}
response = await fetch(request)
response = new Response(response.body, response)
response.headers.set('Cache-Tag', 'pdp')
event.waitUntil(cache.put(request, response.clone()))
return response
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment