Caching for Server Side Rendered Astro JS site in cloudflare pages

I have deployed an Astro JS site in Cloudflare pages with Server Side Rendering enabled. I’m unable to find documentation related to caching of Server Side rendered pages. I also want a way to refresh the cache after a certain duration to achieve eventual consistency. How do I achieve this?

Pages does its own caching. You don’t need to do anything special.

1 Like

let’s say Im getting some data from a database and displaying in the webpage, I want my user to still see the same content even if the data changes in the database until the cache expires.
How can I achieve this?
If Im seeing the new data immediately when the data is changed in database doesn’t it mean the data is not cached and rendered every time in server?

You would have to layer your own caching on top, to get that behavior. You could use Workers or the cache settings or rules in the dashboard, but I don’t know that I’d recommend it.

What are you trying to accomplish? Reducing the load on your database?

1 Like

No, The data in the database won’t change frequently. So I don’t want my users to get the webpage from server every time. We’ll refresh the cache when the data changes or over a period of time. It doesn’t matter if the user see’s the old data.

Hey there,

If you have control over the Pages Functions code used to generate SSR responses, you can utlise the Workers Cache API within the Function to cache these resources:

Note that the Function will still execute on a cache hit from the Cache API, it just saves on your external API calls.

Additionally if you only need caching on a custom domain you should be able to add it on top using Cache Rules to mark paths as eligible for cache. Please note that this setup is not recommended and can cause issues with stale assets, though it seems you are already aware of that.