浏览器缓存(.js 文件)存在多长时间
ASP.NET 4.0
如果我在页面中更新了 .js 文件,但是客户端浏览器似乎不会下载新的 .js 文件(由于浏览器缓存?)。哪些因素可能会影响旧的 .js 文件保留在客户端的 PC 上?有没有办法强制客户端下载新的 .js 文件?
编辑: 我还有一个问题——浏览器将 .js 缓存保留多长时间?它是由客户端浏览器设置控制的吗?
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 the 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,你的生产文件不应该经常改变,尝试在你的文件名上加上一个版本,比如jquery,新版本会生成一个新名称的文件,如下
所示:
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:
to:
让浏览器再次下载文件的一个简单方法是在 url 上添加查询字符串
A simple way to get the browser to download a file again is to add a querystring onto the url
一般来说,浏览器会根据请求的确切 URL(带有查询参数)缓存来自服务器的响应。当浏览器再次请求完全相同的 URL 时,将使用缓存的副本。 (当然有缓存策略,但我们假设一个简单的场景)
因此,要强制浏览器获取文件的新副本,您只需更改该文件的请求 url。您可以更改文件的名称,也可以保留名称,仅更改查询参数。
如果您开发页面并且可以更改它,那么您可以使用自动方式在页面上创建 javascript 链接
,其中 XXXX 以编程方式设置为 myfile 的 DateModified 信息的刻度。每个页面渲染中都包含 .js 。因此,每当此文件被修改时,输出脚本的链接就会自动更改为新值,并且浏览器被迫再次下载它。
例如,如果我有一个 funcs.js 文件,则渲染的输出将类似于
稍后,如果文件更改,渲染将更改为更改后的
modified查询参数将强制浏览器再次下载文件
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
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
Later, if the file is changed, the rendering will change to something like
the altered modified query parameter will force the browser to download the file again