From f02af6326d2c10f05f18091223bd0e89604b16e0 Mon Sep 17 00:00:00 2001 From: Enis Necipoglu Date: Mon, 25 Mar 2024 10:31:52 +0300 Subject: [PATCH] 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 435e318afa..6735d54d14 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? 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>(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(); }