0

I have an ASP.NET MVC 5 application, this application includes HTML, CSS, and Javascript files when I do some update in HTML, CSS or Javascript and deploy it in the server, to see that change I have to clear the cache in local PC.

This is a common question, but answers are not straight as I see.

So I wish to know, can I include a function to refresh the existing HTML, CSS and JS files using javascript approach. when the application load, gets the HTML, CSS and JS files as fresh copies. This is like clear the cache in the browser using javascript.

If cannot achieve this any other alternative?

Kelum
  • 1,551
  • 4
  • 20
  • 37
  • 1
    Possible duplicate - https://stackoverflow.com/questions/49547/how-to-control-web-page-caching-across-all-browsers – Luís P. A. Oct 18 '18 at 15:13
  • 1
    In order for us to help you out you will need to atleast show some attempt with your code, what error messages you recieved or whats not working. Otherwise can't really write down code for you. If you want to do more reading up - heres few things to look at: https://stackoverflow.com/questions/1011605/clear-the-cache-in-javascript – Shaze Oct 18 '18 at 15:13
  • @Shaze actually the first half of day I spent to find frontend solution for this? since I lost among article, asked from community – Kelum Oct 18 '18 at 15:20
  • How frequent does the data change in your page [project]? Is it a RESTful services interfaces your trying to achieve or is there some backend database ? – Shaze Oct 18 '18 at 15:22
  • just want to clear when after I update any of CSS or HTML or JS only – Kelum Oct 18 '18 at 15:31

1 Answers1

0

Since you're using MVC 5, this can automatically be handled for JS and CSS for you, if you use bundling and minification.

The request ... for the bundle ... contains a query string pair.... The query string ... has a value token that is a unique identifier used for caching. As long as the bundle doesn't change, the ASP.NET application will request the ... bundle using this token. If any file in the bundle changes, the ASP.NET optimization framework will generate a new token, guaranteeing that browser requests for the bundle will get the latest bundle.

Essentially, the ASP.NET application will handle the caching of JS and CSS for you.

You can read more about how this works and how to implement it on the MSDN article for Bundling and Minification.

I'm not sure what you mean by caching HTML. If you're using a .cshtml file, it shouldn't be cached by default as far as I know. However, you can specify on your controller how long to cache the results of an action for, such as in this post:

You can use the OutputCacheAttribute to control server and/or browser caching for specific actions or all actions in a controller.

If you provide my detail on how the HTML is being cached, I might be able to provide a more helpful answer.

Jack
  • 1,283
  • 12
  • 29