Skip to content

Instantly share code, notes, and snippets.

@Jobians
Last active January 31, 2026 00:36
Show Gist options
  • Select an option

  • Save Jobians/018eb2b708c91a6c5e4c7ed6b2c930ff to your computer and use it in GitHub Desktop.

Select an option

Save Jobians/018eb2b708c91a6c5e4c7ed6b2c930ff to your computer and use it in GitHub Desktop.
Fix next-themes Flash in Vite React (Dark Mode)

Vite React next-themes Dark Mode Fix

Minimal <head> snippet to prevent the flash of white background (FOUC) in Vite React SPAs using next-themes (chakra ui).

<head>
    <meta charset="UTF-8" />
    <link rel="icon" type="image/svg+xml" href="/vite.svg" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>App</title>
    <script>
      (function () {
        const theme = localStorage.getItem('theme');
        const prefersDark = window.matchMedia(
          '(prefers-color-scheme: dark)'
        ).matches;
        if (theme === 'dark' || (!theme && prefersDark)) {
          document.documentElement.classList.add('dark');
        }
      })();
    </script>
</head>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment