Caching Third-Party API Responses Served by a Cloudflare Worker

I’m working on caching the JSON output of a third-party API, which itself is served by a Cloudflare worker and returns with a cf-cache-status: DYNAMIC. My goal is to cache this data in my Cloudflare worker to improve performance.

My current approach is with fetch() so I can use tiered-cache, as shown in the code snippet below. However, my own worker also results in a cf-cache-status: DYNAMIC and does not seem to cache the endpoint.

// Attempting to cache the third-party API response
const res = await fetch(endpointUrl, { cf: { cacheTtl: 300, cacheEverything: true }});

The headers response of the API am trying to cache are as follows:

HTTP/2 200 
date: Mon, 04 Dec 2023 01:42:08 GMT
content-type: application/json; charset=utf-8
content-range: 0-7/*
cf-ray: 830059df5c601686-SJC
cf-cache-status: DYNAMIC
access-control-allow-origin: *
content-location: /todos?select=%2A
strict-transport-security: max-age=2592000; includeSubDomains
vary: Accept-Encoding
via: kong/2.8.1
content-profile: public
x-kong-proxy-latency: 4
x-kong-upstream-latency: 17
server: cloudflare
alt-svc: h3=":443"; ma=86400

I do not believe you can cache another person’s Cloudflare resource. This diagram somewhat describes what’s going on:

2 Likes

Thanks for your reply @sdayman and the link to the docs, this might push me to Hyperdrive.

The third party API is a postgREST server which sits on top of postgres to provide a REST API for the DB. So when I send a GET to “https://postgrest-server.com/todos?select=*” it responds back with a JSON results from the query. Since the third party API is using also using a worker from what I can tell, I am unable to cache the JSON results?