Skip to content

fix: resolve dark mode white flash on initial page load#7446

Open
AnkitRewar11 wants to merge 9 commits intolayer5io:masterfrom
AnkitRewar11:fix/dark-mode-flicker
Open

fix: resolve dark mode white flash on initial page load#7446
AnkitRewar11 wants to merge 9 commits intolayer5io:masterfrom
AnkitRewar11:fix/dark-mode-flicker

Conversation

@AnkitRewar11
Copy link

Description

This PR fixes #7443

Fixes the white background flash (FOUC) that appears
briefly before dark theme loads on initial page visit.

Changes Made

  • Added setHtmlAttributes to set dark background at HTML level during SSR before any JS runs
  • Added visibility hidden/visible to prevent white flash while theme variables are being applied
  • Fixed JSON parsing bug — removed single quote wrapping around JSON.stringify
  • Fixed theme keys to use ThemeSetting values instead of hardcoded "light"/"dark"
  • Added fallback background color in fonts.css

Notes for Reviewers

Tested on initial page load — white flash no longer appears before dark theme loads.

Signed commits

  • Yes, I signed my commits.

Refactor theme handling to use ThemeSetting values as keys and improve theme application logic.

Signed-off-by: Ankit Rewar <171806101+AnkitRewar11@users.noreply.github.com>
Signed-off-by: Ankit Rewar <171806101+AnkitRewar11@users.noreply.github.com>
@YASHMAHAKAL
Copy link

@AnkitRewar11 Build is failing :(

onRenderBody.js Outdated
export const onRenderBody = ( { setPreBodyComponents }) => {
export const onRenderBody = ({ setPreBodyComponents, setHtmlAttributes }) => {
// FIX 4: Set dark background at HTML level during SSR — before any JS runs
setHtmlAttributes({ style: "background-color: #000;" });
Copy link

@YASHMAHAKAL YASHMAHAKAL Mar 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AnkitRewar11 Change to setHtmlAttributes({ style: { backgroundColor: "#000" } });
This can be root cause of build failure : )

Signed-off-by: Ankit Rewar <171806101+AnkitRewar11@users.noreply.github.com>
@AnkitRewar11 AnkitRewar11 requested a review from YASHMAHAKAL March 2, 2026 22:01
@l5io
Copy link
Contributor

l5io commented Mar 2, 2026

🚀 Preview for commit 16be0d1 at: https://69a60abf69446feaf20b1156--layer5.netlify.app

…hite flash

Signed-off-by: Ankit Rewar <171806101+AnkitRewar11@users.noreply.github.com>
@l5io
Copy link
Contributor

l5io commented Mar 3, 2026

🚀 Preview for commit 4c00b0f at: https://69a6f5b69b91ec0965af033c--layer5.netlify.app

Signed-off-by: Ankit Rewar <171806101+AnkitRewar11@users.noreply.github.com>
@l5io
Copy link
Contributor

l5io commented Mar 3, 2026

🚀 Preview for commit e238605 at: https://69a6fc3180551b17ed94b78c--layer5.netlify.app

Copy link
Contributor

@Bhumikagarggg Bhumikagarggg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please look it carefully it is still not working

Signed-off-by: Ankit Rewar <171806101+AnkitRewar11@users.noreply.github.com>
Signed-off-by: Ankit Rewar <171806101+AnkitRewar11@users.noreply.github.com>
@l5io
Copy link
Contributor

l5io commented Mar 3, 2026

🚀 Preview for commit dc10791 at: https://69a707bdb720f07b5a3f10e3--layer5.netlify.app

@AnkitRewar11
Copy link
Author

@Bhumikagarggg Fixed FOUC properly by updating the onRenderBody.js script to set the background dynamically before hydration. Reverted isDark to false to avoid dark flashes for light mode users. Please review.

@YASHMAHAKAL
Copy link

🚀 Preview for commit dc10791 at: https://69a707bdb720f07b5a3f10e3--layer5.netlify.app

@AnkitRewar11, are you aware that this specific link called netlify preview actually shows a site-preview for your changes ? after every commit you'll see this preview which carries chnages you made in your recent commits. so before requesting any review make sure everything is working in those specific netlify previews.. Thanks : )

@YASHMAHAKAL
Copy link

@AnkitRewar11 and yes, i can still see white flash in latest preview :(

Refactor theme handling logic and improve background color application.

Signed-off-by: Ankit Rewar <171806101+AnkitRewar11@users.noreply.github.com>
@l5io
Copy link
Contributor

l5io commented Mar 3, 2026

🚀 Preview for commit cdd8e7d at: https://69a716e51f79b722326cbaca--layer5.netlify.app

Signed-off-by: Ankit Rewar <171806101+AnkitRewar11@users.noreply.github.com>
@l5io
Copy link
Contributor

l5io commented Mar 3, 2026

🚀 Preview for commit 8fd5caa at: https://69a71c097894c73d2fa21956--layer5.netlify.app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dark mode flicker — white background appears briefly before theme loads

4 participants