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 <simon.hartfield@googlemail.com>
Co-authored-by: Andy Butland <abutland73@gmail.com>
This commit is contained in:
SimonHartfield
2025-02-04 11:00:44 +00:00
committed by GitHub
parent 7dc6e3ed5b
commit 2422a02b23
2 changed files with 22 additions and 7 deletions

View File

@@ -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<SmidgeOptions>
{
private readonly IOptions<RuntimeMinificationSettings> _runtimeMinificatinoSettings;
private readonly IOptions<RuntimeMinificationSettings> _runtimeMinificationSettings;
public SmidgeOptionsSetup(IOptions<RuntimeMinificationSettings> runtimeMinificatinoSettings)
=> _runtimeMinificatinoSettings = runtimeMinificatinoSettings;
=> _runtimeMinificationSettings = runtimeMinificatinoSettings;
/// <summary>
/// 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.
/// </summary>
/// <param name="options"></param>
/// <param name="options">Instance of <see cref="SmidgeOptions"></see> to configure.</param>
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);
}
}

View File

@@ -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;