How long the browser cache is stored (.js files)

ASP.NET 4.0

If I updated my .js file on the page, however, it seems that the client browser is not loading the new .js file (due to browser cache?). What factors can affect the old .js file on the client PC? Is there a way to get the client to upload a new .js file?

EDIT: I have another question: how long does the browser keep the .js cache? Is it controlled in the client browser settings?

+4
source share
3 answers

An easy way to get the browser to download the file again is to add a url request

/myfile.js?v=1 
+7
source

well, your production file is not recommended to take risks often, try putting the version in your file name, for example jquery, the new version generates a file with this new name:

 /myfile.1.0.0.js 

in

 /myfile.1.0.1.js 
+8
source

In general, browsers will cache the response from the server based on the requested URL (with request parameters). When the browser again requests the same URL, a cached copy will be used. (of course, there are caching strategies, but let's say a simple scenario)

So, to get the browser to get a new copy of your file, you just need to change the request URL for this file. You either change the file name, or save the name, or simply change the query parameter.

If you are developing a page and you can change it, you can use the automatic way to create javascript links on the page

 <script type="text/javascript" src="/myfile.js?modified=XXXXX"> </script> 

Where XXXX is programmatically set by ticking the DateModified myfile.js information in each page rendering. Thus, whenever these files are changed, the script output link automatically changes to a new value, and the browser is forced to download it again.

for example, if I have a funcs.js file, then the output will look like

 <script type="text/javascript" src="/funcs.js?modified=43253452352342"> </script> 

Later, if the file is modified, the rendering will change to something like

 <script type="text/javascript" src="/funcs.js?modified=43253456789678"> </script> 

the changed request parameter is changed causes the browser to download the file again

+1
source

All Articles