Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/BootstrapBlazor.Server/Components/App.razor
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
<html lang="en" data-bs-theme='light'>

<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.

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)

})();
</script>
<meta charset="utf-8" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
Expand Down
13 changes: 0 additions & 13 deletions src/BootstrapBlazor.Server/Components/Layout/BaseLayout.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,13 @@
// See the LICENSE file in the project root for more information.
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone

using Microsoft.Extensions.Options;
using Microsoft.JSInterop;

namespace BootstrapBlazor.Server.Components.Layout;

/// <summary>
/// 母版页基类
/// </summary>
public partial class BaseLayout : IAsyncDisposable
{
[Inject]
[NotNull]
private IJSRuntime? JSRuntime { get; set; }

[Inject]
[NotNull]
private IStringLocalizer<BaseLayout>? Localizer { get; set; }
Expand All @@ -33,10 +26,6 @@ public partial class BaseLayout : IAsyncDisposable
[NotNull]
private IDispatchService<bool>? RebootDispatchService { get; set; }

[Inject]
[NotNull]
private IOptions<WebsiteOptions>? WebsiteOption { get; set; }

[NotNull]
private string? FlowText { get; set; }

Expand Down Expand Up @@ -75,8 +64,6 @@ protected override async Task OnInitializedAsync()
await CommitDispatchService.SubscribeAsync(NotifyCommit);
await RebootDispatchService.SubscribeAsync(NotifyReboot);

var module = await JSRuntime.LoadModule($"{WebsiteOption.Value.JSModuleRootPath}Layout/BaseLayout.razor.js");
await module.InvokeVoidAsync("initTheme");
_init = true;
}

Expand Down

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion src/BootstrapBlazor.Server/Components/Pages/Error.razor
Original file line number Diff line number Diff line change
@@ -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.


<h1 class="text-danger">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>
Expand Down