浏览器可以在不接收 304 状态代码的情况下显示缓存中的对象吗?

发布于 2024-11-16 02:22:45 字数 617 浏览 8 评论 0原文

我试图了解是否可以避免对某些嵌入对象的请求,直接从缓存加载它们,而不询问 Web 服务器该对象是否有效(我不希望 Web 服务器以 304 http 状态响应我)代码)可能吗? expire 标头是否适用于这种方式?如何?


当然: 请求:

<script scr="my_js.php"></script> 

响应:

<? header("HTTP/1.1 304 Not Modified");
header("Expires: Mon, 31 Dec 2035 12:00:00 gmt");
header("Cache-Control: max-age=".(60*60*24*365)); 
echo "//this is a simpe example"; ?>

已解决

浏览器从缓存中加载资源,仅在您第一次打开页面时新选项卡或新浏览器窗口)向网络服务器请求资源)。

其他时候,浏览器总是向服务器询问有关缓存中保存的资源的信息。然后,Web 服务器响应 200 或 301。

I'm trying to understand if is it possible to avoid request for some embedded objects, loading them directly from cache without asking to web server if the object is valid or not (i don't want web server response to me with 304 http status code) Is it possible ? Does the expire header works for this way? How?


Of course: Request:

<script scr="my_js.php"></script> 

Response:

<? header("HTTP/1.1 304 Not Modified");
header("Expires: Mon, 31 Dec 2035 12:00:00 gmt");
header("Cache-Control: max-age=".(60*60*24*365)); 
echo "//this is a simpe example"; ?>

Solved

Browser loads resources from his cache without asking them to the web server only the first time you open the page (new tab or new browser window).

The other times browser ALWAYS ask information to the server about the resources saved in his cache. Then, the web server response with 200 or 301.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦冥 2024-11-23 02:22:45

是的,设置一个远程到期标头,并且在该到期之前不会再次下载资产。

如果删除 Last-Modified 和 ETag 标头,您将完全消除 If-Modified-Since 和 If-None-Match 请求及其 304 Not Modified 响应,因此文件将保持缓存状态,而不检查更新,直到过期标题表明有新内容可用!

来源

Yes, setting a distant expiry header and the asset will not be downloaded again until that expiry.

If you remove the Last-Modified and ETag header, you will totally eliminate If-Modified-Since and If-None-Match requests and their 304 Not Modified Responses, so a file will stay cached without checking for updates until the Expires header indicates new content is available!

Source.

沧笙踏歌 2024-11-23 02:22:45

从我的 htaccess 来看......

<IfModule mod_headers.c>

    Header unset Pragma
    FileETag None
    Header unset ETag

    # cache images/pdf docs for 10 days
    <FilesMatch "\.(ico|pdf|jpg|jpeg|png|gif|js)$">

      Header set Expires "Mon, 31 Dec 2035 12:00:00 gmt"
      Header unset ETag  
      Header unset Last-Modified

    </FilesMatch>

    # cache html/htm/xml/txt diles for 2 days
    <FilesMatch "\.(html|htm|xml|txt|xsl)$">
      Header set Cache-Control "max-age=7200, must-revalidate"
    </FilesMatch>

</IfModule>

它似乎不起作用......例如,firebug 的网络面板向我显示总是 200 状态代码,而 access.log 文件向我报告浏览器总是请求外部对象。

From my htaccess ...

<IfModule mod_headers.c>

    Header unset Pragma
    FileETag None
    Header unset ETag

    # cache images/pdf docs for 10 days
    <FilesMatch "\.(ico|pdf|jpg|jpeg|png|gif|js)$">

      Header set Expires "Mon, 31 Dec 2035 12:00:00 gmt"
      Header unset ETag  
      Header unset Last-Modified

    </FilesMatch>

    # cache html/htm/xml/txt diles for 2 days
    <FilesMatch "\.(html|htm|xml|txt|xsl)$">
      Header set Cache-Control "max-age=7200, must-revalidate"
    </FilesMatch>

</IfModule>

it seems doesn't works .... for example firebug's net panel show me always 200 status code and access.log file report me that external objects are always requested by the browser.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文