Skip to content

Fix color flash on page load when setting dark/light mode #2110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 25, 2025

Conversation

Gazler
Copy link
Contributor

@Gazler Gazler commented Feb 24, 2025

Fixes #2112

Previously, when loading the landing page, the page colors would flash briefly. For light mode the background flashes black before the white background loads.

For dark mode, the header would flash white, and the text black, before the correct colors render.

This was happening because the script was being executed after the page had finished loading, likely due to how the page is packaged into chunks.

To fix this, the dark mode script is now set inline, so will be part of the generated HTML.

This fixes #2105

Here's a video showing what was happening before.

tailwindv4.mp4

Previously, when loading the landing page, the page colors would flash
briefly. For light mode the background flashes black before the white
background loads.

For dark mode, the header would flash white, and the text black, before
the correct colors render.

This was happening because the script was being executed after the page
had finished loading, likely due to how the page is packaged into
chunks.

To fix this, the dark mode script is now set inline, so will be part of
the generated HTML.

This fixes tailwindlabs#2105
Copy link

vercel bot commented Feb 24, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
tailwindcss-com ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 25, 2025 10:22am

@philipp-spiess
Copy link
Member

Wow, thanks for looking into it. I switched to a <Script /> tag because we found <script> is not executed for 404 pages in the docs subdirectory (and that's still happening, try to go to http://localhost:3000/404 and switch the theme). So we'll probably need both hooks and make sure the code itself is idempotent... 🙃

@philipp-spiess
Copy link
Member

Thank you!

@philipp-spiess philipp-spiess merged commit e8a38eb into tailwindlabs:main Feb 25, 2025
2 checks passed
@Gazler
Copy link
Contributor Author

Gazler commented Feb 25, 2025

FWIW, it looks like that issue you are seeing only happens locally? Perhaps it's Next.js taking over the error page?

https://tailwindcss-com-git-fork-gazler-fix-dark-mo-afe988-tailwindlabs.vercel.app/404 shows correctly for me.

@philipp-spiess
Copy link
Member

@Gazler Yeah on prod /404 is special, try /not-found. The Vercel preview is now fixed too after I pushed a fix though 😬

Here you see the issue: https://tailwindcss-8uo84vawl-tailwindlabs.vercel.app/not-found

@Gazler
Copy link
Contributor Author

Gazler commented Feb 25, 2025

I see. In that case, the issue still exists on the 404 page. https://tailwindcss.com/not-found

Definitely much less of an issue than it happening on every other page though!

@philipp-spiess
Copy link
Member

You mean the flash? Yeah I don't know how else we can fix it here since synchronous scripts just don't evaluate. But I think this is reasonable too to have this issue on not-found pages. There aren't accessed too often thankfully 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Documentation initial Flickering Theme State on refresh #16789 website CSS issue
2 participants