开发者

How long does browser cache (.js files) exist

ASP.NET 4.0

If I updated my .js file in a page, however it seems client's browser doesn't download the new .js file (due to browser cache?). What factors could affect th开发者_StackOverflowe old .js file remain on client's PC? Is there any way to force client to download new .js file?

EDIT: I still got another question -- How long does the browser keep the .js cache? Is it controlled on client browser setting?


well, your production file is not suposed to chage frequently, try to put a version on your file name, like jquery, a new version generates a file with new name like that:

/myfile.1.0.0.js

to:

/myfile.1.0.1.js


A simple way to get the browser to download a file again is to add a querystring onto the url

/myfile.js?v=1


In general, browsers will cache a responce from the server based on the exact url requested (with query parameters). When the browser asks for the exact same url again, the cached copy will be used. (of course there are caching strategies but let's assume the simple scenario)

So, to force the browser to get a new copy of your file, you just need to change the request's url for that file. You either change the name of the file, or you keep the name, and just change a query parameter.

If you develop the page and you can alter it then you can use an automatic way of creating the javascript link on the page

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

Where XXXX is programmaticaly set to the ticks of the DateModified information of myfile.js in every page render. So, whenever this files gets modified, the output script's link is automatically changed to the new value and the browser is forced to download it again.

for instance if I have a funcs.js file, the rendered output would be something like

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

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

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

the altered modified query parameter will force the browser to download the file again

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