Skip to content

chore(Layout): prevent first-paint theme flash and remove unused layouts - #8915

Merged
ArgoZhang merged 2 commits into
mainfrom
refactor-base
Dec 4, 2026
Merged

ArgoZhang merged 2 commits into
mainfrom
refactor-base

Conversation

@ArgoZhang

@ArgoZhang ArgoZhang commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

Link issues

fixes #8914

Summary By Copilot

  • Apply the saved theme from localStorage with an inline script in App.razor before the first paint, falling back to prefers-color-scheme for auto, so the dark theme no longer flashes.
  • Remove the redundant initTheme JS module invocation and injections from BaseLayout, and delete BaseLayout.razor.js.
  • Delete the empty ErrorLayout and its usage on the Error page.

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Changes are limited to the sample site (BootstrapBlazor.Server); the component library API is unchanged.

Verification

  • Manual (required)
  • Automated

No build or tests were run for this PR-only task; only the branch diff was checked, and no browser verification was performed.

Packaging changes reviewed?

  • Yes
  • No
  • N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Merge the latest code from the main branch

Summary by Sourcery

Set the saved theme before the first paint and remove unused layout code.

Bug Fixes:

  • Apply the saved theme before the page’s first paint, using the system color preference when the saved setting is auto, to prevent a theme flash.

Enhancements:

  • Remove the unused ErrorLayout and its assignment from the error page.

Chores:

  • Delete the obsolete BaseLayout.razor.js theme module and remove its JavaScript runtime and website-options injections from BaseLayout.

@sourcery-ai

sourcery-ai Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Implementation

Remove the layout’s theme-initialization module call and its now-unused dependencies, and delete the module itself.

  • src/BootstrapBlazor.Server/Components/Layout/BaseLayout.razor.cs L6-67 (4 small edits)
  • src/BootstrapBlazor.Server/Components/Layout/BaseLayout.razor.js (deleted)

UI

Apply the saved theme before first paint, resolving the auto setting from the system color-scheme preference.

  • src/BootstrapBlazor.Server/Components/App.razor L8-16

Remove the empty error layout and stop assigning it to the error page.

  • src/BootstrapBlazor.Server/Components/Layout/ErrorLayout.razor (deleted)
  • src/BootstrapBlazor.Server/Components/Pages/Error.razor L2 (removed lines)

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@bb-auto bb-auto Bot added the chore This are tasks or bot action label Dec 4, 2026
@bb-auto bb-auto Bot added this to the v11.1.0 milestone Dec 4, 2026

@bb-auto bb-auto Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Auto approved by bb-auto

@ArgoZhang ArgoZhang changed the title chore(Server): prevent first-paint theme flash and remove unused layouts chore(Layout): prevent first-paint theme flash and remove unused layouts Dec 4, 2026
@ArgoZhang
ArgoZhang merged commit e2909ea into main Dec 4, 2026
4 checks passed
@ArgoZhang
ArgoZhang deleted the refactor-base branch December 4, 2026 03:04
@sourcery-ai

sourcery-ai Bot commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Linked issues

Checked at commit 88c9572

#8914 chore(Server): prevent first-paint theme flash and remove unused layouts · 5 of 5 met

# Requirement Status Notes
1 Set the saved theme before first paint, using the system color preference when the setting is auto. ✅ Met The inline script in App.razor reads localStorage before the stylesheets and applies either the saved theme or the system preference.
2 Remove the IJSRuntime and IOptions injections and the initTheme call from BaseLayout. ✅ Met
3 Delete BaseLayout.razor.js. ✅ Met
4 Delete the empty ErrorLayout and remove its assignment from Error.razor. ✅ Met
5 Keep the changes within the Server sample site without changing the component library or public API. ✅ Met The diff is limited to files under src/BootstrapBlazor.Server.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've found 4 issues

Sourcery assessment

Approval pending. 3 findings to address first.

Blocking findings: src/BootstrapBlazor.Server/Components/Pages/Error.razor:2, src/BootstrapBlazor.Server/Components/App.razor:10, src/BootstrapBlazor.Server/Components/App.razor:10


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

@@ -1,5 +1,4 @@
@page "/error"
@layout ErrorLayout

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Medium · Error page repeats layout failure

When the exception handler routes to /error after an exception while the normal layout or one of its dependencies is being initialized, Routes applies MainLayout because Error.razor no longer selects ErrorLayout; rendering the error page then hits the same failure again, so the caller does not get the error page.

Restore @layout ErrorLayout on Error.razor.

<head>
<script>
(() => {
const theme = localStorage.getItem('theme') ?? 'auto';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Medium · Dark system preference is ignored

When the browser blocks access to local storage, localStorage.getItem('theme') throws before the IIFE checks prefers-color-scheme, leaving the document's hard-coded light theme in place even when the system prefers dark.

Catch local-storage access errors and use the system preference when no saved theme can be read.

<head>
<script>
(() => {
const theme = localStorage.getItem('theme') ?? 'auto';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Medium · Concurrent theme change flashes stale theme

When another tab changes the saved theme after the inline script reads it but before ThemeProvider.init runs, the inline script keeps its earlier theme snapshot for the first paint; later, ThemeProvider.init rereads localStorage and setTheme applies the newer value, so the page briefly shows the old theme.

Reconcile cross-tab theme changes during initialization, for example by handling the storage event and updating the root theme.

const activeTheme = theme === 'auto'
? (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light')
: theme;
document.documentElement.setAttribute('data-bs-theme', activeTheme);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚪ Low · Theme startup bypasses JSInterop

When the sample site's App document is loaded, the inline script calls document.documentElement.setAttribute directly, so theme initialization manipulates the DOM without JSInterop, contrary to the repository rule.

Route the theme DOM update through JSInterop instead of calling setAttribute directly.

From docs/agents/component-checklist.md L21:

- 不直接操作 DOM(用 JSInterop)

@codecov

codecov Bot commented Dec 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (c1d9339) to head (88c9572).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #8915   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          664       664           
  Lines        30526     30526           
=========================================
  Hits         30526     30526           
Flag Coverage Δ
BB 100.00% <ø> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Labels

chore This are tasks or bot action

Projects

None yet

Development

Successfully merging this pull request may close these issues.

chore(Server): prevent first-paint theme flash and remove unused layouts

1 participant