DEV Community

Daniel Pertu
Daniel Pertu

Posted on

Three of our 38 pages tell every cache not to store them, and all three are the ones with a price on

Notifio is a desktop app, and the site that sells it is 38 URLs of Next.js 16 on Vercel: a home page, pricing, download, 16 pages about individual rental sites, five comparisons, five guides, three audience pages and the usual legal pair.

I know which of those are static because I wrote them. What I had never done is check from the outside, which is the only view that matches what a visitor and a crawler actually get. So I pulled the sitemap and sent a HEAD request to every URL in it:

curl -s https://notifio.app/sitemap.xml | grep -o '<loc>[^<]*' | cut -c6- > urls.txt

while read -r u; do
  curl -sI "$u" | grep -iE '^HTTP|cache-control|x-vercel-cache|^age:|x-nextjs|content-length'
done < urls.txt
Enter fullscreen mode Exit fullscreen mode

38 URLs, two completely different answers, and the split is not where you would guess.

The two answers

35 pages come back like this:

HTTP/2 200
age: 1442564
cache-control: public, max-age=0, must-revalidate
x-nextjs-prerender: 1
x-nextjs-stale-time: 300
x-vercel-cache: HIT
content-length: 60694
Enter fullscreen mode Exit fullscreen mode

The other three come back like this:

HTTP/2 200
age: 0
cache-control: private, no-cache, no-store, max-age=0, must-revalidate
x-vercel-cache: MISS
Enter fullscreen mode Exit fullscreen mode

No x-nextjs-prerender, no content-length because the response is streamed, and a cache-control that forbids storage in the browser, in the CDN, and in any proxy between them. private rules out shared caches specifically.

The three are /, /pricing and /download. The home page, the pricing page and the download page: the three most commercially important URLs on the site, and the only three that render on every request.

Why those three

// src/app/pricing/page.tsx
export const dynamic = "force-dynamic";
Enter fullscreen mode Exit fullscreen mode

Three pages declare that, and one helper is the reason:

export async function getDisplayCurrency(): Promise<DisplayCurrency> {
  const headerList = await headers();

  // Set by Vercel's edge network on every request. Absent in local development,
  // where we correctly fall back to GBP.
  return currencyForVisitor(headerList.get("x-vercel-ip-country"), headerList.get("user-agent"));
}
Enter fullscreen mode Exit fullscreen mode

Reading a request header is what makes a page per-request. The module that does it writes down the trade in full, and I am glad it does, because this is exactly the decision somebody would reverse to make a Lighthouse number go up:

 * ## The cost
 *
 * Reading the request makes the calling page render per request, so the pages
 * that show prices give up static rendering and must be marked
 * `force-dynamic`. That is the intended trade: a page that renders instantly
 * and shows the wrong number is worse than one that renders correctly.
Enter fullscreen mode Exit fullscreen mode

The alternative is resolving the currency in the browser, which paints one price and then swaps it, on the exact number the visitor came to read. So three pages pay for a function invocation per request, and 35 do not.

One more line in that module explains a thing the headers cannot show you: crawlers are geolocated to whichever datacentre they are in, so a bot would index dollar prices that disagree with the GBP figures in the page metadata and structured data. Bots are given GBP deliberately.

The 16-day-old cache and the 300-second stale time

Those two numbers in the static responses are worth sitting with:

age: 1442564            # 16.7 days
x-nextjs-stale-time: 300
cache-control: public, max-age=0, must-revalidate
Enter fullscreen mode Exit fullscreen mode

Across the 35 prerendered pages, age ranges from 1,420,679 to 1,449,621 seconds, which is 16.4 to 16.8 days. The last deploy of this site was 17 days before I ran the sweep. So every one of those CDN entries dates from that deploy, and the 8-hour spread between the youngest and oldest is simply the order in which this edge location first needed each page.

Three caches are being described by one response, and they disagree on purpose:

  • cache-control: max-age=0, must-revalidate is for the browser. Ask again every time.
  • x-nextjs-stale-time: 300 is for Next.js's client-side router cache, the in-memory one that makes a prefetched link instant. Five minutes.
  • x-vercel-cache: HIT with an age of 16 days is the CDN, which holds the prerendered HTML until something invalidates it, and the thing that invalidates it is a deploy.

