Skip to content

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

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

fix: resolve dark mode white flash on initial page load#7446
AnkitRewar11 wants to merge 29 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
Contributor

@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
Contributor

@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
Member

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
Member

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
Member

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
Member

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
Contributor

🚀 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
Contributor

@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
Member

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
Member

l5io commented Mar 3, 2026

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

Signed-off-by: Ankit Rewar <171806101+AnkitRewar11@users.noreply.github.com>
Signed-off-by: Ankit Rewar <171806101+AnkitRewar11@users.noreply.github.com>
Updated background colors for dark mode and light mode.

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

l5io commented Mar 7, 2026

🚀 Preview for commit 9095d71 at: https://69abc061ed37728d8e011b33--layer5.netlify.app

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

l5io commented Mar 7, 2026

🚀 Preview for commit 63cb585 at: https://69abd59d900f7bca7380e519--layer5.netlify.app

@AnkitRewar11
Copy link
Author

@YASHMAHAKAL @Bhumikagarggg
Sorry for the earlier mistakes. I’m still learning and needed a few attempts to figure it out.

I’ve pushed a fix for the FOUC issue by updating two files: onRenderBody.js (added visibility: hidden with a dark default) and ThemeManager.js (set isDark default to true).

Could you please check the Netlify preview when you get a chance? Thank you. 🙂

@AnkitRewar11
Copy link
Author

I’m not seeing the issue on the Netlify preview anymore from my side. Could you please check it once as well? 🙂


const defaultState = {
isDark: false,
isDark: true, // default dark
Copy link
Member

Choose a reason for hiding this comment

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

@AnkitRewar11 this is wrong, we do not want default dark mode.

@saurabhraghuvanshii
Copy link
Member

@AnkitRewar11 please also remove all lint changes. Keep only logical changes

Signed-off-by: Ankit Rewar <171806101+AnkitRewar11@users.noreply.github.com>
Removed comments regarding theme handling logic.

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

l5io commented Mar 7, 2026

🚀 Preview for commit 317d48d at: https://69ac0f36af6ad123b89386c5--layer5.netlify.app

Removed comments and simplified theme object spread.

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

l5io commented Mar 7, 2026

🚀 Preview for commit 0aca758 at: https://69ac2059911f6b445ccd334e--layer5.netlify.app


export const ThemeManagerProvider = (props) => {
const [themeSetting, setThemeSetting] = useState(ThemeSetting.SYSTEM);
const [themeSetting, setThemeSetting] = useState(ThemeSetting.DARK);
Copy link
Member

Choose a reason for hiding this comment

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

@AnkitRewar11 keep it system. By default your settings it to dark.

@Bhumikagarggg
Copy link
Contributor

@AnkitRewar11 Thank you for your contribution! Let's discuss this during the website call today at 6:30 PM IST | 7 AM CST Add it as an agenda item to the meeting minutes, if you would 🙂

@leecalcote
Copy link
Member

leecalcote commented Mar 11, 2026

Great effort tackling FOUC — the visibility: hidden + inline script approach is the right direction! One thing to watch: if the try block throws (e.g., localStorage blocked in private browsing), visibility never gets reset to visible and the page goes blank. Moving that line outside the try/catch should sort it. Keep at it. 🙌

@leecalcote
Copy link
Member

leecalcote commented Mar 11, 2026

Nice work digging into the theme initialization flow! The CSS-first background fallback in fonts.css is a solid addition. One heads-up: the new script doesn't check prefers-color-scheme anymore, so users who had "System" selected will always get dark pre-hydration regardless of their OS setting — worth having another look here.

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
Member

l5io commented Mar 12, 2026

🚀 Preview for commit 5b275bf at: https://69b25490c45136bccf8fdbf6--layer5.netlify.app

@l5io
Copy link
Member

l5io commented Mar 12, 2026

🚀 Preview for commit a56ebe2 at: https://69b2584db34fb1e1f835c158--layer5.netlify.app

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

l5io commented Mar 12, 2026

🚀 Preview for commit f3c5dcf at: https://69b25eeaa53125de80717aba--layer5.netlify.app

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

l5io commented Mar 12, 2026

🚀 Preview for commit daea70b at: https://69b2649e420b0200773b520d--layer5.netlify.app

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

l5io commented Mar 12, 2026

🚀 Preview for commit c839a55 at: https://69b26bf4b34fb12d6735c27a--layer5.netlify.app

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

l5io commented Mar 12, 2026

🚀 Preview for commit 03365ec at: https://69b292579886c4ba6da04071--layer5.netlify.app

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

l5io commented Mar 12, 2026

🚀 Preview for commit 624c764 at: https://69b29a9633f4feeb4339e3fd--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

6 participants