From 55f7b48d80afca1551a48750338bd37e0a9d9c90 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Tue, 19 Nov 2024 20:06:02 +0100 Subject: [PATCH] [backend/asp] Fix BlazorSsrHandoffMiddleware not getting triggered --- .../Middleware/BlazorSsrHandoffMiddleware.cs | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/Iceshrimp.Backend/Core/Middleware/BlazorSsrHandoffMiddleware.cs b/Iceshrimp.Backend/Core/Middleware/BlazorSsrHandoffMiddleware.cs index ade772ca..fd53bf66 100644 --- a/Iceshrimp.Backend/Core/Middleware/BlazorSsrHandoffMiddleware.cs +++ b/Iceshrimp.Backend/Core/Middleware/BlazorSsrHandoffMiddleware.cs @@ -1,30 +1,33 @@ +using System.Collections.Concurrent; using System.Reflection; using Iceshrimp.Backend.Core.Extensions; using Microsoft.AspNetCore.Components.Endpoints; namespace Iceshrimp.Backend.Core.Middleware; -public class BlazorSsrHandoffMiddleware(RequestDelegate next) : ConditionalMiddleware +public class BlazorSsrHandoffMiddleware(RequestDelegate next) : IConditionalMiddleware { + private static readonly ConcurrentDictionary Cache = []; + public async Task InvokeAsync(HttpContext context) { - var attribute = context.GetEndpoint() - ?.Metadata.GetMetadata() - ?.Type.GetCustomAttributes() - .FirstOrDefault(); - - if (attribute != null) + context.Response.OnStarting(() => { - context.Response.OnStarting(() => - { - context.Response.Headers.Remove("blazor-enhanced-nav"); - return Task.CompletedTask; - }); - } + context.Response.Headers.Remove("blazor-enhanced-nav"); + return Task.CompletedTask; + }); await next(context); } + public static bool Predicate(HttpContext ctx) + => ctx.GetEndpoint() is { } endpoint && + Cache.GetOrAdd(endpoint, e => e.Metadata.GetMetadata() + ?.Type + .GetCustomAttributes() + .Any() ?? + false); + public static void DisableBlazorJsInitializers(RazorComponentsServiceOptions options) { var property =