Browser cache for static contents - asp.net-mvc

I am trying to update the configuration which effects the browser caching settings for static resources (js, css, images).
I have Sitecore CMS site and images uploaded in CMS, these images are cached in browser (status code = 200 (from cache), when i observed the network in Chrome browser), but my other resources like js and css which served from Website folder are not cached in browser, and the status code is 304 (which is server cache and there is a round trip required to check for any update.)
I have below config settings in web.config file:
<caching>
<profiles>
<add extension=".jpeg" policy="CacheUntilChange" kernelCachePolicy="DontCache" />
<add extension=".js" policy="CacheUntilChange" kernelCachePolicy="DontCache" />
<add extension=".png" policy="CacheUntilChange" kernelCachePolicy="DontCache" />
<add extension=".jpg" policy="CacheUntilChange" kernelCachePolicy="DontCache" />
<add extension=".css" policy="CacheUntilChange" kernelCachePolicy="DontCache" />
<add extension=".json" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" duration="00:00:30" />
</profiles>
</caching>
<staticContent>
<remove fileExtension=".woff2" />
<mimeMap fileExtension=".json" mimeType="application/json" />
<mimeMap fileExtension=".woff2" mimeType="application/x-font-woff2" />
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="30.00:00:00" />
</staticContent>
Now if i remove the caching-profiles entry, then the resources are cached in browser, i can see all the css, js status code as 200 (from cache) for the subsequent request.
My question here is, what is the difference here, why browser cache didn't worked even there is a staticContent settings are added. What is the impact if I remove the existing caching-profiles settings (will it impacts server cache?).
I saw some other links, which explains static cache settings, but i want to know the impact with the changes i did (removed caching-profiles)
Please let me know your inputs.
Below are the Response header details:
With caching-profiles:
Without caching-profiles:
Thanks,
Sharath

So, it appears you are configuring IIS output cache for .jpeg, .js, ..., within the web.config system.webServer/caching node (beware, .webServer, not .web).
Unfortunately, IIS output cache (and Asp.Net output cache too by the way) does also handle client caching, and so it interferes (badly) with your client caching settings. (It should be two separate matters in my opinion, but that is not the way IIS/Asp.Net output caches handle it.)
Your profiles do not set the location attribute, so they default to Server. With output cache semantic, this means "no client cache", thus the no-cache it adds in Cache-Control response header.
To avoid this, you may change your profile location to Any.
<profiles>
<add extension=".jpeg" location="Any" policy="CacheUntilChange" kernelCachePolicy="DontCache" />
<add extension=".js" location="Any" policy="CacheUntilChange" kernelCachePolicy="DontCache" />
<add extension=".png" location="Any" policy="CacheUntilChange" kernelCachePolicy="DontCache" />
<add extension=".jpg" location="Any" policy="CacheUntilChange" kernelCachePolicy="DontCache" />
<add extension=".css" location="Any" policy="CacheUntilChange" kernelCachePolicy="DontCache" />
<add extension=".json" location="Any" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" duration="00:00:30" />
</profiles>
But maybe should you simply not use IIS output-cache for .jpeg, .js, ...
From official IIS documentation:
Output caching is unnecessary for static files, such as HTML, JPG, or GIF files, ...
It has no benefit using IIS output cache for static files. (It may have benefits using an actual cache server such as varnish in front of your IIS, but emulating a cache server with IIS output cache has no benefit for static files.)
If you have some special cases URIs endings with static file extension but actually served dynamically by your application instead of directly corresponding to a file on disk (special case usually involving using rammfar which is a bad thing for application scalability; linked page gives some alternates by the way), better try enabling output-cache only for those URIs, by configuring it under
<configuration>
...
<system.webServer>
... <!-- not here! -->
</system.webServer>
...
<location path="yourDynamicImagesUriBasePath">
<system.webServer>
<caching>
... <!-- move it here -->
(I am not sure it works though, better test it of course.)

Related

Problem in hosting Dot net Core on windows server with IIS (nop commerce)

I want to host nopcommerce 4.10 on windows server IIS. I have installed .net core hosting bundle, .net Sdk and also set its environment variable c++ redistributable package is also installed. But I am still getting this screen. I have also enables stdout but does not getting any log.
I've tried many solutions from forums and all other site but still getting same issue.
Web.config
<configuration>
<system.webServer>
<modules>
<!-- Remove WebDAV module so that we can make DELETE requests -->
<remove name="WebDAVModule" />
</modules>
<handlers>
<!-- Remove WebDAV module so that we can make DELETE requests -->
<remove name="WebDAV" />
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule"
resourceType="Unspecified" />
</handlers>
<!-- When deploying on Azure, make sure that "dotnet" is installed and the
path to it is registered in the PATH environment variable or specify the
full path to it -->
<aspNetCore requestTimeout="23:00:00" processPath="%LAUNCHER_PATH%"
arguments="%LAUNCHER_ARGS%" forwardWindowsAuthToken="false"
stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" startupTimeLimit="3600"
/>
<httpProtocol>
<customHeaders>
<remove name="X-Powered-By" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
%LAUNCHER_PATH% and %LAUNCHER_ARGS% did not get replaced, it should be something like
<aspNetCore requestTimeout="23:00:00" processPath="C:\Program Files (x86)\dotnet\dotnet.exe" arguments=".\Nop.Web.dll" forwardWindowsAuthToken="false" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" startupTimeLimit="3600" />

neo4j Remote guide error: TypeError: NetworkError when attempting to fetch resource

