Iceshrimp.NET/Iceshrimp.Backend/Core/Middleware/BlazorSsrHandoffMiddleware.cs
Laura Hausmann 705e061f74
[backend/asp] Refactor middleware stack
This commit splits the request pipeline conditionally instead of invoking every middleware in the stack.

It also simplifies middleware instantiation by using runtime discovery, allowing for Plugins to add Middleware.
2024-11-18 19:02:44 +01:00

38 lines
No EOL
1.1 KiB
C#

using System.Reflection;
using Iceshrimp.Backend.Core.Extensions;
using Microsoft.AspNetCore.Components.Endpoints;
namespace Iceshrimp.Backend.Core.Middleware;
public class BlazorSsrHandoffMiddleware(RequestDelegate next) : ConditionalMiddleware<BlazorSsrAttribute>
{
public async Task InvokeAsync(HttpContext context)
{
var attribute = context.GetEndpoint()
?.Metadata.GetMetadata<RootComponentMetadata>()
?.Type.GetCustomAttributes<BlazorSsrAttribute>()
.FirstOrDefault();
if (attribute != null)
{
context.Response.OnStarting(() =>
{
context.Response.Headers.Remove("blazor-enhanced-nav");
return Task.CompletedTask;
});
}
await next(context);
}
public static void DisableBlazorJsInitializers(RazorComponentsServiceOptions options)
{
var property =
options.GetType().GetProperty("JavaScriptInitializers", BindingFlags.Instance | BindingFlags.NonPublic) ??
throw new Exception("Failed to disable Blazor JS initializers");
property.SetValue(options, null);
}
}
public class BlazorSsrAttribute : Attribute;