如何确保浏览器不缓存我的JS / CSS [英] How to ensure broswer does not cache my JS/CSS

查看:231
本文介绍了如何确保浏览器不缓存我的JS / CSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

css文件缓存

我想确保用户始终获得最新版本的我的代码。

I want to ensure users always get the latest version of my code.

但是设置它以便浏览器不缓存文件是很糟糕的,因为它需要在每个页面视图中重新加载。我想确保它不使用以前版本的缓存副本。例如。使用版本1.3站点的版本1.2 JS的缓存副本。

However setting it up so that browsers do not cache the files is bad as it would need to be loaded fresh every page view. I want to ensure it doesn't use a cached copy from a previous release. Eg. using a cached copy of the release 1.2 JS with version 1.3 site.

我见过的一种方式涉及更改每个版本的文件名并链接到每个版本的不同文件名发布(例如, myscript1.3.js)这似乎很费力,而且有点怪异。

One way I have seen involves changing the filename on each release and linking to that different file name on each release (eg 'myscript1.3.js') This seems a lot of effort and slightly hacky.

还有更好的方法吗?

推荐答案

完美的解决方案,甚至可以说是最好的解决方案。

That's a perfectly good solution, and I would even go so far as to say that it's the best.

您还可以使用查询参数来指示新版本(?v = 1,?v = 2,...)。但这可能会抛出一些公共代理(它们可能不会缓存文件)。详情请参阅此处: https://developers.google.com/speed/ docs / best-practices / caching#LeverageProxyCaching

You could also use query parameters to indicate a new version (?v=1, ?v=2, ...). But this could throw off some public proxies (they might not cache the files). See here for details: https://developers.google.com/speed/docs/best-practices/caching#LeverageProxyCaching

当然,这需要您付出一些努力。但是您可以在部署过程中自动完成所有这些操作(如何操作完全取决于您的设置,因此,如果您不了解您的项目,我将无法为您提供解决方案)。

Off course this will require some effort on your part. But you can automate all of this in your deploy process (how to do that would depend entirely on your setup, so I can't give you a solution without knowing a lot more about your project).

这篇关于如何确保浏览器不缓存我的JS / CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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