积极的JavaScript缓存 [英] Aggressive JavaScript caching

查看:53
本文介绍了积极的JavaScript缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,即我对HTML文件中引用的一些JavaScript文件进行了更改,但浏览器看不到这些更改。即使Web服务器具有较新的版本,它也保留在浏览器中缓存的副本。

I've run into a problem where I make changes to a few JavaScript files that are referenced in an HTML file, but the browser doesn't see the changes. It holds onto the copy cached in the browser, even though the web server has a newer version.

直到我强制浏览器清除缓存后,我才能看到更改

Not until I force the browser to clear the cache do I see the changes.

这是Web服务器配置吗?我是否需要将JavaScript文件设置为从不缓存?我在 Google Web工具包,它们在每次进行更新时实际上都会创建一个 JavaScript文件名。我认为这是为了防止代理和浏览器保留具有相同名称的旧版本的JavaScript文件。

Is this a web-server configuration? Do I need to set my JavaScript files to never cache? I've seen some interesting techniques in the Google Web Toolkit where they actually create a new JavaScript file name any time an update is made. I believe this is to prevent proxies and browsers from keeping old versions of the JavaScript files with the same names.

是否在某处列出了最佳做法?

Is there a list of best practices somewhere?

推荐答案

我们将产品内部版本号附加到所有Javascript(和CSS等)的末尾,如下所示:

We append a product build number to the end of all Javascript (and CSS etc.) like so:

<script src="MyScript.js?4.0.8243">

浏览器会忽略问号后的所有内容,但升级会导致新的URL,这意味着重新加载缓存。

Browsers ignore everything after the question mark but upgrades cause a new URL which means cache-reload.

这还有一个额外的好处,就是您可以设置表示永不缓存!的HTTP标头。

This has the additional benefit that you can set HTTP headers that mean "never cache!"

这篇关于积极的JavaScript缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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