From 30d4ec4ef598328efb8e7fc186191d76015fd522 Mon Sep 17 00:00:00 2001 From: Kenn Jacobsen Date: Mon, 4 Nov 2024 18:56:17 +0100 Subject: [PATCH] Prevent Delivery API output cache configuration from affecting other controllers (#17405) Co-authored-by: Elitsa --- .../Caching/NoOutputCachePolicy.cs | 19 +++++++++++++++++++ .../UmbracoBuilderExtensions.cs | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 src/Umbraco.Cms.Api.Delivery/Caching/NoOutputCachePolicy.cs diff --git a/src/Umbraco.Cms.Api.Delivery/Caching/NoOutputCachePolicy.cs b/src/Umbraco.Cms.Api.Delivery/Caching/NoOutputCachePolicy.cs new file mode 100644 index 0000000000..936cb72d2b --- /dev/null +++ b/src/Umbraco.Cms.Api.Delivery/Caching/NoOutputCachePolicy.cs @@ -0,0 +1,19 @@ +using Microsoft.AspNetCore.OutputCaching; + +namespace Umbraco.Cms.Api.Delivery.Caching; + +internal sealed class NoOutputCachePolicy : IOutputCachePolicy +{ + ValueTask IOutputCachePolicy.CacheRequestAsync(OutputCacheContext context, CancellationToken cancellationToken) + { + context.EnableOutputCaching = false; + + return ValueTask.CompletedTask; + } + + ValueTask IOutputCachePolicy.ServeFromCacheAsync(OutputCacheContext context, CancellationToken cancellationToken) + => ValueTask.CompletedTask; + + ValueTask IOutputCachePolicy.ServeResponseAsync(OutputCacheContext context, CancellationToken cancellationToken) + => ValueTask.CompletedTask; +} diff --git a/src/Umbraco.Cms.Api.Delivery/DependencyInjection/UmbracoBuilderExtensions.cs b/src/Umbraco.Cms.Api.Delivery/DependencyInjection/UmbracoBuilderExtensions.cs index 34f8b058e6..77f2cae93c 100644 --- a/src/Umbraco.Cms.Api.Delivery/DependencyInjection/UmbracoBuilderExtensions.cs +++ b/src/Umbraco.Cms.Api.Delivery/DependencyInjection/UmbracoBuilderExtensions.cs @@ -105,7 +105,7 @@ public static class UmbracoBuilderExtensions builder.Services.AddOutputCache(options => { - options.AddBasePolicy(_ => { }); + options.AddBasePolicy(build => build.AddPolicy()); if (outputCacheSettings.ContentDuration.TotalSeconds > 0) {