diff --git a/InertiaNetCore/Inertia.cs b/InertiaNetCore/Inertia.cs index 4bcafee..f0691c0 100644 --- a/InertiaNetCore/Inertia.cs +++ b/InertiaNetCore/Inertia.cs @@ -35,6 +35,9 @@ public static class Inertia public static void Share(InertiaProps data) => _factory.Share(data); public static void Flash(string key, string? value) => _factory.Flash(key, value); + + public static void EnableEncryptHistory(bool enable = true) => _factory.EnableEncryptHistory(enable); + public static void ClearHistory() => _factory.ClearHistory(); public static LazyProp Lazy(Func callback) => _factory.Lazy(callback); public static LazyProp Lazy(Func> callback) => _factory.Lazy(callback); diff --git a/InertiaNetCore/Models/InertiaOptions.cs b/InertiaNetCore/Models/InertiaOptions.cs index 759c346..db7a14c 100644 --- a/InertiaNetCore/Models/InertiaOptions.cs +++ b/InertiaNetCore/Models/InertiaOptions.cs @@ -13,6 +13,8 @@ public class InertiaOptions public Action ConfigureSession { get; set; } = _ => { }; + public bool EncryptHistory { get; set; } + private static JsonSerializerOptions DefaultJsonSerializerOptions { get; } = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, diff --git a/InertiaNetCore/Models/InertiaPage.cs b/InertiaNetCore/Models/InertiaPage.cs index a936893..43c6374 100644 --- a/InertiaNetCore/Models/InertiaPage.cs +++ b/InertiaNetCore/Models/InertiaPage.cs @@ -8,4 +8,6 @@ public readonly record struct InertiaPage public required string Component { get; init; } public required string? Version { get; init; } public required string Url { get; init; } + public required bool EncryptHistory { get; init; } + public required bool ClearHistory { get; init; } } diff --git a/InertiaNetCore/Response.cs b/InertiaNetCore/Response.cs index 67d5436..1fa45cd 100644 --- a/InertiaNetCore/Response.cs +++ b/InertiaNetCore/Response.cs @@ -9,10 +9,11 @@ namespace InertiaNetCore; -public class Response(string component, InertiaProps props, string? version, InertiaOptions options) +public class Response(string component, InertiaProps props, string? version, InertiaOptions options, bool? encryptHistory, bool clearHistory) : IActionResult { private IDictionary? _viewData; + private readonly InertiaOptions _options = options; public async Task ExecuteResultAsync(ActionContext context) { @@ -23,7 +24,9 @@ public async Task ExecuteResultAsync(ActionContext context) Url = context.HttpContext.RequestedUri(), Props = await GetFinalProps(context), DeferredProps = GetDeferredProps(context), - MergeProps = GetMergeProps(context) + MergeProps = GetMergeProps(context), + ClearHistory = clearHistory, + EncryptHistory = encryptHistory ?? options.EncryptHistory }; if (!context.HttpContext.IsInertiaRequest()) @@ -39,7 +42,7 @@ public async Task ExecuteResultAsync(ActionContext context) viewData[key] = value; } - await new ViewResult { ViewName = options.RootView, ViewData = viewData }.ExecuteResultAsync(context); + await new ViewResult { ViewName = _options.RootView, ViewData = viewData }.ExecuteResultAsync(context); } else { @@ -47,7 +50,7 @@ public async Task ExecuteResultAsync(ActionContext context) context.HttpContext.Response.Headers.Append("Vary", "Accept"); context.HttpContext.Response.StatusCode = 200; - var jsonResult = new JsonResult(page, options.JsonSerializerOptions); + var jsonResult = new JsonResult(page, _options.JsonSerializerOptions); await jsonResult.ExecuteResultAsync(context); } } @@ -89,7 +92,7 @@ private Dictionary> GetDeferredProps(ActionContext context) } // apply json serialization options to dictionary keys before grouping them - var jsonOptions = options.JsonSerializerOptions as JsonSerializerOptions; + var jsonOptions = _options.JsonSerializerOptions as JsonSerializerOptions; tmp = JsonSerializer.Deserialize>(JsonSerializer.Serialize(tmp, jsonOptions), jsonOptions); return tmp! @@ -113,7 +116,7 @@ private List GetMergeProps(ActionContext context) } // apply json serialization options to dictionary keys before grouping them - var jsonOptions = options.JsonSerializerOptions as JsonSerializerOptions; + var jsonOptions = _options.JsonSerializerOptions as JsonSerializerOptions; tmp = JsonSerializer.Deserialize>(JsonSerializer.Serialize(tmp, jsonOptions), jsonOptions); return tmp!.Select(prop => prop.Key).ToList(); diff --git a/InertiaNetCore/ResponseFactory.cs b/InertiaNetCore/ResponseFactory.cs index 9df54d3..60d6163 100644 --- a/InertiaNetCore/ResponseFactory.cs +++ b/InertiaNetCore/ResponseFactory.cs @@ -14,12 +14,14 @@ namespace InertiaNetCore; internal class ResponseFactory(IHttpContextAccessor contextAccessor, SsrGateway ssrGateway, IOptions options) { private object? _version; + private bool? _encryptHistory; + private bool _clearHistory; public Response Render(string component, InertiaProps? props = default) { props ??= []; - return new Response(component, props, GetVersion(), options.Value); + return new Response(component, props, GetVersion(), options.Value, _encryptHistory, _clearHistory); } public async Task Head(dynamic model) @@ -111,6 +113,16 @@ public void Flash(string key, string? value) context.Features.Set(flash); } + public void EnableEncryptHistory(bool enable = true) + { + _encryptHistory = enable; + } + + public void ClearHistory() + { + _clearHistory = true; + } + public LazyProp Lazy(Func callback) => new(callback); public LazyProp Lazy(Func> callback) => new(callback); public DeferredProp Defer(Func callback, string? group) => new(callback, group);