【问题标题】:Concatenating CSS with cache control将 CSS 与缓存控制连接起来
【发布时间】:2011-08-17 13:08:29
【问题描述】:

好的,这是一个艰难的:

我有这些网站,其中包含几个使用缓存清除的 CSS 文件,并且运行良好。每次通话我都会收到 403 Not Modified。

但现在我在我的 htaccess 中使用这个规则来连接我的所有文件


<FilesMatch "\.combined\.css$">
  Options +Includes
  AddOutputFilterByType INCLUDES text/css
  SetOutputFilter INCLUDES
</FilesMatch>

在 script.combined.css 中你可以找到:


<!--#include file="file1.css" -->
<!--#include file="file2.css" -->

问题来了,即使我的文件“script.combined.css”使用了缓存破坏。它总是返回“200 Ok”。这些是 script.combined.css 的标头:


Request

URL:http://XXX/css/script.combined.css
Request Method:GET
Status Code:200 OK

Request Headers

Accept:text/css,*/*;q=0.1
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Cookie:typo3-login-cookiecheck=true; PHPSESSID=2evc53ngjjobateti38gpahtt7; tx_phpmyadmin=quf4sgevagubd1snd9eoq4g4d0; be_typo_user=0f4c7e291b6d7673310d6f15da687910; Typo3InstallTool=62erpih8fmqbbl3pe75pcenuu0; fe_typo_user=a7e5de71521603161fa598ed523b024b
Host:XXX
Referer:http://XXX/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.30 (KHTML, like Gecko) Ubuntu/11.04 Chromium/12.0.742.112 Chrome/12.0.742.112 Safari/534.30

Response Headers

Accept-Ranges:bytes
Cache-Control:max-age=31536000, public
Connection:Keep-Alive
Content-Type:text/css
Date:Wed, 17 Aug 2011 13:56:42 GMT
Expires:Thu, 16 Aug 2012 13:56:42 GMT
Keep-Alive:timeout=5, max=99
Server:HTTPD
Transfer-Encoding:chunked

...虽然这些是另一个简单 css 文件的标题:


Request URL:http://XXX/css/style.css
Request Method:GET
Status Code:304 Not Modified

Request Headers

Accept:text/css,*/*;q=0.1
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Cookie:typo3-login-cookiecheck=true; PHPSESSID=2evc53ngjjobateti38gpahtt7; tx_phpmyadmin=quf4sgevagubd1snd9eoq4g4d0; be_typo_user=0f4c7e291b6d7673310d6f15da687910; Typo3InstallTool=62erpih8fmqbbl3pe75pcenuu0; fe_typo_user=a7e5de71521603161fa598ed523b024b
Host:XXX
If-Modified-Since:Wed, 17 Aug 2011 11:45:17 GMT
Referer:http://XXX/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.30 (KHTML, like Gecko) Ubuntu/11.04 Chromium/12.0.742.112 Chrome/12.0.742.112 Safari/534.30

Response Headers

Cache-Control:max-age=31536000, public
Connection:Keep-Alive
Date:Wed, 17 Aug 2011 13:56:42 GMT
Expires:Thu, 16 Aug 2012 13:56:42 GMT
Keep-Alive:timeout=5, max=97
Server:HTTPD

我使用了一个扩展来衡量谷歌的页面速度,我收到了这条消息:

指定一个缓存验证器

以下资源缺少缓存验证器。资源 不指定缓存验证器无法有效刷新。 指定 Last-Modified 或 ETag 标头以启用缓存验证 以下资源:

http://XXX/css/script.combined.css?1313583214

我该怎么办??

【问题讨论】:

标签: css .htaccess cache-control typo3


【解决方案1】:

根据 Apache 的服务器端文档包括:

在其默认配置中,Apache 不会在 SSI 页面上发送最后修改日期或内容长度 HTTP 标头,因为这些值很难为动态内容计算。

http://httpd.apache.org/docs/1.3/howto/ssi.html

因此,在您的浏览器中直接向 styles.combined.css 请求将永远不会返回 304 Not Modified 状态。

但如果您设置 expires headers,浏览器只会从本地缓存中提取资源。检查最后修改日期的请求是不必要的,不会被发送。

