如何强制浏览器重新加载缓存的URL而不更改URL [英] How to force the browser to reload a cached URL without changing the URL
问题描述
发生意外,并且某个HTML页面被设置为由浏览器缓存。我们不希望更改该网页的网址,因为其他各方依赖该网址(包括SEO)。
有没有办法让尽可能多的客户端重新加载那个页面?客户经常在网站上浏览时访问该页面,所以我们经常能够在他们遇到问题页面之前向他们发送一些新的JavaScript(或任何其他资源)。也许有一个JavaScript技巧来强制重新加载?
下面的代码片段将使URI的页面的缓存条目无效至 uriOfCachedPage
:
var xhr = new XMLHttpRequest();
xhr.open(GET,uriOfCachedPage,true);
xhr.setRequestHeader(Cache-Control,max-age = 0);
xhr.send();
您需要将此代码注入脚本或HTML页面,以便用户可能获得。
更多信息请查看 XMLHttpRequest 页面和请求缓存控制指令<在RFC 7232超文本传输协议(HTTP / 1.1)中的/ a>部分:缓存。
您也可以存储 cachedPageReloaded
cookie或localStorage中的标志,并且只有在之前未使无效时才使缓存项无效。检查 Document.cookie 和 MDN上的Window.localStorage 页面。
An accident happened and a certain HTML page was set to be cached by browsers. We do not want to change the URL of that page because other parties rely on that URL (including SEO).
Is there a way to make as many clients as possible reload that page? Clients are often coming to that page when browsing around on the site so we are often able to send them some fresh JavaScript (or any other resource) before they hit the problematic page. Maybe there is a JavaScript trick to force a reload?!
Code snippet below will invalidate cache entry of the page with URI equal to uriOfCachedPage
:
var xhr = new XMLHttpRequest();
xhr.open("GET", uriOfCachedPage, true);
xhr.setRequestHeader("Cache-Control", "max-age=0");
xhr.send();
You need to inject this code to script or HTML page that your users will probably get.
For more information check XMLHttpRequest page on MDN and Request Cache-Control Directives section in RFC 7232 Hypertext Transfer Protocol (HTTP/1.1): Caching.
You also can store cachedPageReloaded
flag in cookies or localStorage and invalidate cache entry only if it wasn't invalidated before. Check Document.cookie and Window.localStorage pages on MDN.
这篇关于如何强制浏览器重新加载缓存的URL而不更改URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!