From 2422a02b23fafa433592ab50c8fa003dc97bcd31 Mon Sep 17 00:00:00 2001 From: SimonHartfield Date: Tue, 4 Feb 2025 11:00:44 +0000 Subject: [PATCH] Set Smidge cachebuster type (#18198) * Set Smidge cachebuster type * Amend exception and fix typo * Minor tweak to comment and exception message. --------- Co-authored-by: Simon Hartfield Co-authored-by: Andy Butland --- .../RuntimeMinification/SmidgeOptionsSetup.cs | 27 ++++++++++++++----- .../SmidgeRuntimeMinifier.cs | 2 +- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/Umbraco.Web.Common/RuntimeMinification/SmidgeOptionsSetup.cs b/src/Umbraco.Web.Common/RuntimeMinification/SmidgeOptionsSetup.cs index 37701928c6..2452e30f66 100644 --- a/src/Umbraco.Web.Common/RuntimeMinification/SmidgeOptionsSetup.cs +++ b/src/Umbraco.Web.Common/RuntimeMinification/SmidgeOptionsSetup.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.Options; +using Smidge.Cache; using Smidge.Options; using Umbraco.Cms.Core.Configuration.Models; @@ -6,17 +7,31 @@ namespace Umbraco.Cms.Web.Common.RuntimeMinification; public class SmidgeOptionsSetup : IConfigureOptions { - private readonly IOptions _runtimeMinificatinoSettings; + private readonly IOptions _runtimeMinificationSettings; public SmidgeOptionsSetup(IOptions runtimeMinificatinoSettings) - => _runtimeMinificatinoSettings = runtimeMinificatinoSettings; + => _runtimeMinificationSettings = runtimeMinificatinoSettings; /// - /// Configures Smidge to use in-memory caching if configured that way or if certain cache busters are used + /// Configures Smidge to use in-memory caching if configured that way or if certain cache busters are used. + /// Also sets the cache buster type such that public facing bundles will use the configured method. /// - /// + /// Instance of to configure. public void Configure(SmidgeOptions options) - => options.CacheOptions.UseInMemoryCache = _runtimeMinificatinoSettings.Value.UseInMemoryCache || - _runtimeMinificatinoSettings.Value.CacheBuster == + { + options.CacheOptions.UseInMemoryCache = _runtimeMinificationSettings.Value.UseInMemoryCache || + _runtimeMinificationSettings.Value.CacheBuster == RuntimeMinificationCacheBuster.Timestamp; + + Type cacheBusterType = _runtimeMinificationSettings.Value.CacheBuster switch + { + RuntimeMinificationCacheBuster.AppDomain => typeof(AppDomainLifetimeCacheBuster), + RuntimeMinificationCacheBuster.Version => typeof(UmbracoSmidgeConfigCacheBuster), + RuntimeMinificationCacheBuster.Timestamp => typeof(TimestampCacheBuster), + _ => throw new ArgumentOutOfRangeException("CacheBuster", $"{_runtimeMinificationSettings.Value.CacheBuster} is not a valid value for RuntimeMinificationCacheBuster."), + }; + + options.DefaultBundleOptions.DebugOptions.SetCacheBusterType(cacheBusterType); + options.DefaultBundleOptions.ProductionOptions.SetCacheBusterType(cacheBusterType); + } } diff --git a/src/Umbraco.Web.Common/RuntimeMinification/SmidgeRuntimeMinifier.cs b/src/Umbraco.Web.Common/RuntimeMinification/SmidgeRuntimeMinifier.cs index b06f8d0688..cd0b80d1dd 100644 --- a/src/Umbraco.Web.Common/RuntimeMinification/SmidgeRuntimeMinifier.cs +++ b/src/Umbraco.Web.Common/RuntimeMinification/SmidgeRuntimeMinifier.cs @@ -79,7 +79,7 @@ public class SmidgeRuntimeMinifier : IRuntimeMinifier RuntimeMinificationCacheBuster.AppDomain => typeof(AppDomainLifetimeCacheBuster), RuntimeMinificationCacheBuster.Version => typeof(UmbracoSmidgeConfigCacheBuster), RuntimeMinificationCacheBuster.Timestamp => typeof(TimestampCacheBuster), - _ => throw new NotImplementedException(), + _ => throw new ArgumentOutOfRangeException("CacheBuster", $"{runtimeMinificationSettings.Value.CacheBuster} is not a valid value for RuntimeMinificationCacheBuster."), }; _cacheBusterType = cacheBusterType;