【问题标题】:Leverage browser caching - updates not working利用浏览器缓存 - 更新不起作用
【发布时间】:2016-08-13 13:59:32
【问题描述】:

我正在使用 Google 页面速度洞察来提高性能,但无论我做什么,我都无法让“利用浏览器缓存”消失。

我已经修改了几篇在线文章中描述的 htaccess 文件,并添加了我可以添加的所有组合。在页面洞察中,它抱怨 CSS/PNG/JS/JPG 文件。

# Expires Caching Start #
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css "access 2 day" 
ExpiresByType text/html "access 2 day" 
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/x-icon "access 1 year"
ExpiresByType application/pdf "access 1 month"
ExpiresByType application/javascript "access 1 month" 
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresDefault "access 1 month"
ExpiresByType text/x-javascript "access plus 1 month"  
ExpiresByType application/x-javascript "access plus 1 month"
ExpiresByType text/x-javascript "access 1 month"
</IfModule>
# Expires Caching End # 

【问题讨论】:

    标签: .htaccess web


    【解决方案1】:

    您似乎在许多行中都在“访问加 1 个月”中缺少您。 默认应该是你的可读性的第二行

    这是 h5bp 模板的一部分:

    ExpiresActive on
    ExpiresDefault                                      "access plus 1 month"
    ExpiresByType text/css                              "access plus 1 year"
    ExpiresByType text/html                             "access plus 0 seconds"
    ExpiresByType application/javascript                "access plus 1 year"
    ExpiresByType application/x-javascript              "access plus 1 year"
    ExpiresByType text/javascript                       "access plus 1 year"
    

    ... https://github.com/h5bp/html5-boilerplate/blob/master/dist/.htaccess

    【讨论】:

      【解决方案2】:

      如果您使用的是 Apache,您可能希望运行这些,因为这对我有用:

      sudo a2enmod headers
      sudo a2enmod expires
      service apache2 restart
      

      您可能还想试试这个代码:

      ## EXPIRES CACHING ##
      <IfModule mod_expires.c>
      ExpiresActive On
      ExpiresByType image/jpg "access plus 1 year"
      ExpiresByType image/jpeg "access plus 1 year"
      ExpiresByType image/gif "access plus 1 year"
      ExpiresByType image/png "access plus 1 year"
      ExpiresByType text/css "access plus 1 month"
      ExpiresByType application/pdf "access plus 1 month"
      ExpiresByType text/x-javascript "access plus 1 month"
      ExpiresByType application/x-shockwave-flash "access plus 1 month"
      ExpiresByType image/x-icon "access plus 1 year"
      ExpiresDefault "access plus 2 days"
      </IfModule>
      ## EXPIRES CACHING ##
      

      【讨论】:

      • 该代码与我已有的代码似乎没有太大不同。
      【解决方案3】:

      不妨试试这个:

      # 1 Month for most static assets
      <filesMatch ".(css|jpg|jpeg|png|gif|js|ico)$">
      Header set Cache-Control "max-age=2592000, public"
      </filesMatch>
      

      【讨论】:

        猜你喜欢
        • 2016-09-18
        • 2018-03-27
        • 2018-06-16
        • 2017-01-20
        • 2014-12-24
        • 2013-01-21
        • 2011-10-15
        相关资源
        最近更新 更多