1,646 links on my site were reloading the whole app
A plain <a> to your own page reloads the whole Next.js app, and on this site that stops the radio. 1,646 links did it. How five pull requests found and fixed them: a lint rule that had never read a .jsx file, three codemods, and SiteLink, a component that decides at render time from rules generated off the routes themselves.
This site has a radio. It plays the site's own narration, the audiobook and the spoken essays, and its player lives in the root layout so it can keep playing while you read your way around. That only works if moving between pages never throws the layout away, and until this week 1,646 links on the site did exactly that. They were plain <a href> tags pointing at pages in the same Next.js app. Every one of them turned a click into a full page load, and a full page load stops the radio.
Fixing them took five pull requests. Most of that was mechanical. The part worth writing up is the last 722 links, the ones no tool could fix ahead of time, because whether they point at a page is only known when the page renders.
- PR 1Lint the .jsx files0
The link rule had never run on a component. It reported 271 links.
- PR 2Convert what the rule saw+271
A codemod renamed only the flagged elements. The rule became an error.
- PR 3Convert what it missed+653
246 built at runtime, 407 literals it never flagged, one back link on 94 pages, two dead links.
- PR 4SiteLink+722
Hrefs that come from data, decided while rendering against generated rules.
- PR 5Fix what CI caught0
Rules read from git, not the disk. Press links linted against Press routes.
What one click costs
A plain anchor to your own page tells the browser to start over. It requests a new HTML document, discards the running JavaScript and everything it held, runs every script again, and React builds the tree from nothing. Scroll position goes, a half-filled form goes, and anything mounted in the layout goes with it. On most sites the cost is a flash and a few hundred milliseconds. On a site with audio in the layout, it decides whether the radio works at all.
next/link renders the same <a> element, so it still behaves like a link: you can copy it, open it in a new tab, or follow it with JavaScript off. A plain left click is intercepted instead. The router fetches only what the new route needs, swaps the page segment, and leaves every layout above it mounted. In production it also prefetches links as they scroll into view, so the click usually has nothing left to wait for. The box below lets you click both kinds.
Click a page above. Then switch how the links are built and click again.
Drawn, not heard: no audio plays here.
The rule was on and had never read a component
Next ships an ESLint rule for this exact mistake, @next/next/no-html-link-for-pages, and the config had it on. It had never seen a component. ESLint 9's flat config lints .js, .mjs and .cjs files unless some block names more extensions, and nothing named .jsx, which is where every component in this repo lives. Since the move to flat config, lint had been passing because it was reading the wrong files.
Naming the extension in one files glob was the first pull request. The link rule then reported 271 links. I set it to warn for a few hours so the rest of the codebase could keep merging while the links got fixed.
Literal links, by codemod
The second pull request converted those 271 with a codemod built on @babel/parser. It renamed only the elements ESLint had flagged (both the opening and the closing tag), added one import Link from "next/link" per file, and kept every className, style, aria-* and rel exactly as it was. It would have skipped any link with target or download; none came up. Then the rule went back to being an error, so a new internal <a> fails CI.
Checking that a link really navigates client-side needs one line in the browser console, set before the click:
window.__stillHere = true;
// click the link, then once the address bar changes:
window.__stillHere; // true after a client navigation, undefined after a full loadWhat a lint rule cannot see
A client navigation keeps the window object and a full load hands you a new one, so the marker answers the question without any tooling. I used it on every batch after this one, too, because the rule had missed far more than it caught.
It only reads literal hrefs, and it missed plenty of those as well. The third pull request went after the groups it never reported: 246 links built at runtime, like href={`/reading/${slug}`}; 407 literal links it never flagged, many of them on the Press sites, whose routes resolve per host under one dynamic segment the rule cannot read; and the back link in SubpageNav, a shared header on 94 pages.
Two more codemods handled the first two groups, and they were stricter than the first. A link converted only when every value its href could take, through template literals, string concatenation or a ternary, matched a page.jsx route in its own app and none of the things that only look like pages (route handlers, static files, API routes, rewrites, redirects to another host). That strictness turned up two dead links: a New button pointing at /os/new and a /train link on the house dashboard. Neither page existed. One now opens the create page that does, and the other is gone.
SiteLink: ask when the href is a string
That left 1,022 links whose href came from data: item.href, post.url, a permalink out of a catalog. Many were internal and many were not. No reading of the source can tell which, because the answer is in the data, and the data arrives at render.
So the fourth pull request moved the question there. SiteLink takes the same props as an <a>. When it renders it has the actual string, and it asks one pure function whether that string is a page in its own app. A page gets next/link. Anything else gets the same <a> it was before, props in the same order. One factory builds it twice, once for this site and once for the Press app, so each checks hrefs against its own routes.
export function makeSiteLink(app) {
function SiteLink(props) {
const { query = false, prefetch, replace, scroll, ...anchor } = props;
const plain = anchor.target !== undefined || anchor.download !== undefined
|| !isClientNavigable(anchor.href, { app, query });
if (plain) return <a {...anchor} />;
return <Link {...anchor} prefetch={prefetch} replace={replace} scroll={scroll} />;
}
return SiteLink;
}The rules come from the routes
The function is only as good as its list of things that are never pages. That list is the part I did not want to keep by hand. A script reads it from the code that defines it: every route handler in both apps, every top-level folder under public/, the rewrites in next.config.mjs and the redirects that leave the host. It writes a small data module, and a test re-derives the rules and fails while the committed file is stale. I planted a route handler to check the test, and it failed and named the route.
A few answers no filesystem would give, so the script holds those by hand, with a reason on each line. My favorite is /dl, the download counter. It is a route handler that counts a download and redirects to the file, and a prefetch would count a download nobody asked for.
722 links went through SiteLink, close to half of them on the Press sites. The other 300 open in a new tab, download a file, or sit in files whose tests pin their imports, and they stay plain on purpose. The box below runs the same function over the same generated rules.
A route handler, a rewrite to another project, or a redirect to another host. There is no page here to navigate to.
Generated from 62 route handlers, 25 folders that are never pages, 8 rewrites and 21 redirects to another host.
If the source cannot tell you whether an href is a page, ask when the href is a string.
The same-page query trap
One kind of link looks like a page and is not safe to treat as one: a link to the page you are already on, with a different query string. Under a full load the page starts over and reads ?q=MIT+Press as if it were new. Under a client navigation the page stays mounted, and a page that reads its query once, when it mounts, never sees the change. The address bar updates and nothing else does.
Three places did this here: the publisher and author filters on the reading lists, the force-night link on the Tashkent airport map, and the Lightning voice tutor's way back to its home tab. The first two now handle a plain left click themselves. They filter or switch the sky in place and write the query with replaceState, so the link stays shareable and still works with JavaScript off. The tutor calls the app's own navigate function.
SiteLink keeps any href with a query string as a plain <a> unless the caller passes query, which is a promise that the page follows its query on every navigation. Nothing checks that promise. I found all three by clicking.
What CI caught after the merge
The fourth pull request merged and main went red twice. The drift test passed on my machine and failed in CI, because the script listed public/ from the disk. CI's test shards use a sparse checkout that skips audio, so a folder holding only narration was missing there. The Press app's build copies are gitignored, so they existed only on my disk. The fifth pull request reads the folders from git ls-files, which gives the same list in both places, and names the four build copies in the hand-kept list.
The second failure was older and was mine too. Six Press links I had left alone were real Press pages, and the Press app's own build, which runs lint, rejected them. My codemods had judged Press files against the main site's routes, where /lightning redirects to another host and /play and /brand are folders of static files. The root lint had the same blind spot. One block in the ESLint config, settings: { next: { rootDir: "press" } } for files under press/src, makes the rule read Press routes there. Before the fix it reported exactly those six, and after it, none.
When I would not do this
If your app has a few dozen links, write <Link> and skip all of it. The machinery pays off here because the site is two apps with 92 route handlers between them, and most of its links come out of content catalogs rather than markup.
It also costs something. The rules ship to the browser with every page that uses SiteLink: 6,148 bytes, 1,770 gzipped. The generated file is one more thing that can drift, held only by the test that re-derives it. Prefetching has a price too. The full reading list renders about 250 book links at once, so those are prefetch={false} instead of fetching book pages nobody opened. And the query trap is invisible to every check in this post. The only test for it is to click the link and see whether anything happened.
Get the next one
An occasional note when something genuinely new ships here — essays, free tools, projects. No schedule, no filler, easy out.
Hiring for an AI implementation role?
I make software work inside real organizations, and now that means AI systems a team can check: the buying, the guardrails, the training and the build. If you are filling a role like that, twenty minutes is the quickest way to see whether we fit.
