Last active
November 5, 2020 05:28
-
-
Save shagamemnon/a2429bc007eb365e452320440e79b298 to your computer and use it in GitHub Desktop.
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
# 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