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.
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.
is working on a reply...