Browse Source

Fixing the flickering issue when switching the server to wasm.

pull/22357/head
maliming 2 years ago
parent
commit
980b9c8049
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 47
      framework/src/Volo.Abp.AspNetCore.Components.Web.Theming/Bundling/AbpStyles.razor

47
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<string>? WebAssemblyStyleFiles { get; set; }
@ -16,18 +21,48 @@
private List<string>? StyleFiles { get; set; }
private PersistingComponentStateSubscription _persistingSubscription;
protected override async Task OnInitializedAsync()
{
StyleFiles = new List<string>();
if (!BundleName.IsNullOrWhiteSpace())
_persistingSubscription = ApplicationState.RegisterOnPersisting(Callback);
if (!ApplicationState.TryTakeFromJson<List<string>>(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();
}

Loading…
Cancel
Save