Merge remote-tracking branch 'origin/v9/9.1' into v9/dev

# Conflicts:
#	build/templates/UmbracoPackage/.template.config/template.json
#	build/templates/UmbracoProject/.template.config/template.json
#	src/Directory.Build.props
This commit is contained in:
Bjarke Berg
2021-12-03 07:20:19 +01:00
45 changed files with 1574 additions and 188 deletions

View File

@@ -18,6 +18,7 @@ using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;
using Smidge;
using Smidge.Cache;
using Smidge.FileProcessors;
using Smidge.InMemory;
using Smidge.Nuglify;
@@ -274,6 +275,7 @@ namespace Umbraco.Extensions
new[] { "/App_Plugins/**/*.js", "/App_Plugins/**/*.css" }));
});
builder.Services.AddUnique<ICacheBuster, UmbracoSmidgeConfigCacheBuster>();
builder.Services.AddSmidge(builder.Config.GetSection(Constants.Configuration.ConfigRuntimeMinification));
// Replace the Smidge request helper, in order to discourage the use of brotli since it's super slow
builder.Services.AddUnique<IRequestHelper, SmidgeRequestHelper>();

View File

@@ -24,7 +24,6 @@ namespace Umbraco.Cms.Web.Common.RuntimeMinification
private readonly IHostingEnvironment _hostingEnvironment;
private readonly IConfigManipulator _configManipulator;
private readonly CacheBusterResolver _cacheBusterResolver;
private readonly RuntimeMinificationSettings _runtimeMinificationSettings;
private readonly IBundleManager _bundles;
private readonly SmidgeHelperAccessor _smidge;
@@ -53,7 +52,6 @@ namespace Umbraco.Cms.Web.Common.RuntimeMinification
_hostingEnvironment = hostingEnvironment;
_configManipulator = configManipulator;
_cacheBusterResolver = cacheBusterResolver;
_runtimeMinificationSettings = runtimeMinificationSettings.Value;
_jsMinPipeline = new Lazy<PreProcessPipeline>(() => _bundles.PipelineFactory.Create(typeof(JsMinifier)));
_cssMinPipeline = new Lazy<PreProcessPipeline>(() => _bundles.PipelineFactory.Create(typeof(NuglifyCss)));
@@ -76,10 +74,10 @@ namespace Umbraco.Cms.Web.Common.RuntimeMinification
return defaultCss;
});
Type cacheBusterType = _runtimeMinificationSettings.CacheBuster switch
Type cacheBusterType = runtimeMinificationSettings.Value.CacheBuster switch
{
RuntimeMinificationCacheBuster.AppDomain => typeof(AppDomainLifetimeCacheBuster),
RuntimeMinificationCacheBuster.Version => typeof(ConfigCacheBuster),
RuntimeMinificationCacheBuster.Version => typeof(UmbracoSmidgeConfigCacheBuster),
RuntimeMinificationCacheBuster.Timestamp => typeof(TimestampCacheBuster),
_ => throw new NotImplementedException()
};
@@ -169,18 +167,12 @@ namespace Umbraco.Cms.Web.Common.RuntimeMinification
}
}
/// <inheritdoc />
/// <remarks>
/// Smidge uses the version number as cache buster (configurable).
/// We therefore can reset, by updating the version number in config
/// </remarks>
[Obsolete("Invalidation is handled automatically. Scheduled for removal V11.")]
public void Reset()
{
var version = DateTime.UtcNow.Ticks.ToString();
_configManipulator.SaveConfigValue(Cms.Core.Constants.Configuration.ConfigRuntimeMinificationVersion, version.ToString());
}
}
}

View File

@@ -0,0 +1,77 @@
using System;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Smidge;
using Smidge.Cache;
using Umbraco.Cms.Core.Configuration;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Extensions;
namespace Umbraco.Cms.Web.Common.RuntimeMinification
{
/// <summary>
/// Constructs a cache buster string with sensible defaults.
/// </summary>
/// <remarks>
/// <para>
/// Had planned on handling all of this in SmidgeRuntimeMinifier, but that only handles some urls.
/// </para>
/// <para>
/// A lot of the work is delegated e.g. to <see cref="SmidgeHelper.GenerateJsUrlsAsync(string, bool)"/>
/// which doesn't care about the cache buster string in our classes only the value returned by the resolved ICacheBuster.
/// </para>
/// <para>
/// Then I thought fine I'll just use a IConfigureOptions to tweak upstream <see cref="ConfigCacheBuster"/>, but that only cares about the <see cref="SmidgeConfig"/>
/// class we instantiate and pass through in <see cref="Umbraco.Extensions.UmbracoBuilderExtensions.AddRuntimeMinifier"/>
/// </para>
/// <para>
/// So here we are, create our own to ensure we cache bust in a reasonable fashion.
/// </para>
/// <br/><br/>
/// <para>
/// Note that this class makes some other bits of code pretty redundant e.g. <see cref="UrlHelperExtensions.GetUrlWithCacheBust"/> will
/// concatenate version with CacheBuster value and hash again, but there's no real harm so can think about that later.
/// </para>
/// </remarks>
internal class UmbracoSmidgeConfigCacheBuster : ICacheBuster
{
private readonly IOptions<RuntimeMinificationSettings> _runtimeMinificationSettings;
private readonly IUmbracoVersion _umbracoVersion;
private readonly IEntryAssemblyMetadata _entryAssemblyMetadata;
private string _cacheBusterValue;
public UmbracoSmidgeConfigCacheBuster(
IOptions<RuntimeMinificationSettings> runtimeMinificationSettings,
IUmbracoVersion umbracoVersion,
IEntryAssemblyMetadata entryAssemblyMetadata)
{
_runtimeMinificationSettings = runtimeMinificationSettings ?? throw new ArgumentNullException(nameof(runtimeMinificationSettings));
_umbracoVersion = umbracoVersion ?? throw new ArgumentNullException(nameof(umbracoVersion));
_entryAssemblyMetadata = entryAssemblyMetadata ?? throw new ArgumentNullException(nameof(entryAssemblyMetadata));
}
private string CacheBusterValue
{
get
{
if (_cacheBusterValue != null)
{
return _cacheBusterValue;
}
// Assembly Name adds a bit of uniqueness across sites when version missing from config.
// Adds a bit of security through obscurity that was asked for in standup.
var prefix = _runtimeMinificationSettings.Value.Version ?? _entryAssemblyMetadata.Name ?? string.Empty;
var umbracoVersion = _umbracoVersion.SemanticVersion.ToString();
var downstreamVersion = _entryAssemblyMetadata.InformationalVersion;
_cacheBusterValue = $"{prefix}_{umbracoVersion}_{downstreamVersion}".GenerateHash();
return _cacheBusterValue;
}
}
public string GetValue() => CacheBusterValue;
}
}