Safari/Webkit 上的后退按钮从缓存加载页面而不是新副本

问题描述:

我对 Mac OSX 系统上 Safari/Webkit 的奇怪"行为遇到了问题,该行为与浏览器上的后退按钮禁用了 javascript 的功能有关.

I am having problems with regard to a "strange" behaviour of Safari/Webkit on Mac OSX systems that's related to the functionality of the back button while having javascript disabled on the browser.

我的场景是一个 Web 应用程序,它包含一个登录页面,成功登录后,会将用户引导至 page1.如果用户退出 page1,那么他/她将被重定向到 page2.对 page1 的会话外和未经身份验证的请求将通过 302 HTTP 响应重定向到初始登录页面.

The scenario I have is a web application which consists of a login page that upon a successful login, would lead the user to page1. If the user logs out of page1, then he/she will get redirected to page2. Out of session and un-authenticated requests to page1 are being redirected to the initial login page via a 302 HTTP Response.

还值得一提的是,对 page1 请求的响应中包含以下 HTTP 标头,以防止浏览器缓存页面:

It's also worth to mention that responses to requests to page1 have the following HTTP headers present in them to prevent browsers from caching the page:

Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Expires: Thu, 1 JAN 1970
Pragma: no-cache

从第 1 页成功注销后,用户将被重定向到第 2 页.如果此时用户决定单击后退按钮,则浏览器将从其缓存中加载 page1,而不是再次访问服务器以检索新副本.请注意,这一切都发生在 Safari 中 javascript 被禁用时,启用它会产生从 Web 服务器重新加载 page1 的预期结果,从而使用户被重定向到初始登录页面

Upon a successful log out from page1, the user get redirected to page2. If at this point intime the user decided to click on the back button, then the browser will load page1 from its cache rather than hitting the server again to retrieve a fresh copy. Please not that this all happen while javascript is DISABLED within Safari, having it ENABLED produce the expected result of reloading page1 from the web server thus having the user get redirected to the initial login page

我已尝试在页面正文标记上设置卸载事件处理程序,以防止 Webkit 将页面加载到其页面缓存中,但这似乎并没有解决问题.

I've tried to set an unload event handler on the page body tag to prevent Webkit from having the page loaded into its Page Cache, but that didn't seem to solve the problem.

其他主流浏览器,FF、IE 和 Chrome 似乎在 JS 和非 JS 情况下都产生了预期的结果.

Other mainstream browsers, FF, IE and Chrome seem to produce the expected result both in JS and non JS situations.

我在这里遗漏了什么,或者这听起来更像是 Safari/WebKit 的错误?

Am I missing something here or this sounds more of a bug with Safari/WebKit?

干杯;

这是因为 Webkit/Safari 中的页面缓存功能.您可以在以下链接中找到更多相关信息

This is because of the Page Cache feature in Webkit/Safari. You can find more information about this on the below link

问题在 iOS 5 Safari 中使用页面缓存导航返回/卸载事件未触发

作为快速修复,您可以尝试向窗口卸载事件添加一个空函数.如果出现这种情况,Webkit/Safari 不会将页面添加到 pageCache –– 使页面像其他浏览器一样正常加载.

As a quick fix, You can try adding an empty function to the window unload event. When this is present, Webkit/Safari won't add the page to the pageCache –– leaving the page to load normally like the other browsers do.