Once the max-age duration we specified has expired, or after the browser evicts the files from its cache, we'll be back to making requests to the server, but until then we can see a massive improvement. Our index page still takes 1.58s to load, but as you can see, all our static files are loaded from the cache, which means no requests to our server, and consequently no latency! We're all done in 1.61s instead of the 4.17s we had previously. If we run our high-latency scenario again, we can see our results in action: In this case, we marked the response as public as we want intermediate caches between our server and the user to store the cached file too. You can read up on the details of the various associated cache headers here. In the example provided above, we are setting the Cache-Control header (using the constant values defined in ) to cache our files for 24 hours. If set, the Action is called before each successful response, whether a 200 or 304 response, but it won't be called if the file was not found (and instead returns a 404). It is passed a single parameter, a StaticFileResponseContext, which contains the current HttpContext and also an IFileInfo property representing the current file. This action can be used to specify any additional processing that should occur before a response is sent. One of the overloads of UseStaticFiles takes a StaticFileOptions parameter, which contains the property OnPrepareResponse. When you create a new ASP.NET Core project from the default template, you will find the StaticFileMiddleware is added early in the middleware pipeline, with a call to AddStaticFiles() in Startup.Configure(): public void Configure ( IApplicationBuilder app ) ) Adding Caching to the StaticFileMiddleware in IIS or Nginx), or use a third party provider like CloudFlare. You could also consider adding caching at the reverse proxy level (e.g. You can also use the ResponseCacheAttribute in MVC to decorate Controllers and Actions if you are returning data which is safe to cache. Note that this is not the only way to add cache headers to your site. I'll also show how you can add a version tag to your file links, to ensure you don't inadvertently serve stale data. In this post I'll show how you can add headers to the files served by the StaticFileMiddleware to increase your site's performance. The static files are typically fixed at the time of publish, and so are perfect candidates for caching. in ASP.NET Core, the HTML generated by your Razor templates) and static files (CSS stylesheets, JS, images etc). A typical site serves both dynamically generated content (e.g. In most cases you will find that a significant proportion of the requests to your site can be cached. By allowing caching, your application never sees these additional requests and never has to allocate resources to process them, so it is more available for requests that cannot be cached. One common use case for this is to add caching headers.Īllowing clients and CDNs to cache your content can have a massive effect on your application's performance. Thanks to the ASP.NET Core middleware pipeline, it is relatively simple to add additional HTTP headers to your application by using custom middleware.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |