On a Worker, I am using the cf: {cacheEverything: true, cacheTtl: 60}
request option (per your “cache using fetch” docs), but my requests are still coming back as “cf-cache-status:DYNAMIC”.
How can I correctly cache a remote fetch call, in order to proxy a remote API response through Cloudflare’s edge?
Sample code:
// See example: https://developers.cloudflare.com/workers/examples/cache-using-fetch
async function handleRequest(request) {
const url = new URL(request.url)
const destinationURL = "https://site-api.datocms.com/" + url.pathname + url.search
const myTtl=60
let myHeaders = new Headers();
myHeaders.append("X-Api-Version", "3");
myHeaders.append("Accept", "application/json");
myHeaders.append("Authorization", "Bearer abcdefgh1234567890");
let response = await fetch(destinationURL, {
cf: {
cacheTtl: myTtl,
cacheEverything: true,
},
headers: myHeaders
})
// Reconstruct the Response object to make its headers mutable.
response = new Response(response.body, response)
response.headers.set("Cache-Control", `max-age=${myTtl}`)
return response
}
addEventListener("fetch", event => {
return event.respondWith(handleRequest(event.request))
})
Response headers:
access-control-allow-credentials:true
access-control-allow-headers:authorization, content-type, x-environment, x-site-domain, x-api-version, user-agent, x-session-id
access-control-allow-methods:GET, POST, PUT, OPTIONS, DELETE
access-control-allow-origin:*
access-control-expose-headers:x-ratelimit-limit, x-ratelimit-remaining, x-ratelimit-reset
access-control-max-age:1728000
cache-control:max-age=60
cf-cache-status:DYNAMIC
cf-ray:686fde4a3fa18121-ORD
content-type:application/json; charset=utf-8
date:Mon, 30 Aug 2021 17:51:38 GMT
expect-ct:max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
referrer-policy:strict-origin-when-cross-origin
server:cloudflare
strict-transport-security:max-age=15552000; includeSubDomains; preload
transfer-encoding:chunked
vary:Accept,Accept-Encoding
via:1.1 vegur
x-content-type-options:nosniff
x-download-options:noopen
x-frame-options:SAMEORIGIN
x-permitted-cross-domain-policies:none
x-queue-time:0ms
x-ratelimit-limit:60
x-ratelimit-remaining:59
x-request-id:dc1904a2-ab45-4ac3-a944-d5c3eb73bd4d
x-runtime:0.002616
x-xss-protection:1; mode=block