javascript 如何从服务器端强制刷新客户端js?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20114532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 17:46:04  来源:igfitidea点击:

How to force refresh client js from server side?

javascriptbrowser

提问by Allerson

Here is the case:

这是这种情况:

I get a js to monitor web ads.Because of the browser cache,when i update js on server side,js on client side will not be refreshed immediately.How could i force refresh client js as soon as i update js on server side?

我得到一个 js 来监控网络广告。由于浏览器缓存,当我在服务器端更新 js 时,客户端的 js 不会立即刷新。我如何在服务器端更新 js 后立即强制刷新客户端 js?

p.s. Add version number strategy is not useful in my case.

ps 添加版本号策略在我的情况下没有用。

采纳答案by Wyzard

You can use HTTP's cache-control mechanismsto control the browser's caching.

您可以使用 HTTP 的缓存控制机制来控制浏览器的缓存。

When serving a copy of your JS file, include an ETagand/or Last-Modifiedheader in the response. Also include a "Cache-Control: must-revalidate" header. This tells the browser that it must check back with the server every time, and it can send an If-None-Matchand/or If-Modified-Sinceheader in future requests to ask the server to send the file only if it's changed.

在提供 JS 文件的副本时,在响应中包含ETag和/或Last-Modified标头。还包括一个“ Cache-Control: must-revalidate”标头。这告诉浏览器每次都必须与服务器核对,并且它可以在以后的请求中发送If-None-Match和/或If-Modified-Since标头,要求服务器仅在文件更改时发送文件。

If you'd like to avoid the load of browsers checking with the server every time, and it's OK for the changes to not take effect immediately, you can also include a Dateheader with the current time and an Expiresheader set to some point in the future — maybe 12 or 24 hours. That allows the browser to use its cached copy for the specified amount of time before it has to check back with your server again.

如果您想避免每次都检查服务器的浏览器负载,并且更改不会立即生效,您还可以包含一个带有当前时间的Date标头和一个设置为某个点的Expires标头未来——也许是 12 或 24 小时。这允许浏览器在必须再次检查您的服务器之前在指定的时间内使用其缓存副本。

HTTP's cache-control features are pretty robust, but there are plenty of nuances, such as controls for intermediate caches (e.g. other systems betweenyour server and the user's browser). You'll want to read about caching in HTTPoverall, not just the specific header fields that I've mentioned.

HTTP 的缓存控制功能非常强大,但有很多细微差别,例如中间缓存的控制(例如您的服务器和用户浏览器之间的其他系统)。您需要阅读有关HTTP 缓存的整体内容,而不仅仅是我提到的特定标头字段。

回答by MD Sayem Ahmed

Simple strategy - add a version number as a query string to your js files, and change the number. This will cause the browsers to fetch your js files again -

简单的策略 - 将版本号作为查询字符串添加到您的 js 文件中,并更改编号。这将导致浏览器再次获取您的 js 文件 -

<script src="mysource.js?version=123"></script>

Whenever you change your script on the server, change this version number in the html too. Or better yet, apply a random number as the versionvalue every time you request this script.

每当您更改服务器上的脚本时,也要更改 html 中的此版本号。或者更好的是,version每次请求此脚本时应用一个随机数作为值。

回答by enenen

You can do this by changing the name of the file. Add some version number (could be like parameter, i.e. filename.js?v=time();for PHP for example) or just append some random numbers at the end of the filename.

您可以通过更改文件名来做到这一点。添加一些版本号(可以像参数,filename.js?v=time();例如对于 PHP)或者只是在文件名的末尾附加一些随机数。

Actually I'm not sure whether you can force the client to refresh this type of files. But when changing the file name you will force the browser to get the newest version.

实际上我不确定您是否可以强制客户端刷新此类文件。但是在更改文件名时,您将强制浏览器获取最新版本。