From c95846c1f638b59c93a438332a0c7c84c919cce0 Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 13 Mar 2025 15:59:29 +0800 Subject: [PATCH 1/3] Avoid loading the script twice after prerender. --- .../Bundling/AbpScripts.razor | 35 +++++++++++++++---- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Components.Web.Theming/Bundling/AbpScripts.razor b/framework/src/Volo.Abp.AspNetCore.Components.Web.Theming/Bundling/AbpScripts.razor index 8c9d070764..234cb04b60 100644 --- a/framework/src/Volo.Abp.AspNetCore.Components.Web.Theming/Bundling/AbpScripts.razor +++ b/framework/src/Volo.Abp.AspNetCore.Components.Web.Theming/Bundling/AbpScripts.razor @@ -1,4 +1,6 @@ +@implements IDisposable @inject IComponentBundleManager BundleManager +@inject PersistentComponentState ApplicationState @if (ScriptFiles != null) { foreach (var file in ScriptFiles) @@ -8,6 +10,9 @@ } @code { + + private const string PrerenderedKey = "abp_script_prerendered"; + [Parameter] public List? WebAssemblyScriptFiles { get; set; } @@ -16,18 +21,34 @@ private List? ScriptFiles { get; set; } + private PersistingComponentStateSubscription _persistingSubscription; + protected override async Task OnInitializedAsync() { - ScriptFiles = new List(); - - if (!BundleName.IsNullOrWhiteSpace()) + _persistingSubscription = ApplicationState.RegisterOnPersisting(Callback); + if (!ApplicationState.TryTakeFromJson(PrerenderedKey, out _)) { - ScriptFiles = (await BundleManager.GetScriptBundleFilesAsync(BundleName!)).ToList(); + // We are in prerendering mode + ScriptFiles = []; + if (!BundleName.IsNullOrWhiteSpace()) + { + ScriptFiles = (await BundleManager.GetScriptBundleFilesAsync(BundleName!)).ToList(); + } } - - if (OperatingSystem.IsBrowser() && WebAssemblyScriptFiles != null) + else { - ScriptFiles.AddIfNotContains(WebAssemblyScriptFiles); + if (OperatingSystem.IsBrowser() && WebAssemblyScriptFiles != null) + { + ScriptFiles = WebAssemblyScriptFiles; + } } } + + private Task Callback() + { + ApplicationState.PersistAsJson(PrerenderedKey, PrerenderedKey); + return Task.CompletedTask; + } + + public void Dispose() => _persistingSubscription.Dispose(); } From 980b9c8049f361825673a80ddb9028940ad12c05 Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 13 Mar 2025 21:29:56 +0800 Subject: [PATCH 2/3] Fixing the flickering issue when switching the server to wasm. --- .../Bundling/AbpStyles.razor | 47 ++++++++++++++++--- 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Components.Web.Theming/Bundling/AbpStyles.razor b/framework/src/Volo.Abp.AspNetCore.Components.Web.Theming/Bundling/AbpStyles.razor index bdcfd26bec..77f3aaa7d1 100644 --- a/framework/src/Volo.Abp.AspNetCore.Components.Web.Theming/Bundling/AbpStyles.razor +++ b/framework/src/Volo.Abp.AspNetCore.Components.Web.Theming/Bundling/AbpStyles.razor @@ -1,4 +1,6 @@ +@implements IDisposable @inject IComponentBundleManager BundleManager +@inject PersistentComponentState ApplicationState @if (StyleFiles != null) { foreach (var file in StyleFiles) @@ -8,6 +10,9 @@ } @code { + + private const string PrerenderedKey = "abp_style_prerendered"; + [Parameter] public List? WebAssemblyStyleFiles { get; set; } @@ -16,18 +21,48 @@ private List? StyleFiles { get; set; } + private PersistingComponentStateSubscription _persistingSubscription; + protected override async Task OnInitializedAsync() { - StyleFiles = new List(); - - if (!BundleName.IsNullOrWhiteSpace()) + _persistingSubscription = ApplicationState.RegisterOnPersisting(Callback); + if (!ApplicationState.TryTakeFromJson>(PrerenderedKey, out var scriptFiles)) { - StyleFiles = (await BundleManager.GetStyleBundleFilesAsync(BundleName!)).ToList(); + // We are in prerendering mode + StyleFiles = []; + if (!BundleName.IsNullOrWhiteSpace()) + { + StyleFiles = (await BundleManager.GetStyleBundleFilesAsync(BundleName!)).ToList(); + } } + else + { + StyleFiles = scriptFiles; + if (OperatingSystem.IsBrowser() && StyleFiles != null && WebAssemblyStyleFiles != null) + { + StyleFiles.AddIfNotContains(WebAssemblyStyleFiles); + } + } + } - if (OperatingSystem.IsBrowser() && WebAssemblyStyleFiles != null) + private bool _hasRemoveServerStyle = false; + + protected override async Task OnAfterRenderAsync(bool firstRender) + { + if (!_hasRemoveServerStyle && OperatingSystem.IsBrowser() && WebAssemblyStyleFiles != null) { - StyleFiles.AddIfNotContains(WebAssemblyStyleFiles); + _hasRemoveServerStyle = true; + await Task.Delay(3000); + StyleFiles = WebAssemblyStyleFiles; + StateHasChanged(); } } + + private Task Callback() + { + ApplicationState.PersistAsJson(PrerenderedKey, StyleFiles); + return Task.CompletedTask; + } + + public void Dispose() => _persistingSubscription.Dispose(); } From 52b53a4e5379c1a51ff67692fa6d4fbed2ec1753 Mon Sep 17 00:00:00 2001 From: maliming Date: Fri, 14 Mar 2025 10:15:34 +0800 Subject: [PATCH 3/3] Remove unnecessary initialization of ScriptFiles and StyleFiles during prerendering --- .../Bundling/AbpScripts.razor | 1 - .../Bundling/AbpStyles.razor | 1 - 2 files changed, 2 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Components.Web.Theming/Bundling/AbpScripts.razor b/framework/src/Volo.Abp.AspNetCore.Components.Web.Theming/Bundling/AbpScripts.razor index 234cb04b60..921937a124 100644 --- a/framework/src/Volo.Abp.AspNetCore.Components.Web.Theming/Bundling/AbpScripts.razor +++ b/framework/src/Volo.Abp.AspNetCore.Components.Web.Theming/Bundling/AbpScripts.razor @@ -29,7 +29,6 @@ if (!ApplicationState.TryTakeFromJson(PrerenderedKey, out _)) { // We are in prerendering mode - ScriptFiles = []; if (!BundleName.IsNullOrWhiteSpace()) { ScriptFiles = (await BundleManager.GetScriptBundleFilesAsync(BundleName!)).ToList(); diff --git a/framework/src/Volo.Abp.AspNetCore.Components.Web.Theming/Bundling/AbpStyles.razor b/framework/src/Volo.Abp.AspNetCore.Components.Web.Theming/Bundling/AbpStyles.razor index 77f3aaa7d1..0b522c02e4 100644 --- a/framework/src/Volo.Abp.AspNetCore.Components.Web.Theming/Bundling/AbpStyles.razor +++ b/framework/src/Volo.Abp.AspNetCore.Components.Web.Theming/Bundling/AbpStyles.razor @@ -29,7 +29,6 @@ if (!ApplicationState.TryTakeFromJson>(PrerenderedKey, out var scriptFiles)) { // We are in prerendering mode - StyleFiles = []; if (!BundleName.IsNullOrWhiteSpace()) { StyleFiles = (await BundleManager.GetStyleBundleFilesAsync(BundleName!)).ToList();