简体   繁体   English

对于使用的图标字体,Chrome 未使用预加载警告

[英]Chrome unused preload warning for an icon font that is used

I have an icon font that I preload in Chrome with我有一个在 Chrome 中预加载的图标字体

<link rel="preload" as="font" type="font/ttf" href="/static/media/IconFont.ad47b1fb.ttf" crossorigin="anonymous">

and reference later in my CSS with并稍后在我的 CSS 中使用

@font-face {
  font-family: "IconFont";
  src: url(/static/media/IconFont.d9fff078.eot);
  src: url(/static/media/IconFont.d9fff078.eot#iefix)
      format("embedded-opentype"),
    url(/static/media/IconFont.ad47b1fb.ttf) format("truetype"),
    url(/static/media/IconFont.c8a8e064.woff) format("woff"),
    url(/static/media/IconFont.979fb19e.svg#IconFont) format("svg");
  font-weight: normal;
  font-style: normal;
}

Within one second of the page loading I use Unicode code point U+E95B with my icon font.在页面加载后的一秒钟内,我将 Unicode 代码点 U+E95B 与我的图标字体一起使用。

I still get a warning from Chrome, though, that says:不过,我仍然收到来自 Chrome 的警告,上面写着:

The resource http://localhost:3000/static/media/IconFont.ad47b1fb.ttf was
preloaded using link preload but not used within a few seconds from the
window's load event. Please make sure it has an appropriate `as` value and
it is preloaded intentionally.

How do I get rid of this warning?我如何摆脱这个警告?

Try changing from rel="preload" to rel="prefetch".尝试从 rel="preload" 更改为 rel="prefetch"。

<link rel="prefetch" as="font" type="font/ttf" href="/static/media/IconFont.ad47b1fb.ttf" crossorigin="anonymous">

rel="prefetch" is used for a specific resource that is required but not use immediately. rel="prefetch" 用于需要但不立即使用的特定资源。 Chrome apparently isn't registering it's use in time and gives the warning, which is my guess. Chrome 显然没有及时注册它的使用并给出警告,这是我的猜测。

If prefetch doesn't work try rel="dns-prefetch".如果预取不起作用,请尝试 rel="dns-prefetch"。 rel="dns-prefetch" tells the browser to resolve the dns so when it is needed it can be loaded quickly. rel="dns-prefetch" 告诉浏览器解析 dns,以便在需要时可以快速加载。

I think prefetch should work though, as it actually requests and downloads the resource and stores it in the cache for later use, but it doesn't cause the browser warning if it isn't used quickly.我认为预取应该可以工作,因为它实际上请求并下载资源并将其存储在缓存中以供以后使用,但如果不快速使用它不会引起浏览器警告。

[EDIT] [编辑]
According to this page this page, load your css first also using preload, then your font, ie根据this page this page,首先使用预加载加载你的css,然后是你的字体,即

  <link rel="preload" as="style" href="[your-css-file-here.css]">
  <link rel="preload" as="font" crossorigin type="font/tff" href="/static/media/IconFont.ad47b1fb.ttf">

Both the css and the font are preloaded then the page renders, so the css doesn't have to be loaded after the font. css 和字体都预先加载,然后页面呈现,因此 css 不必在字体之后加载。

In your css file add "local('IconFont')," shown below, full example here在您的 css 文件中添加“local('IconFont')”,如下所示, 此处为完整示例

src: local('IconFont'),
    url(/static/media/IconFont.ad47b1fb.ttf) format("truetype"),
    url(/static/media/IconFont.ad47b1fb.ttf) format("woff"),
    /* continue your font declaration */

About all I can think of to help with this.关于我能想到的所有帮助。 Hope this helps.希望这可以帮助。

This is an example from MDN.这是一个来自 MDN 的例子。

https://mdn.github.io/html-examples/link-rel-preload/fonts/ https://mdn.github.io/html-examples/link-rel-preload/fonts/

And that gives the same warning too.这也给出了同样的警告。 When i open the developer tool and press Ctrl+F5 which forces the browser to hard loading of all resources this warning does not come across.当我打开开发人员工具并按 Ctrl+F5 强制浏览器硬加载所有资源时,不会出现此警告。 If I load the page with F5 warning appears.如果我加载带有 F5 警告的页面。 So this should be a some kind of confusion on browser side.所以这应该是浏览器端的某种混淆。 But i couldn't find a reliable answer.但我找不到可靠的答案。

https://github.com/mdn/html-examples/blob/master/link-rel-preload/fonts/index.html https://github.com/mdn/html-examples/blob/master/link-rel-preload/fonts/index.html

在此处输入图像描述

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

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