简体   繁体   English

使用缓存刷新页面

[英]Refresh page using cache

I am trying to refresh a page, but WANT to use the cache and can't figure out how to do this.我正在尝试刷新页面,但想要使用缓存并且无法弄清楚如何执行此操作。 There are two situations:有两种情况:

  • If I click in the URL bar and hit enter (or visit the page from somewhere else) it reloads the page/images from the cache.如果我单击 URL 栏并按回车键(或从其他地方访问页面),它将从缓存中重新加载页面/图像。 GREAT!伟大的!

  • If I click on the refresh button or use Javascript to refresh the page it grabs all the images again and takes forever.如果我单击刷新按钮或使用 Javascript 刷新页面,它会再次抓取所有图像并永久保存。 NOT GREAT!不是很好!

I've tried: top.location.reload(false);我试过: top.location.reload(false); and top.location.reload(true);top.location.reload(true); (I'm sending this from in an iFrame) and neither used the cache. (我是从 iFrame 中发送的)并且都没有使用缓存。 I'm avoiding using location so it doesn't end up in the browser history twice.我避免使用location ,因此它不会两次出现在浏览器历史记录中。

Question : How do I reload the page using the cached images?问题:如何使用缓存的图像重新加载页面? Is there a different javascript function or is this a mod_expires issue?是否有不同的 javascript function 或者这是一个 mod_expires 问题?

Thanks for any help in advance!提前感谢您的帮助!

EDIT: (info from chrome: developer tools)编辑:(来自 chrome 的信息:开发者工具)

  • When navigating to the page I get "From Cache" for all images导航到页面时,我得到所有图像的“来自缓存”
  • When refreshing page I get "304 - Not Modified" for all images (and it takes the time to download each)刷新页面时,我得到所有图像的“304 - 未修改”(并且下载每个图像都需要时间)

EDIT 2: (Headers from an image, safari: developer tools)编辑 2:(来自图像的标题,safari:开发人员工具)

Javascript: top.location.reload(false); Javascript:top.location.reload(假); (No Cache!) (没有缓存!)

Status Code:304 Not Modified

Request Headers
Cache-Control:max-age=0
If-Modified-Since:Tue, 28 Jun 2011 07:13:17 GMT
If-None-Match:"104684ae-a7d-66e41d40"
Referer:http://getdirectus.com/dev/media.php
User-Agent:Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; en-us) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1

Response Headers
Cache-Control:max-age=157680000
Connection:Keep-Alive
Date:Tue, 28 Jun 2011 16:56:50 GMT
Etag:"104684ae-a7d-66e41d40"
Expires:Sun, 26 Jun 2016 16:56:50 GMT
Keep-Alive:timeout=5, max=94
Server:Apache/2.0.54

Navigating to page: (Uses cache)导航到页面:(使用缓存)

Status Code:200 OK

Response Headers**
Accept-Ranges:bytes
Cache-Control:max-age=157680000
Connection:Keep-Alive
Content-Length:2685
Content-Type:image/jpeg
Date:Tue, 28 Jun 2011 16:54:20 GMT
Etag:"104684ae-a7d-66e41d40"
Expires:Sun, 26 Jun 2016 16:54:20 GMT
Keep-Alive:timeout=5, max=99
Last-Modified:Tue, 28 Jun 2011 07:13:17 GMT
Server:Apache/2.0.54

The documentation for window.location.reload( false ); window.location.reload( false );文档 says it will load from cache.说它将从缓存中加载。 If that isn't happening then you may be seeing a browser bug.如果这没有发生,那么您可能会看到浏览器错误。 See if you can replicate the problem in another browser.看看您是否可以在另一个浏览器中复制该问题。

EDIT (for your edit): You are seeing that behaviour because you don't have an Expires header set in the future.编辑(供您编辑):您看到这种行为是因为您将来没有设置 Expires header。 You will need to add an Expires header in Apache .您需要在 Apache 中添加 Expires header

window.location.href = window.location.href;

If the location contains a # , be sure to remove it before setting href .如果位置包含# ,请务必在设置href之前将其删除。

Your second request was initiated by manually refreshing the page.您的第二个请求是通过手动刷新页面发起的。 When you do this, the browser sends up an additional cache-control:max-age=0 header with the request.当你这样做时,浏览器会发送一个额外的cache-control:max-age=0 header 请求。 This is where the 304 (Not Modified) is coming from.这就是 304(未修改)的来源。

If you navigate within the site (using links), the browser will continue to use its cache.如果您在站点内导航(使用链接),浏览器将继续使用其缓存。

If you want to enable caching in the client, mind sending Expire headers, eg with mod_expires如果您想在客户端启用缓存,请注意发送Expire标头,例如使用mod_expires

Using this site , I only got green image with location.refresh(true);使用这个站点,我只得到了带有location.refresh(true);的绿色图像。 . . With location.refresh();使用location.refresh(); or location.refresh(false);location.refresh(false); I got the red image.我得到了红色的图像。 Working ok, I guess.工作正常,我猜。

There are two separate things to consider here:这里有两件不同的事情需要考虑:

1: the request... browser -> server 1:请求...浏览器->服务器

2: the response... server -> browser 2:响应...服务器 -> 浏览器

When you refresh a page you cannot get around the browser doing a requests for page assets to the server.当您刷新页面时,您无法绕过浏览器向服务器请求页面资产。

What you can do is make sure the server sends a minimal response.您可以做的是确保服务器发送的响应最少。

The best way to achieve this, is to use etags in your response headers.实现这一点的最佳方法是在响应标头中使用etags That way the browser will send a if-none-match request to the server, and get a 304 Nothing changed response back assuming nothing has been modifed.这样,浏览器将向服务器发送一个if-none-match请求,并在没有修改任何内容的情况下返回304 Nothing changed响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM