In v13 my images are loading with a cache-control max-age=604800 (aka 7 days). I’m getting dinged for it on the Google Lighthouse performance score. So I wanted to up the max-age.
I found some settings in appsettings that I thought would do the trick, but it doesn’t seem that these are working. Do I have these settings implemented correctly?
@LuukPeters the default for ImagingCacheSettings > BrowserMaxAge is 7 days and that’s the same max-age I’m seeing on all of my images rendered by Umbraco.
if you don’t want to do in outboundrules.. as can’t always use them… (slight update from Response Caching | Umbraco CMS)
But for imagesharp cache, you should follow the app setting mentioned here too. Umbraco:CMS:Imaging:Cache:BrowserMaxAge
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Headers;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Microsoft.Net.Http.Headers;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Extensions;
using IHostingEnvironment = Umbraco.Cms.Core.Hosting.IHostingEnvironment;
namespace Umbraco.Docs.Samples.Web.Tutorials;
public class ConfigureStaticFileOptions : IConfigureOptions<StaticFileOptions>
{
// These are the starting segments of the paths we want to cache
private static readonly HashSet<string> _cachedFilePaths = new(StringComparer.OrdinalIgnoreCase)
{
"/css","/styles","/scripts","/js","/images","/fonts", "/app_plugins"
};
private readonly string _backOfficePath;
private readonly bool _isDevelopmentEnv;
public ConfigureStaticFileOptions(IOptions<GlobalSettings> globalSettings, IHostingEnvironment hostingEnvironment, IWebHostEnvironment env)
{
_backOfficePath = globalSettings.Value.GetBackOfficePath(hostingEnvironment);
_isDevelopmentEnv = env.IsDevelopment();
}
public void Configure(StaticFileOptions options)
=> options.OnPrepareResponse = ctx =>
{
// if we are in development
if (_isDevelopmentEnv)
{
return;
}
// Exclude Umbraco backoffice assets
if (ctx.Context.Request.Path.StartsWithSegments(_backOfficePath))
{
return;
}
if (_cachedFilePaths.Any(s => ctx.Context.Request.Path.StartsWithSegments(s, StringComparison.OrdinalIgnoreCase)))
{
ResponseHeaders headers = ctx.Context.Response.GetTypedHeaders();
// Update or set Cache-Control header
CacheControlHeaderValue cacheControl = headers.CacheControl ?? new CacheControlHeaderValue();
cacheControl.Public = true;
cacheControl.MaxAge = TimeSpan.FromDays(30);
headers.CacheControl = cacheControl;
}
};
public class ResponseCacheingComposer : IComposer
{
public void Compose(IUmbracoBuilder builder)
=> builder.Services.AddTransient<IConfigureOptions<StaticFileOptions>, ConfigureStaticFileOptions>();
}
}