douchu4048 2013-11-06 02:11
浏览 82

标头缓存似乎被忽略了

I've been wondering what seems to be wrong in my cache.

I set this headers so that it will not cache contents on the website (html, css, php) files.

header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1.
header('Pragma: no-cache'); // HTTP 1.0.
header('Expires: 0'); // Proxies.

So I went on to clear cache first on my Mozilla, reload the page then got this:

enter image description here

Seems fine because status is 200 OK, now I edited the file style.css then do a refresh, here is the result:

enter image description here

It says 304 Not Modified. I am no expert but isn't that means it cache the file? Also, I edited style.css, and my changes didn't appear. Need to do a Ctrl+F5 to reset it. Why is that?

  • 写回答

1条回答 默认 最新

  • dso407787736 2013-11-06 06:57
    关注

    Extending from comments:

    What you were doing on your headers only affect that resource -- which most likely would be a PHP-generated HTML page.

    To demonstrate, here's a simplified process:

    • Browser: GET index.php
    • Server: PHP: Cache-Control: no-cache; output content...
    • Browser: GET style.css
    • Server: (Default headers;) output style.css

    So to ask the client to not cache CSS, you can:

    1. Add additional header for all CSS in your HTTP server configuration. Example in Apache:

      <FilesMatch "\.css$">
          Header add Cache-Control no-cache
      </FilesMatch>
      
    2. Use PHP to redistribute CSS:

      <?php
      header("Content-Type: text/css");
      header("Cache-Control: no-cache");
      ?>
      body {
      /* ... */
      }
      
    3. If you don't mind modifying HTML on every edit (or if you're using some kind of version control that support this feature), you can make your HTML look like this:

      <link rel="stylesheet" type="text/css" href="style.css?20131111" />
      
    评论

报告相同问题?

悬赏问题

  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。