【讨论】:

    【解决方案2】:

    我建议您看看html5 boilerplate 项目的出色工作,它为您在新项目的开始提供了良好的基础。但更重要的是,它包含一个带有良好注释规则的 .htaccess 文件。

    以下 sn-p 可能对您特别有用:

    # ----------------------------------------------------------------------
    # Expires headers (for better cache control)
    # ----------------------------------------------------------------------
    
    # These are pretty far-future expires headers.
    # They assume you control versioning with cachebusting query params like
    #   <script src="application.js?20100608">
    # Additionally, consider that outdated proxies may miscache 
    #   www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/
    
    # If you don't use filenames to version, lower the CSS  and JS to something like
    #   "access plus 1 week" or so.
    
    <IfModule mod_expires.c>
      ExpiresActive on
    
    # Perhaps better to whitelist expires rules? Perhaps.
      ExpiresDefault                          "access plus 1 month"
    
    # cache.appcache needs re-requests in FF 3.6 (thanks Remy ~Introducing HTML5)
      ExpiresByType text/cache-manifest       "access plus 0 seconds"
    
    # Your document html 
      ExpiresByType text/html                 "access plus 0 seconds"
    
    # Data
      ExpiresByType text/xml                  "access plus 0 seconds"
      ExpiresByType application/xml           "access plus 0 seconds"
      ExpiresByType application/json          "access plus 0 seconds"
    
    # Feed
      ExpiresByType application/rss+xml       "access plus 1 hour"
      ExpiresByType application/atom+xml      "access plus 1 hour"
    
    # Favicon (cannot be renamed)
      ExpiresByType image/x-icon              "access plus 1 week" 
    
    # Media: images, video, audio
      ExpiresByType image/gif                 "access plus 1 month"
      ExpiresByType image/png                 "access plus 1 month"
      ExpiresByType image/jpg                 "access plus 1 month"
      ExpiresByType image/jpeg                "access plus 1 month"
      ExpiresByType video/ogg                 "access plus 1 month"
      ExpiresByType audio/ogg                 "access plus 1 month"
      ExpiresByType video/mp4                 "access plus 1 month"
      ExpiresByType video/webm                "access plus 1 month"
    
    # HTC files  (css3pie)
      ExpiresByType text/x-component          "access plus 1 month"
    
    # Webfonts
      ExpiresByType font/truetype             "access plus 1 month"
      ExpiresByType font/opentype             "access plus 1 month"
      ExpiresByType application/x-font-woff   "access plus 1 month"
      ExpiresByType image/svg+xml             "access plus 1 month"
      ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
    
    # CSS and JavaScript
      ExpiresByType text/css                  "access plus 1 year"
      ExpiresByType application/javascript    "access plus 1 year"
    
      <IfModule mod_headers.c>
        Header append Cache-Control "public"
      </IfModule>
    
    </IfModule>
    
    
    
    # ----------------------------------------------------------------------
    # ETag removal
    # ----------------------------------------------------------------------
    
    # FileETag None is not enough for every server.
    <IfModule mod_headers.c>
      Header unset ETag
    </IfModule>
    
    # Since we're sending far-future expires, we don't need ETags for
    # static content.
    #   developer.yahoo.com/performance/rules.html#etags
    FileETag None
    

    【讨论】:

    • HTML5boilerplate 正是我使用的!而且它不起作用:(你写给我的那个sn-p,我已经在使用它了。
    • 您提供的 css 文件 (script.combined.css) 是由服务器端代码生成还是通过脚本 / mod_rewrite 并路由到脚本?我怀疑你使用 php,如果组合脚本是用 php 编写的,然后提供服务,你需要在 php 中使用 add a header for caching...
    • 我已经编辑了帖子中script.combiend.css的内容。有了这个和 .htaccess 中的规则,这就是我用来生成该文件的全部内容。我没有明确使用任何 PHP(据我所知)。
    • 不应该可以正常工作。就目前而言,这听起来像是一个页面速度问题,您可能想检查您使用 chrome 对该文件发出的请求,以查看服务器究竟发送了什么......但除此之外,我不知所措
    • header 是我看到 Code 200 的地方。我已经在帖子中写了 Headers,所以你可以看看那里是否有问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-08
    相关资源
    最近更新 更多