If you read the first two and conclude the site revalidates constantly, you have the wrong model. There is no revalidation here at all. These pages are built from TypeScript arrays at build time, so new HTML exists only when I deploy, and the CDN is correct to hold a copy for 16 days.

sitemap.xml is the odd one out: HIT with an age of 1.5 days rather than 16. It is a route handler rather than a page, with its own cache entry and its own eviction, and nothing about the 38 URLs in it changed in the meantime.

The tripwire on the other 35

Nine page files declare the opposite of the trio:

export const dynamic = "force-static";
Enter fullscreen mode Exit fullscreen mode

That is not required. Those pages would be static anyway, because none of them reads a request. It is there because of what happens if one of them ever does.

Under force-static, Next does not fail the build and does not quietly make the page dynamic. It makes headers() return empty values. Combined with the currency helper's fallback, a comparison page that accidentally grew a price component would show GBP to every visitor in the world, and stay static. That is a visible, uniform, obvious bug, and I would much rather have it than the alternative, which is 16 pages silently moving from the CDN to a function invocation per request with nothing in the product to say so.

It is a declaration that says "if this page ever wants to read the request, I want to find out by seeing the wrong currency, not by reading a bill".

What the static 35 weigh

Since content-length comes free with the sweep:

35 pages          1,991,036 bytes of HTML
average           56.9 KB
largest           /compare/visualping     72,321 bytes
smallest          /about                  33,994 bytes
Enter fullscreen mode Exit fullscreen mode

57 KB of HTML per page is not small for pages with no images in them, and the reason is that the content of each page is a TypeScript object rendered into a lot of markup. The three dynamic pages have no content-length at all, so a sweep like this cannot weigh them, which is its own small lesson about measuring streamed responses.

One aside about the config

While checking which pages opt out, I opened next.config.ts:

import type { NextConfig } from "next";

const nextConfig: NextConfig = {
  // Stripe webhooks need the raw body, disable body parsing for that route
  // handled per-route via export const config
};

export default nextConfig;
Enter fullscreen mode Exit fullscreen mode

The object is empty. The comment describes a Pages Router concern: App Router route handlers get the raw body from await req.text(), which is exactly what our Stripe webhook does, and no export const config exists anywhere in the repository. So the entire configuration of this Next.js app is a comment about a problem it does not have. That is a good sign, and the comment should still go.

Worth running on your own site

The whole measurement is two commands and no tooling, and it answers a question that is surprisingly hard to answer from inside a codebase: which of my pages are actually static right now, in production, today. A headers() call three imports deep in a shared component moves a page from the first category to the second, and nothing in your build output shouts about it.

The signal to look for is x-nextjs-prerender. If a page you believe is static does not have it, something on that page is reading the request.

If you want to see the three pages that pay for themselves, they are the home page, pricing and download, and the price you see on each one is resolved before the HTML leaves the server.

Top comments (3)

Collapse
 
hanin_mp profile image
Hanin Mp •

Using x-nextjs-prerender as the signal for what is actually static in production is a neat trick. The trio being home, pricing and download is the classic trade-off. I would measure TTFB for those three against the static 35 from a couple of regions, since the function invocation sits directly on the LCP path and origin-level field data blends both groups. Serving crawlers GBP by default keeps the structured data consistent, which is the right call. Have you compared field LCP for the three dynamic pages yet?

Collapse
 
linkbruno profile image
linkbruno.com •

Did the three MISS pages look the same on a GET? curl -sI sends HEAD, and a server is allowed to answer HEAD with different headers than GET, which matters most for streamed, force-dynamic routes. Repeating the sweep with curl -s -D - -o /dev/null would show exactly what a browser or crawler receives.

Collapse
 
elias_0549e0e2ef8cf6ad89a profile image
RAMO •

The detail that it's a single helper like getDisplayCurrency pulling the whole route into dynamic rendering is a nasty one, since nothing in the page itself looks dynamic. Checking from the outside with HEAD requests against the sitemap is a great audit habit. I'd be curious whether moving the currency choice to the client, or into a small cached variant per currency, kept the pages static for you.