Skip to main content
← Назад до блогу
next.jsreact-19dark-modezustandperformance

Dark Mode в Next.js без мигання та попереджень React 19

Як замінити next-themes на Zustand + useServerInsertedHTML для темної теми без мигання в Next.js 15+ без попереджень React 19.

Опубліковано 11 квітня 2026 р.8 хв читання

TL;DR

next-themes рендерить <script> тег всередині React Client Component для запобігання мигання теми (FOUC). React 19 тепер попереджає про це — і немає способу це придушити. Бібліотека не оновлювалась з березня 2025. Рішення: замінити next-themes на Zustand стор + useServerInsertedHTML для інжекту скрипта поза React деревом. Нуль нових залежностей. Нуль мигання. Нуль попереджень.

Проблема

Якщо ви використовуєте next-themes з Next.js 15+ і React 19, ви отримуєте цю помилку в консолі на кожному завантаженні сторінки:

Encountered a script tag while rendering React component.
Scripts inside React components are never executed when rendering on the client.
Consider using template tag instead.

src/providers/theme-provider.tsx (7:10) @ ThemeProvider

Це не hydration mismatch. Це React 19 явно попереджає, що <script> теги, відрендерені React компонентами на клієнті, ніколи не виконуються. Скрипт працює під час SSR (він є в HTML), але React позначає це як некоректне.

Чому це відбувається

next-themes повинен встановити правильний клас теми на <html> до гідрації React — інакше буде мигання невірної теми. Для цього він інжектить inline <script> через React.createElement:

next-themes internals (minified)
// next-themes renders a <script> inside a Client Component
return React.createElement(Provider, { value },
  React.createElement("script", {
    suppressHydrationWarning: true,
    dangerouslySetInnerHTML: { __html: `(...theme init code...)` }
  }),
  children
)

React 19 змінив поведінку: script теги всередині компонентів тепер явно позначаються. До React 19 це мовчки ігнорувалось. Проп suppressHydrationWarning на скрипті не допомагає — він придушує попередження гідрації, а не попередження "script в компоненті".

Що ми пробували (і чому не спрацювало)

Ми систематично перепробували всі підходи перед знаходженням рішення:

AttemptResult
suppressHydrationWarning on <html>Suppresses hydration mismatch, but NOT the script tag warning
Delay mount with useState + useEffectWarning disappears, but causes FOUC (theme flash)
Raw <script> in layout <head>React 19 catches it — same warning even in Server Components
next/script with beforeInteractiveStill rendered inside React tree — same warning
Remove <head>, put Script in bodySame warning — it's inside <html> which is React-managed
next-themes@1.0.0-beta.0Beta, no release date, unclear if fixed

Рішення: Zustand + useServerInsertedHTML

Ключовий інсайт: useServerInsertedHTML — це хук Next.js, який інжектить HTML в SSR потік поза React деревом компонентів. Скрипт потрапляє в HTML, але React ніколи не "бачить" його під час клієнтського рендерингу — тому немає попередження. У поєднанні з Zustand стором для реактивного стану теми, ми отримуємо повну заміну без залежностей.

Як це працює

SSR (server):
  useServerInsertedHTML → injects <script> into HTML stream
  ↓
  Browser receives HTML with theme script already in it
  ↓
  Script runs BEFORE React hydrates → correct class on <html>
  ↓
  No flash. No mismatch. No warning.

Client (after hydration):
  useEffect → _init() → Zustand store syncs with localStorage
  ↓
  useTheme() → reactive theme state for components

Крок 1: Zustand стор

Стор керує станом теми, застосовує класи до DOM, обробляє системну тему та синхронізується між вкладками. Метод _init() повертає функцію очищення для використання в useEffect:

store/use-theme-store.ts
import { create } from "zustand";

const STORAGE_KEY = "theme";
const MEDIA_QUERY = "(prefers-color-scheme: dark)";

function getSystemTheme(): "light" | "dark" {
  return window.matchMedia(MEDIA_QUERY).matches ? "dark" : "light";
}

function applyTheme(resolved: string, disableTransition: boolean) {
  const d = document.documentElement;

  if (disableTransition) {
    const style = document.createElement("style");
    style.appendChild(
      document.createTextNode(
        "*,*::before,*::after{transition:none!important}"
      )
    );
    document.head.appendChild(style);
    window.getComputedStyle(document.body);
    setTimeout(() => document.head.removeChild(style), 1);
  }

  d.classList.remove("light", "dark");
  d.classList.add(resolved);
  d.style.colorScheme = resolved;
}

interface ThemeState {
  theme: string;
  systemTheme: "light" | "dark";
  resolvedTheme: string;
  setTheme: (theme: string) => void;
  _init: (disableTransition: boolean) => () => void;
}

