什么时候浏览器自动外部JavaScript中清除缓存文件? [英] When does browser automatically clear cache of external JavaScript file?

查看:128
本文介绍了什么时候浏览器自动外部JavaScript中清除缓存文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有具有在任何时间正在编辑的可能性一个JavaScript资源。一旦它被编辑我希望它传播到用户的浏览器比较快(如可能为15分钟左右),但是,这种资源正在编辑的频率并不多见(也许2个月)。

我宁愿资源将在浏览器缓存,因为它会经常检索,但我也很喜欢缓存以获取在浏览器上以半固定的间隔复位。

我知道我可以通过无缓存头,当我要求的资源,但我想知道当缓存将自动重置本身在浏览器上,如果我不通过无缓存。

我想这将对于每个浏览器是独立的,但我不知道。

我试图谷歌这一点,但最让我找到了命中为有关清除浏览器的缓存... ...这是不是我要找的。

解决方案

您可以通过版本字符串作为GET参数到脚本标记的URL。参数将不会由静态JavaScript文件进行评估,但强制浏览器来获取新版本。

如果您不想每次编辑你可以计算它基于文件系统的时间戳或您的颠覆提交号码的来源时指定版本字符串:

 <脚本的src =/ TIME_STAMP的script.js = 1224147832156?类型=文/ JavaScript的>< / SCRIPT>
<脚本的src =/的script.js svn_version = 678?类型=文/ JavaScript的>< / SCRIPT>

I have a JavaScript resource that has the possibility of being edited at any time. Once it is edited I would want it to be propagated to the user's browser relatively quickly (like maybe 15 minutes or so), however, the frequency of this resource being editing is few and far between (maybe 2 a month).

I'd rather the resource to be cached in the browser, since it will be retrieved frequently, but I'd also like the cache to get reset on the browser at a semi-regular interval.

I know I can pass a no-cache header when I request for the resource, but I was wondering when the cache would automatically reset itself on the browser if I did not pass no-cache.

I imagine this would be independent for each browser, but I'm not sure.

I tried to Google this, but most of the hits I found were about clearing the browser's cache... which isn't what I'm looking for.

解决方案

You may pass a version string as a get parameter to the URL of your script tag. The parameter won't be evaluated by the static JavaScript file but force the browser to get the new version.

If you do not want to assign the version string every time you edited the source you may compute it based on the file system time stamp or your subversion commit number:

<script src="/script.js?time_stamp=1224147832156" type="text/javascript"></script>
<script src="/script.js?svn_version=678" type="text/javascript"></script>

   

这篇关于什么时候浏览器自动外部JavaScript中清除缓存文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