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#358groveom wants to merge 1 commit intopacocoursey:mainfrom groveom:main
groveom wants to merge 1 commit intopacocoursey:mainfrom
groveom:main
Conversation
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
<head>inpublic/index.htmlfor Vite, CRA, and other SPA setups.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.