Steven Prins

Steven Prins

Cache the response of Cloudflare Functions

15-10-2022 | 109 words | 1 minute reading time | Tags:

When using Functions with Cloudflare Pages, you can cache the response with Page Rules. You can see it for example here. The time will not updated when you refresh the page, because the response will be cached on the Cloudflare Edge Network, and thus will not add to your Workers usage. The function contains the following code:

1export async function onRequestGet() {
2  return new Response(`This response will be cached by Cloudflare! Server time: ${new Date()}`);
3}

I’m not sure if this intended behavior, because it doesn’t work for normal Cloudflare Workers. For this to work, you need the following Page Rule:

  • Setting: Cache Level
  • Cache Level: Cache Everything