From 262df29f8cdca6cf92544c77661f7c3376c0a199 Mon Sep 17 00:00:00 2001 From: Enis Necipoglu <info@enisnecipoglu.com> Date: Mon, 25 Mar 2024 10:26:30 +0300 Subject: [PATCH 1/3] Persist component state for WebApp Styles --- .../Bundling/AbpStyles.razor | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 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 488caba7975..053e0efd014 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,11 +1,13 @@ @using Volo.Abp +@implements IDisposable @inject IComponentBundleManager BundleManager +@inject PersistentComponentState PersistentComponentState @if (StyleFiles != null) { foreach (var file in StyleFiles) { - <link rel="stylesheet" href="@file"/> + <link rel="stylesheet" href="@file" /> } } @@ -18,16 +20,37 @@ private List<string>? StyleFiles { get; set; } + private PersistingComponentStateSubscription persistingSubscription; + protected override async Task OnInitializedAsync() { if (BundleName == null) { throw new AbpException("The BundleName parameter of the AbpStyles component can not be null!"); } - StyleFiles = (await BundleManager.GetStyleBundleFilesAsync(BundleName!)).ToList(); + + persistingSubscription = PersistentComponentState.RegisterOnPersisting(PersistStyleFiles); + + if (PersistentComponentState.TryTakeFromJson<List<string>>(nameof(StyleFiles), out var restoredStyleFiles)) + { + StyleFiles = restoredStyleFiles; + } + else + { + StyleFiles = (await BundleManager.GetStyleBundleFilesAsync(BundleName!)).ToList(); + } + if (OperatingSystem.IsBrowser() && WebAssemblyStyleFiles != null) { - StyleFiles.AddRange(WebAssemblyStyleFiles); + StyleFiles!.AddRange(WebAssemblyStyleFiles); } } + + private Task PersistStyleFiles() + { + PersistentComponentState.PersistAsJson(nameof(StyleFiles), StyleFiles); + return Task.CompletedTask; + } + + public void Dispose() => persistingSubscription.Dispose(); } From 6107bc50640991af9d3608a98c713aa89c2d7960 Mon Sep 17 00:00:00 2001 From: Enis Necipoglu <info@enisnecipoglu.com> Date: Mon, 25 Mar 2024 10:31:39 +0300 Subject: [PATCH 2/3] Update AbpStyles.razor --- .../Bundling/AbpStyles.razor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 053e0efd014..bae9b382f3a 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 @@ -42,7 +42,7 @@ if (OperatingSystem.IsBrowser() && WebAssemblyStyleFiles != null) { - StyleFiles!.AddRange(WebAssemblyStyleFiles); + StyleFiles?.AddRange(WebAssemblyStyleFiles); } } From f02af6326d2c10f05f18091223bd0e89604b16e0 Mon Sep 17 00:00:00 2001 From: Enis Necipoglu <info@enisnecipoglu.com> Date: Mon, 25 Mar 2024 10:31:52 +0300 Subject: [PATCH 3/3] Persist component state for WebApp Scripts --- .../Bundling/AbpScripts.razor | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 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 435e318afac..6735d54d14b 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,5 +1,7 @@ @using Volo.Abp +@implements IDisposable @inject IComponentBundleManager BundleManager +@inject PersistentComponentState PersistentComponentState @if (ScriptFiles != null) { @@ -18,16 +20,37 @@ private List<string>? ScriptFiles { get; set; } + private PersistingComponentStateSubscription persistingSubscription; + protected override async Task OnInitializedAsync() { if (BundleName == null) { throw new AbpException("The BundleName parameter of the AbpScripts component can not be null!"); } - ScriptFiles = (await BundleManager.GetScriptBundleFilesAsync(BundleName!)).ToList(); + + persistingSubscription = PersistentComponentState.RegisterOnPersisting(PersistScriptFiles); + + if (PersistentComponentState.TryTakeFromJson<List<string>>(nameof(ScriptFiles), out var restoredStyleFiles)) + { + ScriptFiles = restoredStyleFiles; + } + else + { + ScriptFiles = (await BundleManager.GetScriptBundleFilesAsync(BundleName!)).ToList(); + } + if (OperatingSystem.IsBrowser() && WebAssemblyScriptFiles != null) { - ScriptFiles.AddRange(WebAssemblyScriptFiles); + ScriptFiles?.AddRange(WebAssemblyScriptFiles); } } + + private Task PersistScriptFiles() + { + PersistentComponentState.PersistAsJson(nameof(ScriptFiles), ScriptFiles); + return Task.CompletedTask; + } + + public void Dispose() => persistingSubscription.Dispose(); }