Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blazor Web App - Persist Style & Scripts #19410

Merged
merged 3 commits into from
Mar 25, 2024
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
@using Volo.Abp
@implements IDisposable
@inject IComponentBundleManager BundleManager
@inject PersistentComponentState PersistentComponentState

@if (ScriptFiles != null)
{
Expand All @@ -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();
}
Original file line number Diff line number Diff line change
@@ -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" />
}
}

Expand All @@ -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();
}
Loading