-
-
Save 9kopb/51fc31620d9ff3eeaebf769b35948b37 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
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