DEV Community

Daniel Pertu
Daniel Pertu

Posted on

Scrolling to the bottom of one of our pages fetches 39 more, and the two that cost a server render are in every footer

Nakodo is 106 static pages with the same footer on all of them: five columns, 37 links, built from the content registry so every family's hub is one click from anywhere. It is a good footer. It is also, as far as the browser is concerned, a list of 37 things to go and download.

Here is the whole of our prefetch configuration, across 616 source files:

$ grep -rn "prefetch" src --include=*.tsx --include=*.ts
src/proxy.ts:61:  //   navigations and prefetches fetch them by. The sign-in pages still run it.
Enter fullscreen mode Exit fullscreen mode

A comment. Not one <Link prefetch={...}> anywhere. So what follows is what Next 16 does when you leave it alone.

The measurement

For four pages: disable the HTTP cache through CDP, load, wait, then scroll to the bottom in six steps and wait again. Count the entries in performance.getEntriesByType("resource") whose URL carries _rsc=.

Page Document After load At the bottom
/ 52,220 B 17 reqs, 78,303 B 74 reqs, 346,511 B
/guides/how-to-find-influencers 48,296 B 18 reqs, 78,737 B 78 reqs, 395,684 B
/tools 37,440 B 25 reqs, 123,702 B 71 reqs, 343,245 B
/pricing 43,763 B 24 reqs, 84,087 B 78 reqs, 349,744 B

Reading one guide to the end costs 8.2 times the page's own transfer in payloads for pages the reader has not asked for. 39 distinct routes, out of the 42 distinct internal hrefs on the page.

This is not a bug and it is not misconfiguration. The docs that ship inside the package say exactly this:

As each <Link> enters the viewport, Next.js prefetches the route behind it and schedules the work so a page full of links doesn't flood the network.

The scheduler is real and you can see it working: the requests arrive in waves as each footer column scrolls into view, not in one burst.

Two requests per route

78 requests for 39 routes is not a retry. Each route is fetched twice, with different request headers. Capturing Network.requestWillBeSent through CDP and filtering the headers:

/guides?_rsc=5CB68i4pnAekjehf
  rsc: 1
  next-router-prefetch: 1
  next-router-segment-prefetch: /_tree
  next-url: /

/guides?_rsc=iq8w9ONpDr2xWDZM
  rsc: 1
  next-router-prefetch: 1
  next-router-segment-prefetch: /!KG1hcmtldGluZyk/guides/__PAGE__
  next-url: /
Enter fullscreen mode Exit fullscreen mode

The first is the route tree, 1,680 bytes on the wire, and every tree request on the page shares one _rsc token because they are all made from the same router state. The second is the page segment itself: 41,927 bytes uncompressed, about 8 KB on the wire for that route.

!KG1hcmtldGluZyk is base64 for (marketing). Our route group name, the one thing in the App Router that is supposed to be invisible in URLs, travels in a header on every prefetch in the browser's network panel. Harmless, and a small surprise.

The part that costs money

Static routes come back from the edge:

$ curl -sI 'https://nakodo.app/guides?_rsc=abc123' \
    -H 'rsc: 1' -H 'next-router-prefetch: 1' \
    -H 'next-router-segment-prefetch: /_tree' -H 'next-url: /'

age: 231275
cache-control: public, max-age=0, must-revalidate
content-type: text/x-component
x-nextjs-prerender: 1
x-nextjs-stale-time: 300
x-vercel-cache: HIT
Enter fullscreen mode Exit fullscreen mode

A prerendered payload, two and a half days old, served from the CDN. Thirty-seven of those a page view is bandwidth and nothing else.

Two of our pages cannot do that. The home page and the pricing page read a request header, because the price is shown in the visitor's own currency, so both are dynamic. Prefetch them and you get:

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

And the body is 299 bytes of nothing:

0:{"f":[[["",{"children":["(marketing)",{"children":["pricing",{"children":
["__PAGE__",{},"$undefined","$undefined",4096]},...]}]],null,[null,null],true]],
"q":"","i":false,"S":false,"h":null,"r":"$undefined"}
Enter fullscreen mode Exit fullscreen mode

That is the documented behaviour: a dynamic route is not prefetched unless it has a loading.js, so the browser is told the shape of the tree and no content. The request still went to the origin, still missed the cache, and still returned a header saying do not store this.

Both of those routes are in the footer of all 106 pages. The logo links home and the Product column links to pricing. So the two pages on the site that cannot be served from the edge are also the two that every single page view asks the origin for, without anybody clicking anything.

Is any of it worth it

Prefetching is a bet: spend bytes now so that one navigation later is instant. The bet is good on an application where people move between a handful of screens all day. Our 106 public pages are mostly reached from search, read once, and left. Most visits click zero of those 39 routes, and the ones that do click usually click a link inside the text, not in the footer.

So the honest reading of 396 KB is that nearly all of it is waste, and the right fix is not a global switch. It is this distinction:

  • Links inside the writing are the reader following an argument. Those are worth prefetching, and there are a handful per page.
  • The footer is a sitemap. It exists so that crawlers and the occasional determined human can reach everything. prefetch={false} belongs there.

The second fix is smaller and more annoying: a page that cannot be cached should not be in the chrome of every page. Either the pricing page stops reading the country header on the server (it would need the currency to arrive another way), or the footer link to it opts out of prefetch, so the only server render of the pricing page is one somebody asked for.

Neither is shipped yet. Both are now written down with the measurement next to them, which in my experience is the only form of a performance note that ever gets acted on.

See it

Open the guide I measured with DevTools on the Network tab, filter for _rsc, tick Disable cache, and scroll slowly to the bottom. The requests arrive column by column. Then click one footer link and watch the navigation complete with no request at all, because that one was already paid for.

If you want the other half of the story, our pricing page is one of the two that answer x-vercel-cache: MISS to a prefetch, and the tools hub is the page with the most prefetching above the fold: 25 requests before you scroll at all, because its five calculator cards are links.

Top comments (0)