强制(或高度鼓励)浏览器直接从缓存加载webfonts

时间:2013-12-14 03:43:10

标签: caching browser header webfonts google-webfonts

我正在尝试让我的网站加载一些网页字体,我想配置响应标头告诉浏览器缓存字体,保留字体,直接从缓存中获取文件而不是发送对服务器的请求。

Google Webfonts在此方面做得很好;即使我在Chrome或Firefox中重新加载,该字体也会直接从缓存加载,甚至无法检查是否有更新版本可用(它会返回200状态,Chrome中会显示(from cache)

然而,当我发送我的响应(我使用nginx作为Web服务器)和所有常用的缓存头时,它无法执行此操作。即使在浏览器不会发送请求(以及服务器发送304)的情况下,重新加载常规页面也会很棒。

以下是从缓存中提供的Google标题(来自其中一个WOFF文件):

HTTP/1.1 200 OK
status: 200 OK
version: HTTP/1.1
access-control-allow-origin: *
age: 13190
alternate-protocol: 80:quic
cache-control: public, max-age=31536000
content-length: 28588
content-type: font/woff
date: Fri, 13 Dec 2013 23:51:45 GMT
expires: Sat, 13 Dec 2014 23:51:45 GMT
last-modified: Thu, 13 Sep 2012 17:47:52 GMT
server: sffe
timing-allow-origin: *
x-content-type-options: nosniff
x-xss-protection: 1; mode=block

这是我的服务器发送的响应之一(“直播”服务):

HTTP/1.1 200 OK
Server: nginx
Date: Sat, 14 Dec 2013 03:40:37 GMT
Content-Type: font/opentype
Content-Length: 244744
Last-Modified: Mon, 09 Dec 2013 22:35:59 GMT
Connection: keep-alive
Expires: Sun, 14 Dec 2014 03:40:37 GMT
Cache-Control: max-age=31536000
Access-Control-Allow-Origin: *
Cache-Control: public, max-age=315360000
Accept-Ranges: bytes

(我意识到有2个Cache-Control标头,但根据规格,这应该没问题?第一个​​是nginx,第二个是我手动添加'public')。

是什么导致两个回复之间的行为有很大差异?

1 个答案:

答案 0 :(得分:2)

我对这些东西并不过分苛刻,但在配置Web服务器方面确实有一点经验。我决定快速查看一下你的问题,我在nginx网站上找到了可能有用的信息。

http://nginx.org/ru/docs/http/ngx_http_headers_module.html

但是,我看到的主要内容是nginx响应没有发送“Age”标题。

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

以下是w3c的主要内容:

  

14。6年龄

  The Age response-header field conveys the sender's estimate of the
  amount of time since the response (or its revalidation) was
  generated at the origin server. A cached response is "fresh" if
  its age does not exceed its freshness lifetime. Age values are
  calculated as specified in section 13.2.3.

       Age = "Age" ":" age-value
       age-value = delta-seconds

  Age values are non-negative decimal integers, representing time in
  seconds.

  If a cache receives a value larger than the largest positive
  integer it can represent, or if any of its age calculations
  overflows, it MUST transmit an Age header with a value of
  2147483648 (2^31). An HTTP/1.1 server that includes a cache MUST
  include an Age header field in every response generated from its
  own cache. Caches SHOULD use an arithmetic type of at least 31
  bits of range.

发送“Age”标题可能会解决您的问题。祝你好运!