export const useThemeStore = create<ThemeState>((set, get) => ({
  theme: "system",
  systemTheme: "light",
  resolvedTheme: "light",

  setTheme: (newTheme) => {
    const { systemTheme, disableTransitionOnChange } = get();
    const resolved = newTheme === "system" ? systemTheme : newTheme;
    localStorage.setItem(STORAGE_KEY, newTheme);
    applyTheme(resolved, disableTransitionOnChange);
    set({ theme: newTheme, resolvedTheme: resolved });
  },

  _init: (disableTransition) => {
    const stored = localStorage.getItem(STORAGE_KEY) || "system";
    const system = getSystemTheme();
    const resolved = stored === "system" ? system : stored;

    set({ theme: stored, systemTheme: system, resolvedTheme: resolved });
    applyTheme(resolved, disableTransition);

    // System theme changes
    const mq = window.matchMedia(MEDIA_QUERY);
    const onChange = (e: MediaQueryListEvent) => {
      const newSystem = e.matches ? "dark" : "light";
      const { theme } = get();
      set({ systemTheme: newSystem,
            resolvedTheme: theme === "system" ? newSystem : theme });
      if (theme === "system") applyTheme(newSystem, disableTransition);
    };
    mq.addEventListener("change", onChange);

    // Cross-tab sync
    const onStorage = (e: StorageEvent) => {
      if (e.key !== STORAGE_KEY || !e.newValue) return;
      const system = getSystemTheme();
      const resolved = e.newValue === "system" ? system : e.newValue;
      set({ theme: e.newValue, resolvedTheme: resolved });
      applyTheme(resolved, disableTransition);
    };
    window.addEventListener("storage", onStorage);

    return () => {
      mq.removeEventListener("change", onChange);
      window.removeEventListener("storage", onStorage);
    };
  },
}));

export function useTheme() {
  return useThemeStore((s) => ({
    theme: s.theme,
    setTheme: s.setTheme,
    resolvedTheme: s.resolvedTheme,
    systemTheme: s.systemTheme,
  }));
}

Крок 2: ThemeProvider

Провайдер робить дві речі: інжектить скрипт запобігання FOUC через useServerInsertedHTML та ініціалізує Zustand стор при монтуванні:

providers/theme-provider.tsx
"use client"

import { useEffect } from "react"
import { useServerInsertedHTML } from "next/navigation"
import { useThemeStore } from "@/store/use-theme-store"

const THEME_INIT_SCRIPT = `(function(){
  try {
    var t = localStorage.getItem("theme") || "system";
    var r = t;
    if (t === "system") {
      r = window.matchMedia("(prefers-color-scheme: dark)").matches
        ? "dark" : "light";
    }
    document.documentElement.classList.remove("light", "dark");
    document.documentElement.classList.add(r);
    document.documentElement.style.colorScheme = r;
  } catch(e) {}
})()`

export function ThemeProvider({
  children,
  disableTransitionOnChange = false,
}) {
  // Injects script into SSR HTML outside the React tree
  // — prevents FOUC without triggering React 19 warning
  useServerInsertedHTML(() => (
    <script dangerouslySetInnerHTML={{ __html: THEME_INIT_SCRIPT }} />
  ))

  useEffect(() => {
    return useThemeStore.getState()._init(disableTransitionOnChange)
  }, [disableTransitionOnChange])

  return <>{children}</>
}

Крок 3: Layout

app/layout.tsx
import { ThemeProvider } from "@/providers/theme-provider"

export default function RootLayout({ children }) {
  return (
    <html lang="en" suppressHydrationWarning>
      <body>
        <ThemeProvider disableTransitionOnChange>
          {children}
        </ThemeProvider>
      </body>
    </html>
  )
}

Крок 4: Використання

components/theme-switch.tsx
import { useTheme } from "@/store/use-theme-store"

export function ThemeSwitch() {
  const { theme, setTheme } = useTheme();

  return (
    <button onClick={() => setTheme(
      theme === "dark" ? "light" : "dark"
    )}>
      {theme === "dark" ? "☀️" : "🌙"}
    </button>
  );
}

Міграція з next-themes

API навмисно ідентичний. Міграція — це одна зміна імпорту на файл:

migration
- import { useTheme } from "next-themes"
+ import { useTheme } from "@/store/use-theme-store"

  // API is identical — no other changes needed
  const { theme, setTheme, resolvedTheme, systemTheme } = useTheme()

Порівняння

next-themesZustand + useServerInsertedHTML
React 19 warningYes — <script> in client componentNo
FOUC preventionYesYes
Cross-tab syncYesYes
System theme detectionYesYes
disableTransitionOnChangeYesYes
Bundle size~3.5 KB~1.5 KB (uses existing Zustand)
Dependencies+1 (next-themes)0 (Zustand already in project)
Maintenance riskAbandoned since March 2025Your own code

Чому не інші альтернативи?

@wrksz/themes

Drop-in заміна, яка також використовує useServerInsertedHTML. Працює, але це ще одна залежність від одного мейнтейнера. Якщо next-themes нас чомусь навчив — залежності закидають. З ~100 рядками коду ви можете повністю володіти рішенням.

next-themes@1.0.0-beta.0

Існує на npm, але без дати релізу, без changelog і без чіткої вказівки, що попередження React 19 виправлено. Ставити продакшн код на безстрокову бету — це ризик, який не варто брати.

CSS-only (prefers-color-scheme)

Працює для детекції системної теми, але не може обробити збереження вибору користувача (localStorage), ручне перемикання теми або опцію "system". Для цього потрібен JavaScript.

Висновки

  1. next-themes фактично закинуто — останній реліз березень 2025, попередження React 19 не виправлено
  2. useServerInsertedHTML — правильний примітив Next.js для інжекту скриптів без попереджень React
  3. Zustand забезпечує реактивний стан теми з меншим кодом, ніж Context провайдер
  4. Все рішення — ~100 рядків, нуль нових залежностей, і ви володієте кожним рядком

Джерела