更新缓存的图像/css/js,而无需重命名 [英] Updating cached images/css/js without renaming

查看:53
本文介绍了更新缓存的图像/css/js,而无需重命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对人们如何处理浏览器缓存中更新image/css/js感兴趣.

I'm interested how people deal with updating images/css/js with regards to the the browser cache.

使用mod_expires并具有将来的到期日等显然很不错-但是,如果您不想一直重命名文件,那么如何更新这些文件?

It's obviously good to use mod_expires and have a future expiry etc - but how can you then update those files if you don't want to rename the file all the time?

有没有人拥有版本号不错的花招,这不会冒着浏览器(或代理)不想缓存文件的风险,但仍然可以保证用户可以在更新后看到新版本?

Does anyone have any cool tricks with a version number which will not risk the browser (or proxy) not wanting to cache the file, but will still guarantee the user will see the new version if it's updated?

推荐答案

我将添加带有最后一次文件修改时间戳的查询字符串参数:

I would add a query string parameter with the timestamp of the last file modification:

http://example.com/script.js?v=123456

或者,您可以在应用程序中定义一个全局变量并将其用作时间戳.当您更改其中一个文件时,只需增加该变量即可.这意味着所有资源将同时失效,但这可能是比读取文件时间戳更简单的解决方案.

Alternatively, you can define a global variable in your application and use it as timestamp. When you change one of the files, just increment that variable. That means that all your resources will be invalidated at the same time, but that may be a simpler solution than reading the timestamp of the file.

这篇关于更新缓存的图像/css/js,而无需重命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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