简体   繁体   English

未使用 HTTP2 推送 webfonts

[英]HTTP2 pushed webfonts not used

I'm sending a Link preload header in the HTTP2 response.我在 HTTP2 响应中发送一个Link预加载标头。 Like this one:像这个:

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

scripts, styles and images don't cause any problem - they are pushed and used.脚本、样式和图像不会引起任何问题 - 它们被推送和使用。 But fonts are pushed and then requested/fetched again and the Chromium console complains:但是字体被推送然后再次请求/获取并且 Chromium 控制台抱怨:

The resource https://example.com/assets/font/sourcesanspro_regular.woff2 was preloaded using link preload but not used within a few seconds from the window's load event.资源https://example.com/assets/font/sourcesanspro_regular.woff2是使用链接预加载预加载的,但在窗口加载事件的几秒钟内未使用。 Please make sure it wasn't preloaded for nothing.请确保它不是无故预加载的。

Here are the response headers of the mentioned font.以下是上述字体的响应标头。

Pushed:推:

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

Requested after push:推送后请求:

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

What am I doing wrong?我做错了什么?

You have to add crossorigin for fonts:您必须为字体添加 crossorigin:

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

For more information see here: https://github.com/w3c/preload/issues/32 and here: https://www.smashingmagazine.com/2016/02/preload-what-is-it-good-for/ .有关更多信息,请参见此处: https : //github.com/w3c/preload/issues/32和此处: https : //www.smashingmagazine.com/2016/02/preload-what-is-it-good-for/ .

One point worth going over: You have to add a crossorigin attribute when fetching fonts, as they are fetched using anonymous mode CORS.值得一提的一点:在获取字体时必须添加 crossorigin 属性,因为它们是使用匿名模式 CORS 获取的。 Yes, even if your fonts are on the same origin as the page.是的,即使您的字体与页面同源。 Sorry.对不起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM