Skip to content

Commit

Permalink
feat(templates): add StaticSsr render mode to Boilerplate #8080 (#8081)
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmoradi authored Jul 21, 2024
1 parent 10997f8 commit 9f98ee2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public partial class TodoPage
private string? underEditTodoItemTitle;
private string newTodoTitle = string.Empty;
private ConfirmMessageBox confirmMessageBox = default!;
private IList<TodoItemDto> allTodoItems = default!;
private IList<TodoItemDto> allTodoItems = [];
private IList<TodoItemDto> viewTodoItems = default!;
private List<BitDropdownItem<string>> sortItems = [];
private BitSearchBox searchBox = default!;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
//-:cnd:noEmit
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.Web;

namespace Boilerplate.Client.Core.Services;

public static class AppRenderMode
{
public static readonly bool PrerenderEnabled = false;

private static IComponentRenderMode Auto { get; } = new InteractiveAutoRenderMode(PrerenderEnabled);
private static IComponentRenderMode BlazorWebAssembly { get; } = new InteractiveWebAssemblyRenderMode(PrerenderEnabled);
private static IComponentRenderMode BlazorServer { get; } = new InteractiveServerRenderMode(PrerenderEnabled);
// https://learn.microsoft.com/en-us/aspnet/core/blazor/components/render-modes#render-modes
public static IComponentRenderMode Auto { get; } = new InteractiveAutoRenderMode(PrerenderEnabled);
public static IComponentRenderMode BlazorWebAssembly { get; } = new InteractiveWebAssemblyRenderMode(PrerenderEnabled);
public static IComponentRenderMode BlazorServer { get; } = new InteractiveServerRenderMode(PrerenderEnabled);
public static IComponentRenderMode? StaticSsr { get; } = null /*Pre-rendering without interactivity*/;
public static IComponentRenderMode NoPrerenderBlazorWebAssembly => new InteractiveWebAssemblyRenderMode(prerender: false);

public static IComponentRenderMode Current =>
AppEnvironment.IsDev()
public static IComponentRenderMode? Current =>
AppEnvironment.IsDev()
? BlazorServer // For better development experience.
: Auto; // For better production experience.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ public class PrerenderStateService : IPrerenderStateService, IAsyncDisposable
private readonly PersistentComponentState? persistentComponentState;
private readonly ConcurrentDictionary<string, object?> values = new();

private static bool noPersistant = AppRenderMode.Current == AppRenderMode.StaticSsr ||
AppRenderMode.PrerenderEnabled is false ||
AppPlatform.IsBlazorHybrid;

public PrerenderStateService(PersistentComponentState? persistentComponentState = null)
{
if (noPersistant) return;
subscription = persistentComponentState?.RegisterOnPersisting(PersistAsJson, AppRenderMode.Current);
this.persistentComponentState = persistentComponentState;
}
Expand All @@ -24,8 +29,7 @@ public PrerenderStateService(PersistentComponentState? persistentComponentState
[CallerMemberName] string memberName = "",
[CallerFilePath] string filePath = "")
{
if (AppRenderMode.PrerenderEnabled is false || AppPlatform.IsBlazorHybrid)
return await factory();
if (noPersistant) return await factory();

string key = $"{filePath.Split('\\').LastOrDefault()} {memberName} {lineNumber}";

Expand All @@ -34,8 +38,7 @@ public PrerenderStateService(PersistentComponentState? persistentComponentState

public async Task<T?> GetValue<T>(string key, Func<Task<T?>> factory)
{
if (AppRenderMode.PrerenderEnabled is false || AppPlatform.IsBlazorHybrid)
return await factory();
if (noPersistant) return await factory();

if (persistentComponentState!.TryTakeFromJson(key, out T? value)) return value;

Expand All @@ -46,8 +49,7 @@ public PrerenderStateService(PersistentComponentState? persistentComponentState

void Persist<T>(string key, T value)
{
if (AppRenderMode.PrerenderEnabled is false || AppPlatform.IsBlazorHybrid)
return;
if (noPersistant) return;

values.TryRemove(key, out object? _);
values.TryAdd(key, value);
Expand All @@ -63,8 +65,7 @@ async Task PersistAsJson()

public async ValueTask DisposeAsync()
{
if (AppRenderMode.PrerenderEnabled is false || AppPlatform.IsBlazorHybrid)
return;
if (noPersistant) return;

subscription?.Dispose();
}
Expand Down

0 comments on commit 9f98ee2

Please sign in to comment.