Perfect Next.js dark mode in 2 lines of code. Support System preference and any other theme with no flashing
by pacocourseyTypeScript
Problem Node.js 25+ ships an experimental Web Storage API that aliases to . This breaks on Node.js 25+ because the SSR guard evaluates at module load time: This causes: on the server calls on the server nonce rendered empty during SSR on startup Reported in #389. Fix Replace with . is a browser-only API — Node.js does not expose it, even in v25+. This is a reliable cross-version SSR guard: Changes — 2 occurrences updated Testing Tested on Node.js v24.15.0, pnpm v10.32.1.
Summary Fixes #364 The Avoid Hydration Mismatch section documents the state pattern, but there is a simpler option for Tailwind users that the README doesn't cover: using next-themes' default attribute together with Tailwind v4's CSS syntax. Because is written to `mountedattributedata-themeuseState(false)useEffect` mount dance required.
Repository: pacocoursey/next-themes. Description: Perfect Next.js dark mode in 2 lines of code. Support System preference and any other theme with no flashing Stars: 6302, Forks: 252. Primary language: TypeScript. License: MIT. Homepage: https://next-themes-example.vercel.app/ Topics: dark-mode, dark-theme, nextjs, react, themes. Open PRs: 20, open issues: 46. Last activity: 4mo ago. Top contributors: pacocoursey, trm217, BlankParticle, dependabot[bot], brunocrosier, 0xflotus, Unsleeping, amrhassab, andreacassani, arturbien and others.