Add Cache-Control header for static files (*.js, *.css, *.woff2, etc.)
How can I set the Cache-Control header for static files (*.js, *.css, *.woff2, etc.)? I managed to set the header by adding the following code to the Configure() method of Startup.cs:
However, this only works when add this code before the app.UseUmbraco(), but when I do that it breaks ImageSharp. When I add this code after app.UseUmbraco() the header is not being added to the response.
Adding the following code to Startup.cs before app.UseUmbraco() seems to do the trick. Need to do some more testing to see if this doesn't break anything.
I tried your solution but is not working for static files (fonts, etc..).
I am now testing with replacing app.UseStaticFiles on Startup.cs with the following code, seems to work
Doesn’t Umbraco already set its own cache-control header for /umbraco and package files etc.? Or are all the backoffice file caches properly invalidated using querystring or based on another cache busting technique? I would think you don’t want to enforce a very strict cache protocol for the backoffice files to prevent issues after updating Umbraco or one of the packages.
I also change the max-age for frontend files to improve page load and PageSpeed Insights scores. But for the backoffice I’d rather use no cache than adding a cache protocol that could potentially cause issues after upgrading Umbraco of packages.
Also, did you test to see if your solution does not interfere with ImageSharp? If it doesn't I'm curious to hear when you're calling UseStaticFiles() in Startup.cs.
Having a hell of a time trying to get both ImageSharp and static file cache-control working at the same time.
I'm using Joao's app.UseStaticFiles code above in StartUp.cs. However if it's placed before app.UseUmbraco() then ImageSharp breaks and images can no longer be manipulated via query string. If it's placed after app.UseUmbraco(), then ImageSharp works again but all static files fail to be cached.
I tried those path.StartsWith and EndWith conditionals but that didn't seem to make any difference. What is the best way to configure StartUp.cs so all files as well as any images not being manipulated by ImageSharp are cached?
Yeah I finally tried that last night and it seems to work OK. I came to the conclusion we just have to throw out UseStaticFiles() completely in order to utilize crops and ImageSharp manipulation while maintaining content-cache.
I was having the same issue. My solution was to use the app.UseWhen() conditional mapper, to exclude the media and umbraco paths. This must be done before the app.UseUmbraco()
Hey Pascal, I just tested your solution on Umbraco 10 and it works great! Thank you!
The only problem I have right now is with Smidge js/css files. For some reason Smidge is overwriting whatever cache header I set with:
cache-control: public, max-age=864000, s-maxage=864000
Anyone has figured out a way to change max-age on Smidge generated files?
thanks for your input, I didn't test it as in the meantime we just applied another solution. Basically we forked Smidge and did the change in the source code to hard code the cache header to whatever we want.
That is happening for me with Umbraco 13.
Basically when UseStaticFiles is enabled in statup.cs the Image Processor does nothing.
Pascal's work around did the trick but I am not sure thats the right solution. This would mean that static caching simply will not be applied to add images within the media folder.
Surely that cant be Umbraco's recommended way around this?
I'm currently using the following solution on an Umbraco 13 site and everything seems to be working just fine. The Cache-Control header is being added to CSS, JS, fonts, images and /media files.
// Add Cache-Control header to static files
builder.Services.Configure<StaticFileOptions>(options =>
{
options.OnPrepareResponse = ctx =>
{
// Skip files from Umbraco backoffice / packages
if (ctx.Context.Request.Path.StartsWithSegments("/umbraco") || ctx.Context.Request.Path.StartsWithSegments("/App_Plugins"))
return;
// Set specific cache-control response header
var responseHeaders = ctx.Context.Response.GetTypedHeaders();
var cacheControl = responseHeaders.CacheControl ?? new CacheControlHeaderValue();
cacheControl.Public = true;
cacheControl.MaxAge = TimeSpan.FromSeconds(31536000);
responseHeaders.CacheControl = cacheControl;
};
});
Add Cache-Control header for static files (*.js, *.css, *.woff2, etc.)
How can I set the Cache-Control header for static files (*.js, *.css, *.woff2, etc.)? I managed to set the header by adding the following code to the Configure() method of Startup.cs:
However, this only works when add this code before the app.UseUmbraco(), but when I do that it breaks ImageSharp. When I add this code after app.UseUmbraco() the header is not being added to the response.
Adding the following code to Startup.cs before app.UseUmbraco() seems to do the trick. Need to do some more testing to see if this doesn't break anything.
I tried your solution but is not working for static files (fonts, etc..). I am now testing with replacing app.UseStaticFiles on Startup.cs with the following code, seems to work
I think it’s safer to exclude the /umbraco folder.
Why do you think so? Static files used by the backoffice aren't supposed to change too, isn't it?
Doesn’t Umbraco already set its own cache-control header for /umbraco and package files etc.? Or are all the backoffice file caches properly invalidated using querystring or based on another cache busting technique? I would think you don’t want to enforce a very strict cache protocol for the backoffice files to prevent issues after updating Umbraco or one of the packages.
I also change the max-age for frontend files to improve page load and PageSpeed Insights scores. But for the backoffice I’d rather use no cache than adding a cache protocol that could potentially cause issues after upgrading Umbraco of packages.
Also, did you test to see if your solution does not interfere with ImageSharp? If it doesn't I'm curious to hear when you're calling
UseStaticFiles()
in Startup.cs.Hey guys,
Having a hell of a time trying to get both ImageSharp and static file cache-control working at the same time.
I'm using Joao's app.UseStaticFiles code above in StartUp.cs. However if it's placed before app.UseUmbraco() then ImageSharp breaks and images can no longer be manipulated via query string. If it's placed after app.UseUmbraco(), then ImageSharp works again but all static files fail to be cached.
I tried those path.StartsWith and EndWith conditionals but that didn't seem to make any difference. What is the best way to configure StartUp.cs so all files as well as any images not being manipulated by ImageSharp are cached?
Did you try this solution: https://our.umbraco.com/forum/umbraco-9/107501-add-cache-control-header-for-static-files-starjs-starcss-starwoff2-etc#comment-334432 ?
For me this works just fine on v9.5.0 and v10.0.1.
Yeah I finally tried that last night and it seems to work OK. I came to the conclusion we just have to throw out UseStaticFiles() completely in order to utilize crops and ImageSharp manipulation while maintaining content-cache.
I was having the same issue. My solution was to use the app.UseWhen() conditional mapper, to exclude the media and umbraco paths. This must be done before the app.UseUmbraco()
I hope that helps.
Hey Pascal, I just tested your solution on Umbraco 10 and it works great! Thank you!
The only problem I have right now is with Smidge js/css files. For some reason Smidge is overwriting whatever cache header I set with: cache-control: public, max-age=864000, s-maxage=864000
Anyone has figured out a way to change max-age on Smidge generated files?
Hi João
This worked for me
Hello Vasco,
thanks for your input, I didn't test it as in the meantime we just applied another solution. Basically we forked Smidge and did the change in the source code to hard code the cache header to whatever we want.
Did anyone find a solution for this?
That is happening for me with Umbraco 13. Basically when UseStaticFiles is enabled in statup.cs the Image Processor does nothing.
Pascal's work around did the trick but I am not sure thats the right solution. This would mean that static caching simply will not be applied to add images within the media folder.
Surely that cant be Umbraco's recommended way around this?
Hi David
Have you tried this which has been documented now: https://docs.umbraco.com/umbraco-cms/reference/response-caching#modify-the-cache-control-header-for-static-files
and furthermore the
Cache-Control
header for images processed by ImageSharp:https://docs.umbraco.com/umbraco-cms/reference/response-caching#modify-the-cache-control-header-for-imagesharp.web
I'm currently using the following solution on an Umbraco 13 site and everything seems to be working just fine. The Cache-Control header is being added to CSS, JS, fonts, images and /media files.
This needs to be called before
builder.Build()
.Arjan,
Is this in the ConfigureServices method or the Configure method. Can you share a little more of the setup.cs file.
I want to double check exactly where you have this
I'm only using a Program.cs, see this blog post.
The Cache-Control code is inside my custom
RegisterApplicationServices()
extension method:is working on a reply...