diff --git a/DeepDrftPublic.Client/Layout/DeepDrftMenu.razor b/DeepDrftPublic.Client/Layout/DeepDrftMenu.razor index 4c7ce0c..00b5c06 100644 --- a/DeepDrftPublic.Client/Layout/DeepDrftMenu.razor +++ b/DeepDrftPublic.Client/Layout/DeepDrftMenu.razor @@ -77,23 +77,21 @@ private bool _mobileMenuOpen; private Guid _viewportSubscriptionId; - protected override async Task OnInitializedAsync() - { - // During SSR prerender the dark-mode state is already seeded by the server-side - // DarkModeService (via IHttpContextAccessor + DarkModeSettings + PersistentComponentState). - // Invoking the EventCallback here during prerender triggers a re-render cycle on the - // SSR renderer that never completes, hanging the page. Guard to interactive-only so - // the cookie sync only runs when the component is actually mounted in the browser. - if (!RendererInfo.IsInteractive) return; - - IsDarkMode = DarkModeCookieService.GetDarkModeAsync(); - await IsDarkModeChanged.InvokeAsync(IsDarkMode); - } - protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) { + // Runs here (not OnInitializedAsync) because OnAfterRenderAsync(firstRender) + // is guaranteed not to execute during any SSR prerender pass. During prerender + // (now interactive across all of Routes), awaiting IsDarkModeChanged.InvokeAsync + // triggers a parent re-render cycle that cannot complete on the prerender renderer + // and hangs the response. Server-side DarkModeService has already seeded + // DarkModeSettings via PersistentComponentState, so the prerender paint is + // already correct; this call reads that persisted value and propagates it to the + // parent so the menu's IsDarkMode parameter stays consistent with DarkModeSettings. + IsDarkMode = DarkModeCookieService.GetDarkMode(); + await IsDarkModeChanged.InvokeAsync(IsDarkMode); + _viewportSubscriptionId = Guid.NewGuid(); await BrowserViewportService.SubscribeAsync( _viewportSubscriptionId, diff --git a/DeepDrftPublic.Client/Services/DarkModeCookieService.cs b/DeepDrftPublic.Client/Services/DarkModeCookieService.cs index 0b12689..177894e 100644 --- a/DeepDrftPublic.Client/Services/DarkModeCookieService.cs +++ b/DeepDrftPublic.Client/Services/DarkModeCookieService.cs @@ -7,12 +7,9 @@ public class DarkModeCookieService(DarkModeSettings darkModeSetting, IJSRuntime { private const int EXPIRY_DAYS = 365; - public bool GetDarkModeAsync() + public bool GetDarkMode() { return darkModeSetting.IsDarkMode; - // var value = await js.InvokeAsync("eval", - // $"document.cookie.split('; ').find(c => c.startsWith('{COOKIE_NAME}='))?.split('=')[1]"); - // return value == "true"; } public async ValueTask SetDarkModeAsync(bool isDarkMode)