Skip to content

docs: add instructions to prevent white flash for SPAs#358

Closed
groveom wants to merge 1 commit intopacocoursey:mainfrom
groveom:main
Closed

docs: add instructions to prevent white flash for SPAs#358
groveom wants to merge 1 commit intopacocoursey:mainfrom
groveom:main

Conversation

@groveom
Copy link
Contributor

@groveom groveom commented May 22, 2025

Summary

This pull request updates the README to help users of next-themes in SPA environments (such as Vite, CRA, or any non-Next.js React app) prevent a white flash (FOUC) on reload. It documents how to add an inline script that applies the correct theme class before the app loads, ensuring a seamless dark/light mode experience.

Changes

  • Added a new section in the README titled "Prevent white flash (SPAs only)".
  • Provided an inline script to be placed at the very top of the <head> in public/index.html for Vite, CRA, and other SPA setups.
  • Included an explanation about the necessity of this approach and how it resolves FOUC.

Reason for Change

While next-themes handles FOUC automatically in Next.js via server-side script injection, SPA frameworks do not include this by default. This omission can cause a white flash before the user's preferred theme is applied, impacting user experience. Documenting this script helps SPA users implement a reliable, recommended fix.

Additional Notes

This is a documentation-only change. The provided solution is widely used in the SPA community and ensures the correct theme is applied on the first paint, eliminating FOUC. No breaking changes are introduced; this update improves the onboarding and experience for SPA users of next-themes.

@pacocoursey
Copy link
Owner

Hmm ideally we can just fix this in the library itself. You are basically duplicating a (worse) version of the script that is used internally for recommendations only for SPAs.

I think with a React 19 release we could have this supported in any React app with their new script tag support.

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.

2 participants