有没有办法使用js清除浏览器缓存,至少我的域相关文件?

时间:2022-11-19 12:01:55

I have a list of js files, css and images which doesn't need to load from server every time, but if there is any update in files or bug fixes, only during that time I want to replace the files from browser cache, I know there is no access to browser cache, but is there any other ways to do so? My application will be used by specific users (known people), where I can install any program in their system, can anybody suggest me efficient way to do so? I don't want to load the files every time from server by setting 'no-cache'.

我有一个js文件,css和图像的列表,不需要每次都从服务器加载,但如果文件中有任何更新或错误修复,只有在那段时间我想从浏览器缓存中替换文件,我知道没有访问浏览器缓存的权限,但有没有其他方法可以这样做?我的应用程序将由特定用户(已知人员)使用,我可以在他们的系统中安装任何程序,任何人都可以建议我这样做的有效方法吗?我不想通过设置'no-cache'每次从服务器加载文件。

4 个解决方案

#1


4  

A commonly-used trick is to set the cache for the files to last for ages, so that they aren't requested again. However, when you want them to be updated, you can append a timestamp to the filename after a question mark. EG:

一个常用的技巧是将文件的缓存设置为持续多年,以便不再请求它们。但是,当您希望更新它们时,可以在问号后面添加时间戳到文件名。例如:

<link rel="stylesheet" href="style.css?123211212"/>

Every time the number changes, the browser thinks it's a different file and will re-download it. If the number doesn't change, then it uses the cached version.

每次数字更改时,浏览器都认为它是一个不同的文件,并会重新下载。如果数字没有改变,那么它使用缓存版本。

#2


3  

The most effective way to force the browser to refresh certain files at certain times is to add an arbitrary extra query string to the link:

强制浏览器在特定时间刷新某些文件的最有效方法是向链接添加任意额外查询字符串:

<script type="text/javascript" src="http://mywebsite.com/js/scripttoload.js"></script>

then change to:

然后改为:

 <script type="text/javascript" src="http://mywebsite.com/js/scripttoload.js?V=2"></script>

Next time the page is requested the browser will think this is a new file. There are loads of other ways with headers etc but this works well

下次请求页面时,浏览器会认为这是一个新文件。标题等有很多其他方法,但这很有效

#3


2  

No, there isn't.

不,没有。

Javascript doesn't have access to the cache - the browser doesn't expose this information to the javascript engine.

Javascript无权访问缓存 - 浏览器不会将此信息公开给javascript引擎。

#4


0  

What I do is, as part of the build process, rename all the statically referenced files to something involving their md5 hash. Then I set the headers so that they're cached for the max possible time. As soon as they change, they get a new name, so there's never an issue.

我所做的是,作为构建过程的一部分,将所有静态引用的文件重命名为涉及其md5哈希的内容。然后我设置标题,以便它们被缓存最长可能的时间。一旦他们改变,他们就会得到一个新名字,所以从来没有问题。

#1


4  

A commonly-used trick is to set the cache for the files to last for ages, so that they aren't requested again. However, when you want them to be updated, you can append a timestamp to the filename after a question mark. EG:

一个常用的技巧是将文件的缓存设置为持续多年,以便不再请求它们。但是,当您希望更新它们时,可以在问号后面添加时间戳到文件名。例如:

<link rel="stylesheet" href="style.css?123211212"/>

Every time the number changes, the browser thinks it's a different file and will re-download it. If the number doesn't change, then it uses the cached version.

每次数字更改时,浏览器都认为它是一个不同的文件,并会重新下载。如果数字没有改变,那么它使用缓存版本。

#2


3  

The most effective way to force the browser to refresh certain files at certain times is to add an arbitrary extra query string to the link:

强制浏览器在特定时间刷新某些文件的最有效方法是向链接添加任意额外查询字符串:

<script type="text/javascript" src="http://mywebsite.com/js/scripttoload.js"></script>

then change to:

然后改为:

 <script type="text/javascript" src="http://mywebsite.com/js/scripttoload.js?V=2"></script>

Next time the page is requested the browser will think this is a new file. There are loads of other ways with headers etc but this works well

下次请求页面时,浏览器会认为这是一个新文件。标题等有很多其他方法,但这很有效

#3


2  

No, there isn't.

不,没有。

Javascript doesn't have access to the cache - the browser doesn't expose this information to the javascript engine.

Javascript无权访问缓存 - 浏览器不会将此信息公开给javascript引擎。

#4


0  

What I do is, as part of the build process, rename all the statically referenced files to something involving their md5 hash. Then I set the headers so that they're cached for the max possible time. As soon as they change, they get a new name, so there's never an issue.

我所做的是,作为构建过程的一部分,将所有静态引用的文件重命名为涉及其md5哈希的内容。然后我设置标题,以便它们被缓存最长可能的时间。一旦他们改变,他们就会得到一个新名字,所以从来没有问题。