Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 9kopb/51fc31620d9ff3eeaebf769b35948b37 to your computer and use it in GitHub Desktop.
Save 9kopb/51fc31620d9ff3eeaebf769b35948b37 to your computer and use it in GitHub Desktop.
addEventListener("fetch", (event) => {
event.respondWith(
handleRequest(event.request).catch(
(err) => new Response(err.stack, { status: 500 })
)
);
});
async function handleRequest(request) {
const { pathname, searchParams, host } = new URL(request.url);
const url = new URL(request.url)
url.hostname = "photos.example.com"
const newRequest = new Request(
url.toString(),
request
)
let response = await fetch(newRequest,
{
cf: {
// Always cache this fetch regardless of content type
cacheTtlByStatus: { "200-299": 16070400, 404: 1, "500-599": 0 }
},
});
response = new Response(response.body, response)
// Set cache control headers to cache on browser for 25 minutes
response.headers.set("Cache-Control", "public, max-age=16070400")
return response
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment