Skip to content
Search
  • ServicesPage
  • Our workPage
  • Case studiesPage
  • BlogPage
  • AI solutionsPage
  • Contact usPage
All articles

Caching a Next.js site on Cloudflare without breaking it

Moinul Hossain Miraj · · 3 min read · Web Development

Caching a Next.js site on Cloudflare without breaking it

A load test told us our pages slowed down badly at 50 to 150 users. The pages themselves were static: Next.js had prerendered them and sent Cache-Control: s-maxage=3600. Yet every request went all the way to our Node server. Cloudflare sat in front and cached nothing.

Why Cloudflare did not cache the pages

Cloudflare's response header said:

code
cf-cache-status: DYNAMIC

By default, Cloudflare caches static files (images, scripts, styles) but not HTML, whatever your cache headers say. To cache pages you need a Cache Rule.

The rule

In Cloudflare, under Caching and Cache Rules, we created one rule:

  • Match: GET requests for the site, except the paths that must never be cached.
  • Cache eligibility: eligible for cache.
  • Edge TTL: use the cache-control header if present, bypass cache if not.
  • Browser TTL: respect the origin.

The expression looked like this:

code
(http.host eq "example.com"
 and http.request.method eq "GET"
 and not starts_with(http.request.uri.path, "/admin")
 and not starts_with(http.request.uri.path, "/api")
 and not starts_with(http.request.uri.path, "/cart")
 and not starts_with(http.request.uri.path, "/checkout")
 and not starts_with(http.request.uri.path, "/search")
 and not any(http.request.headers.names[*] in {"rsc" "next-router-prefetch"}))

Using "use cache-control header if present" means the site stays in charge: pages that send s-maxage get cached for that long, and anything dynamic that sends private or no-store is left alone.

The trap: the RSC header

The last line matters most, and it is easy to miss.

With the App Router, Next.js uses the same URL for two different things:

  • A normal visit to /services returns HTML.
  • When you click a link to /services inside the site, the browser fetches the same URL with an RSC: 1 header and gets the page's React Server Component data instead of HTML.

The response carries Vary: rsc, ... to say "these differ", but Cloudflare does not use Vary for HTML. Without the exclusion, Cloudflare could cache the RSC data under /services and then serve that raw data to the next visitor who opens the page directly. They would see a screen of text instead of your site.

Skipping requests that carry the rsc or next-router-prefetch headers keeps those requests out of the cache. Normal page visits are cached; in-app navigation goes to the origin, which is fine because Next.js caches that data on the server.

The second trap: testing with HEAD

After deploying the rule, we checked it:

code
curl -sI https://example.com/services | grep cf-cache-status

DYNAMIC, every time. We simplified the rule, checked the order, and checked that it was active. Still DYNAMIC.

The rule was fine. curl -I sends a HEAD request, and our rule only matches GET. Testing with a real GET request:

code
curl -s -o /dev/null -D - https://example.com/services | grep -i cf-cache-status

The first request said MISS, the second HIT.

How we checked it was safe

  • Cached pages: /, /services, /blog go from MISS to HIT.
  • Not cached: /cart, /checkout, /search and /api stay DYNAMIC.
  • A request with RSC: 1 is not cached, and a normal visit still returns text/html.

Things to remember

  • Content edits now take up to the cache time to appear. Purge the cache after an important change, or connect your CMS publish event to a purge.
  • Exclude anything personal (cart, checkout, account) and anything that depends on the query (search).
  • Test with GET, not HEAD, and test twice.

After the rule, most page requests are answered by Cloudflare's edge and the origin barely notices traffic spikes. If your Next.js site is behind Cloudflare but every request still hits your server, we can help set it up.

Have a project in mind?

Tell us what you need. We get back to you as soon as possible.

Start a project