Skip to content

fix: resolve flash of unstyled content in dark mode#766

Open
TusharThakur04 wants to merge 1 commit intonodejs:mainfrom
TusharThakur04:fix/dark-mode-FOUC
Open

fix: resolve flash of unstyled content in dark mode#766
TusharThakur04 wants to merge 1 commit intonodejs:mainfrom
TusharThakur04:fix/dark-mode-FOUC

Conversation

@TusharThakur04
Copy link
Copy Markdown

Description

This PR resolves the FOUC by resolving system to dark or light using matchMedia.
Earlier system was directly being applied as set-theme = system

Validation

before :

Screencast.from.2026-04-13.19-26-34.webm

after :

Screencast.from.2026-04-13.19-24-03.webm

Related Issues

n/a

Check List

  • I have read the Contributing Guidelines and made commit messages that follow the guideline.
  • I have run node --run test and all tests passed.
  • I have check code formatting with node --run format & node --run lint.
  • I've covered new added functionality with unit tests if necessary.

@TusharThakur04 TusharThakur04 requested a review from a team as a code owner April 13, 2026 14:23
@cursor
Copy link
Copy Markdown

cursor bot commented Apr 13, 2026

PR Summary

Low Risk
Low risk: small change to web HTML generation and template injection, with minimal logic that only affects initial theme selection before render.

Overview
Ensures the web generator inlines a dedicated theme-initialization script into the HTML <head> so the correct light/dark theme is applied before first paint, reducing dark-mode FOUC.

Replaces the hardcoded inline theme snippet in template.html with a ${themeScript} placeholder, and updates generate.mjs to read ui/theme-script.mjs and inject it as an inline <script> during generation (including resolving theme=system via matchMedia).

Reviewed by Cursor Bugbot for commit 34de276. Bugbot is set up for automated code reviews on this repo. Configure here.

@vercel
Copy link
Copy Markdown

vercel bot commented Apr 13, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
api-docs-tooling Ready Ready Preview Apr 15, 2026 6:12pm

Request Review

Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 893ddea. Configure here.

Comment thread src/generators/web/template.html Outdated
Comment thread src/generators/web/template.html Outdated

<!-- Apply theme before paint to avoid Flash of Unstyled Content -->
<script>document.documentElement.setAttribute("data-theme", document.documentElement.style.colorScheme = localStorage.getItem("theme") || (matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light"));</script>
<script>(function(){var p=localStorage.getItem("theme");var t=(!p||p==="system")?(matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light"):p;document.documentElement.setAttribute("data-theme",t);document.documentElement.style.colorScheme=t;})();</script>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we should make this code block more readable for developers who want to contribute or review it. Instead of writing it as a single line, using more descriptive variable names shouldn't significantly impact the file size. @nodejs/web-infra WDYT?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If that's the case this script source should be in its own file and then inlined via a template string {{}}

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.

3 participants