Skip to content

Commit

Permalink
V9: Fix backoffice javascript loading slowly (#11714)
Browse files Browse the repository at this point in the history
* Update Smidge

* Add SmidgeRequestHelper to override the compression order

By default Smidge will always try and use Brotli (br), this compression is good in the the sense that it compresses the file quite a bit, but sadly it's super super slow, like 10 seconds for backoffice js slow.

* Remove unnecessary new keyword

Co-authored-by: nikolajlauridsen <[email protected]>
  • Loading branch information
nikolajlauridsen and nikolajlauridsen authored Dec 1, 2021
1 parent b5a895e commit 980cf00
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ public static IUmbracoBuilder AddRuntimeMinifier(this IUmbracoBuilder builder)
});

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>();
builder.Services.AddSmidgeNuglify();
builder.Services.AddSmidgeInMemory(false); // it will be enabled based on config/cachebuster

Expand Down
78 changes: 78 additions & 0 deletions src/Umbraco.Web.Common/RuntimeMinification/SmidgeRequestHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
using Microsoft.Net.Http.Headers;
using Smidge;
using Smidge.Models;

namespace Umbraco.Cms.Web.Common.RuntimeMinification
{
public class SmidgeRequestHelper : IRequestHelper
{
private RequestHelper _wrappedRequestHelper;

public SmidgeRequestHelper(IWebsiteInfo siteInfo)
{
_wrappedRequestHelper = new RequestHelper(siteInfo);
}

/// <inheritdoc/>
public string Content(string path) => _wrappedRequestHelper.Content(path);

/// <inheritdoc/>
public string Content(IWebFile file) => _wrappedRequestHelper.Content(file);

/// <inheritdoc/>
public bool IsExternalRequestPath(string path) => _wrappedRequestHelper.IsExternalRequestPath(path);

/// <summary>
/// Overrides the default order of compression from Smidge, since Brotli is super slow (~10 seconds for backoffice.js)
/// </summary>
/// <param name="headers"></param>
/// <returns></returns>
public CompressionType GetClientCompression(IDictionary<string, StringValues> headers)
{
var type = CompressionType.None;

if (headers is not IHeaderDictionary headerDictionary)
{
headerDictionary = new HeaderDictionary(headers.Count);
foreach ((var key, StringValues stringValues) in headers)
{
headerDictionary[key] = stringValues;
}
}

var acceptEncoding = headerDictionary.GetCommaSeparatedValues(HeaderNames.AcceptEncoding);
if (acceptEncoding.Length > 0)
{
// Prefer in order: GZip, Deflate, Brotli.
for (var i = 0; i < acceptEncoding.Length; i++)
{
var encoding = acceptEncoding[i].Trim();

CompressionType parsed = CompressionType.Parse(encoding);

// Not pack200-gzip.
if (parsed == CompressionType.GZip)
{
return CompressionType.GZip;
}

if (parsed == CompressionType.Deflate)
{
type = CompressionType.Deflate;
}

// Brotli is typically last in the accept encoding header.
if (type != CompressionType.Deflate && parsed == CompressionType.Brotli)
{
type = CompressionType.Brotli;
}
}
}

return type;
}
}
}
4 changes: 2 additions & 2 deletions src/Umbraco.Web.Common/Umbraco.Web.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
<PackageReference Include="NETStandard.Library" Version="2.0.3" />
<PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
<PackageReference Include="SixLabors.ImageSharp.Web" Version="1.0.4" />
<PackageReference Include="Smidge.Nuglify" Version="4.0.2" />
<PackageReference Include="Smidge.InMemory" Version="4.0.2" />
<PackageReference Include="Smidge.Nuglify" Version="4.0.3" />
<PackageReference Include="Smidge.InMemory" Version="4.0.3" />
<PackageReference Include="Dazinator.Extensions.FileProviders" Version="2.0.0" />
<PackageReference Include="Umbraco.Code" Version="1.2.0">
<PrivateAssets>all</PrivateAssets>
Expand Down

0 comments on commit 980cf00

Please sign in to comment.