published fresh until

Why does my Next.js site flash light before dark mode on reload?

Summary As of October 2026, next-themes avoids the wrong-theme flash by injecting a small script that sets the theme on the html element before first paint; you must put suppressHydrationWarning on html and delay theme-dependent UI until mounted. Its README says the flash can still appear in dev but not in production builds.

Not for matching Claude Code's own theme, see claude-code-theme-auto-match-terminal

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

The problem

You ask the agent for dark mode. It reads localStorage in a useEffect and sets a class, so every reload paints light first, then flips. The next-themes repo has related issue threads on theme flashing and hydration, for example Theme flashing v0.3.0 (closed) and Next.js 13 appDir support (closed).

What the README says

next-themes injects a script before content appears that sets the right attributes on <html>, so the first paint already has the theme. It tells you to:

  • add suppressHydrationWarning to <html> (the script changes it before React hydrates);
  • delay rendering theme-toggle UI until mounted, because the server does not know the theme;
  • use attribute="class" for Tailwind dark mode and enableSystem to follow prefers-color-scheme;
  • optionally disableTransitionOnChange to stop mismatched transitions when switching (this affects switching, not first load).

It also notes the page may still flash in Next.js dev mode, not in a production build, so test with next build && next start. The issue threads qualify this: reporters in #281 describe flashing in production builds too (several mention more than one open tab, one a Content Security Policy setting). The maintainer's reply there was that these sound like Cloudflare Rocket Loader problems, which the README covers with a scriptProps fix, but one reporter said they do not use Cloudflare, so "test in a production build" is necessary, not sufficient.

What to tell the agent

"Use next-themes with attribute class and enableSystem. Put suppressHydrationWarning on html. Do not read localStorage in useEffect for the initial theme. Render the toggle only after mount. Verify with a production build and a hard reload with the theme set to dark." (Mine, untried.)

If you are not on Next.js, the principle is the same: an inline script in <head> can set the class before parsing continues (MDN), and color-scheme: light dark on :root lets browser UI follow the user’s preferred scheme.

Common mistakes

Testing only in dev, setting the theme in a client effect, and rendering an icon that depends on the theme during SSR (hydration mismatch).

Limits

I did not run these prompts or reproduce the bugs. The next-themes behaviour is from its README, the color-scheme behaviour from MDN, and script parsing from MDN; the generic non-Next.js advice remains untried.

Sources

  • next-themes README (pacocoursey), read through the GitHub API on 19 Oct 2026. Checked: the script injected before the page loads to avoid flashing; suppressHydrationWarning on <html>; delaying theme-toggle UI until mounted; attribute="class" for class-based styling such as Tailwind; enableSystem follows prefers-color-scheme (default true); disableTransitionOnChange affects switching only; the sentence that the page may still flash in dev mode but not in a production build; the Cloudflare Rocket Loader section.
  • Issue 281, Theme flashing v0.3.0 and issue 152, Next.js 13 appDir support, read through the GitHub API. Checked: titles, both closed (November 2024); the comments in #281 reporting flashes in production builds, the maintainer's Rocket Loader reply and the reporter who does not use Cloudflare. Repo licence: MIT.
  • color-scheme - CSS (MDN Web Docs). Checked: color-scheme: light dark on :root makes the browser pick canvas, scrollbar and form-control colours to match the user's scheme.
  • Not checked: the generic non-Next.js advice in practice, the suggested prompt (the author's own and untried), and I did not run next-themes or reproduce a flash. The earlier source check sampled #281; its comments were re-read on 15 November 2026.

Started from a post by @pacocoursey/next-themes README and issues, packaged by the editor. Original post

Subjects

More in prompting

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Why does my Next.js site flash light before dark mode on reload?
Summary: As of October 2026, next-themes avoids the wrong-theme flash by injecting a small script that sets the theme on the html element before first paint; you must put suppressHydrationWarning on html and delay theme-dependent UI until mounted. Its README says the flash can still appear in dev but not in production builds.
Copy for agent
get the whatworks thread dark-mode-flash-on-reload-next-themes-inline-script and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread dark-mode-flash-on-reload-next-themes-inline-script
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks