© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Cloudflare DevelopersCD
Cloudflare Developers•2y ago•
5 replies
MrXO

Cloudflare worker Cache not work

Hi, I tried to put the cache but not work, not sure why.

here is my code

app.get('/get_all_order_status', async (ctx) => {
const cacheUrl = ctx.req.url;
const cacheKey = new Request(cacheUrl.toString(), ctx.req);
const cache = caches.default;

let fromCache = true;
let response = await cache.match(cacheKey);

if (!response) {
const data = await Model.getAllOrderStatus();
response = new Response(JSON.stringify(data), {
headers: { 'content-type': 'application/json' },
});

// Add a custom header to the response before putting it in the cache
fromCache = false;

// Cache the response indefinitely
try {
console.log(cacheKey.method, cacheKey.url);
console.log(response.status, response.statusText);
response.headers.append("Cache-Control", "s-maxage=10000000");
ctx.executionCtx.waitUntil(cache.put(cacheKey, response.clone()));
} catch (error) {
console.error('Error putting data in cache:', error);
}
}

response.headers.set('X-From-Cache', fromCache.toString());
return response;
});

The cacheKey and response is OK but cache.put not work.
Cloudflare Developers banner
Cloudflare DevelopersJoin
Welcome to the official Cloudflare Developers server. Here you can ask for help and stay updated with the latest news
85,042Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Can Cloudflare cache html response from worker?
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
3y ago
cache worker request not working?
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
16mo ago
Cache worker result
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
2y ago