Dhruv Kumar

My homepage broke an hour after every deploy

For five days, anyone who had chosen dark on this site got a light homepage. The page arrived dark, held for a moment, and switched itself to light. Every other page stayed the way it was asked to be.

Nothing I run could have told me. The build passed. The page was correct when it deployed, and it stayed correct for an hour.

It showed up when I had Claude Code drive a headless browser through the live site page by page, looking for console errors. The homepage logged this, on every load:

Error: Minified React error #418

Unminified: “Hydration failed because the server rendered HTML didn’t match the client. As a result this tree will be regenerated on the client.”

Regenerated on the client

Those last words are where the theme went. The theme here is a data-theme attribute on <html>, set by a small inline script before the first paint. When React attaches itself to the server’s HTML, it leaves that attribute alone. When it gives up on that HTML and renders the document itself, it first removes every attribute <html> has, then sets the ones it was given. It was never given that one.

<html lang="en" class="…" data-js data-theme="dark">

The mismatch was in the layout, above any Suspense boundary, so “this tree” was the whole document.

Not on my machine

next dev drew the homepage without a complaint. The copy my last local build had left on disk was right too. Only the live page was wrong, and it was wrong every time.

So I stopped reading the code and compared two documents: the HTML the server sent, and the page as it stood once React had redrawn it. They differed in the first line.

Every page on this site opens with a top line. On the homepage it says where I am and what time it is there. On every other page it is my name, and the name links home. The server had drawn my homepage with every other page’s top line.

/index

The top line chooses between the two like this:

const pathname = usePathname();
const isHome = pathname === "/";

So the server had been given some other address. A Next.js page carries its own data inside its HTML, and in that data is the path it was rendered at, split at the slashes. From my local build, then from the live site:

\"c\":[\"\",\"\"]        at /
\"c\":[\"\",\"index\"]   at /index

My homepage isn’t built just once. It shows my GitHub calendar, so Next regenerates it in the background, at most once an hour. The build makes the first copy, at /. Vercel makes every copy after that, and makes it at /index. On the server, usePathname() now said /index. In the browser it said /, as it always had.

For an hour after a deploy the build’s copy is served and nothing is wrong. Then the first regeneration replaces it, and the page stays broken until the next deploy. And the hour after a deploy is when I look.

It has a number

I was not the first. It is vercel/next.js #95648, open since July, and the thread reads like my evening: found in production, and not reproduced until the reporter let the page regenerate. A maintainer confirmed it the day after it was filed, and a fix went up for review on 20 July: #95973. As I write this it hasn’t merged, so on Vercel a regenerated homepage still renders at /index.

The fix

Asking for the address was my mistake before it was anyone’s bug. Whether a page is the homepage is a fact about the route, and a route is the same wherever it is rendered. The homepage is the page with no segments under the root layout:

export function useIsHome(): boolean {
  return useSelectedLayoutSegments().length === 0;
}

The second change is for the next mismatch, because there will be one. A browser extension that rewrites a page before React reads it can cause the same regeneration, and those are not mine to fix. So the theme no longer depends on React never starting over. If data-theme is missing when the theme switch mounts, a layout effect puts it back before the browser paints.

Still at /index

The fix went out late on 27 November. An hour later Vercel regenerated the homepage, and I asked it where it had been rendered. You can ask it as well:

curl -sL -H Accept:text/html dhruvkumar.dev |
grep -o '\\"c\\":\[[^]]*\]'\"c\":[\"\",\"index\"]   that night 

When I ran it that night it said index, as it had while the page was broken. The server was still rendering my homepage at an address I never gave it. The page had stopped caring: it draws the same first line at either one.

It is the issue’s command with two additions: -L, to follow the redirect to www, and -H, because this site answers a bare curl with my résumé. It works on any App Router site. If your homepage regenerates in the background on Vercel, it is worth ten seconds.

What a build can’t show

Nothing in my build was wrong. No test I could have run against it would have failed, because the page a build makes is the correct one. Looking at the live site that evening turned up two more of the same kind.

Every page but the homepage and one case study was downloading the pixel font my name is set in, 28 KB of it, and using it for nothing: the font’s preload hint rode along in the homepage’s data, which the other pages fetch ahead of a click back home. And the uptime checks this site says run about every hour had been running about every nine hours, because GitHub starts a scheduled workflow when it has room. It ran 14 times between 22 and 27 November.

All three are fixed. None of them has a test, and I don’t yet know what one would look like: each only exists on the live site, where no build looks.