Repository navigation
chore(Layout): prevent first-paint theme flash and remove unused layouts - #8915
Conversation
Reviewer's GuideImplementationRemove the layout’s theme-initialization module call and its now-unused dependencies, and delete the module itself.
UIApply the saved theme before first paint, resolving the auto setting from the system color-scheme preference.
Remove the empty error layout and stop assigning it to the error page.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Linked issuesChecked at commit 88c9572 #8914 chore(Server): prevent first-paint theme flash and remove unused layouts · 5 of 5 met
|
There was a problem hiding this comment.
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
| @@ -1,5 +1,4 @@ | |||
| @page "/error" | |||
| @layout ErrorLayout | |||
There was a problem hiding this comment.
🟡 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'; |
There was a problem hiding this comment.
🟡 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'; |
There was a problem hiding this comment.
🟡 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); |
There was a problem hiding this comment.
⚪ 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 Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #8915 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 664 664
Lines 30526 30526
=========================================
Hits 30526 30526
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Link issues
fixes #8914
Summary By Copilot
localStoragewith an inline script inApp.razorbefore the first paint, falling back toprefers-color-schemeforauto, so the dark theme no longer flashes.initThemeJS module invocation and injections fromBaseLayout, and deleteBaseLayout.razor.js.ErrorLayoutand its usage on theErrorpage.Regression?
Risk
Changes are limited to the sample site (
BootstrapBlazor.Server); the component library API is unchanged.Verification
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?
☑️ Self Check before Merge
Summary by Sourcery
Set the saved theme before the first paint and remove unused layout code.
Bug Fixes:
auto, to prevent a theme flash.Enhancements:
ErrorLayoutand its assignment from the error page.Chores:
BaseLayout.razor.jstheme module and remove its JavaScript runtime and website-options injections fromBaseLayout.