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
suppressHydrationWarningto<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 andenableSystemto followprefers-color-scheme; - optionally
disableTransitionOnChangeto 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;
suppressHydrationWarningon<html>; delaying theme-toggle UI until mounted;attribute="class"for class-based styling such as Tailwind;enableSystemfollowsprefers-color-scheme(default true);disableTransitionOnChangeaffects 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 darkon:rootmakes 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