【问题标题】:HTTP2 pushed webfonts not usedHTTP2 推送的 webfonts 未使用
【发布时间】:2018-03-06 05:31:52
【问题描述】:

我在 HTTP2 响应中发送 Link 预加载标头。喜欢这个:

Link: </assets/script/main.js?h=1795387974>; rel=preload; as=script, </assets/font/sourcesanspro_regular.woff2>; rel=preload; as=font

脚本、样式和图像不会引起任何问题——它们是被推送和使用的。但是字体被推送,然后再次请求/获取,Chromium 控制台抱怨:

资源https://example.com/assets/font/sourcesanspro_regular.woff2 已使用链接预加载进行预加载,但在窗口加载事件后的几秒钟内未使用。请确保它不是无缘无故预加载的。

这里是提到的字体的响应头。

推送:

accept-ranges:bytes
cache-control:max-age=5184000, public
content-length:16892
content-type:application/octet-stream
date:Mon, 25 Sep 2017 09:22:05 GMT
last-modified:Mon, 18 Sep 2017 14:33:31 GMT
pragma:public
status:200
x-content-type-options:nosniff
x-frame-options:SAMEORIGIN
x-http2-push:pushed
x-xss-protection:1; mode=block

推送后请求:

accept-ranges:bytes
cache-control:max-age=5184000, public
content-length:16892
content-type:application/octet-stream
date:Mon, 25 Sep 2017 09:22:05 GMT
last-modified:Mon, 18 Sep 2017 14:33:31 GMT
pragma:public
status:200
x-content-type-options:nosniff
x-frame-options:SAMEORIGIN
x-xss-protection:1; mode=block

我做错了什么?

【问题讨论】:

    标签: push webfonts http2 preload h2o-http


    【解决方案1】:

    您必须为字体添加跨域:

    Link: </assets/font/sourcesanspro_regular.woff2>; rel=preload; as=font; crossorigin
    

    更多信息请看这里:https://github.com/w3c/preload/issues/32 在这里:https://www.smashingmagazine.com/2016/02/preload-what-is-it-good-for/

    值得回顾的一点:你必须添加一个跨域属性 获取字体时,因为它们是使用匿名模式 CORS 获取的。 是的,即使您的字体与页面的来源相同。对不起。

    【讨论】:

    • 我认为您的解决方案中有错字。在crossorigin 之前需要;。这是一个没有值的属性。
    • 正确!固定。
    猜你喜欢
    • 2018-09-06
    • 2019-01-04
    • 2018-05-25
    • 2019-02-25
    • 2017-09-09
    • 2021-06-05
    • 2018-03-11
    • 2019-08-11
    • 2018-04-27
    相关资源
    最近更新 更多