Just installed the new version (community edition 3.4.9) and I have this configuration at neo4j.conf:
browser.remote_content_hostname_whitelist=*
browser.post_connect_cmd=play http://127.0.0.1:80/egov-guide/introduction.html;
The idea is to run a specific guide introducing the database. with previous versions runs well, but with this one, I get the following error:
Remote guide error: TypeError: NetworkError when attempting to fetch resource.
Just to complete: if I switch to 3.4.7 or 3.4.8 the educational guide is accessible. This last one is available through IIS and the web.config is like that:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="GET, PUT, POST, DELETE, HEAD, OPTIONS" />
<add name="Access-Control-Allow-Credentials" value="true" />
<add name="Access-Control-Allow-Headers" value="X-Requested-With, origin, content-type, accept" />
</customHeaders>
</httpProtocol>
<staticContent>
<mimeMap fileExtension=".grass" mimeType="neo4j/style" />
</staticContent>
</system.webServer>
</configuration>
I don't know if I'm doing something wrong
The neo4j guys solved the problem. They told that we must send Pragma and Cache-Control headers with the requests since neo4j-browser 3.4.9.
So the web.config should be completed, like this:
<add name="Access-Control-Allow-Headers" value="X-Requested-With, origin, content-type, accept, Pragma, Cache-Control" />

cache static images by keeping Cache-Control set to no cache in asp.net mvc

I have images in my ~/Content/Images folder. And these images are used in Layout and other pages. When i request a page "Contact", it loads images referenced on that page and i can see requests in IIS log for each image load. This is ok for the first request. and for 2nd and further requests to contact page, i do not want these images to be requested from server again, want them to be load from cache.
I don’t want to change security settings in my web.config
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Cache-Control" value="no-cache, no-store" />
<add name="Pragma" value="no-cache" />
<add name="Expires" value="-1" />
</customHeaders>
</httpProtocol>
</system.webServer>
I am trying to add output cache setting for images as
<system.webServer>
<caching>
<profiles>
<add extension=".gif" policy="CacheUntilChange" kernelCachePolicy="DontCache" location="Client" />
<add extension=".jpg" policy="CacheUntilChange" kernelCachePolicy="DontCache" location="Client" />
<add extension=".jpeg" policy="CacheUntilChange" kernelCachePolicy="DontCache" location="Client" />
<add extension=".png" policy="CacheUntilChange" kernelCachePolicy="DontCache" location="Client" />
</profiles>
</caching>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="1.00:00:00" />
</staticContent>
</system.webServer>
But above setting still sending a requests for images each time I am refreshing the page.
do not want session values to be cached so I set cache-control to no-cache.
I want to stop these requests for static images until I change it.
What i am missing here?

IIS default document not found

After I manually set up a virtual directory in IIS on an Umbraco website (that uses MVC), I get a 404 not found error when attempting to browse the URL:
mydomain/myvirtualdirectory/
I have the file index.html in the physical directory that the virtual directory points to and I configured IIS to look for index.html as the default document for the virtual directory.
Turns out that Umbraco was 'intefering'. I needed to add a web.config under the physical folder that the virtual directory points to (the 'ExtensionlessUrlHandler' bit is switching off functionality applied by Umbraco, which was the main problem). This can all be generated via the UI in IIS, or can just be added manually.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<defaultDocument>
<files>
<clear />
<add value="index.html" />
<add value="Default.htm" />
<add value="Default.asp" />
<add value="index.htm" />
<add value="iisstart.htm" />
<add value="default.aspx" />
</files>
</defaultDocument>
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
</handlers>
</system.webServer>
</configuration>

asp mvc IIS7 less/sass empty file

I'm trying to use less in my new project. I've installed dotLess and System.Web.Optimization.Less packeges (as described in Yet Another "Add LESS to your ASP.NET MVC Project" Post) and locally everything works fine. But when I publish project IIS7 server responds with empty css file /Content/Main/site?v=
Here is my web.config
...
<configSections>
<section name="dotless" type="dotless.Core.configuration.DotlessConfigurationSectionHandler, dotless.Core" />
</configSections>
<system.web>
<httpHandlers>
<add path="*.less" verb="GET" type="dotless.Core.LessCssHttpHandler, dotless.Core" />
</httpHandlers>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
<add name="dotless" path="*.LESS" verb="*" type="dotless.Core.LessCssHttpHandler,dotless.Core" resourceType="File" preCondition=""/>
</handlers>
</system.webServer>
<dotless minifyCss="false" cache="true" web="false" />
And my BundleConfig.cs
bundles.Add(new LessBundle("~/Content/Main/site").Include("~/Content/Main/Site.less"));
When I append css extension to my Site.less file (Site.less.css) server responds with content (/Content/Main/site?v=K-FFpFNtIXPUlQamnX3qHX_A5r7TM2xbAgcuEmpm3O41) but it still pure less with all its variables and etc.
The same with Bundle Transformer: Sass and SCSS
What have I missed?
As you are getting
/Content/Main/site?v=K-FFpFNtIXPUlQamnX3qHX_A5r7TM2xbAgcuEmpm3O41
then this means that the bundling is working correctly. Therefore there is probably something in Site.less that is causing the problem. One thing to watch out for is relative urls. For example if you have an image in your css
.bacon {
background-image: url(bacon.png);
}
The bundle will look for this with ~/Content/Main taken from the name of the bundle. (/site is fine and can be anything, it's just the name of the bundle)
The first thing to try is take out any imports, images etc and see if it bundles correctly. You can do this on your local dev build by enabling optimizations in the your bundle config.
BundleTable.EnableOptimizations = true
Another way to solve this is to remove dependency on System.Web.Optimization.Less and dotLess and use free Visual Studio Extension https://github.com/madskristensen/WebCompiler for precompilation of less to css (with and without minification).

Resources