html - 针对使用的图标字体的 Chrome 未使用预加载警告

标签 html css google-chrome fonts preload

我在 Chrome 中预加载了一个图标字体

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

稍后在我的 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;
}

在页面加载后的一秒钟内,我使用 Unicode 代码点 U+E95B 和我的图标字体。

不过,我仍然收到来自 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.

我如何摆脱这个警告?

最佳答案

尝试将 rel="preload"更改为 rel="prefetch"。

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

rel="prefetch"用于需要但不会立即使用的特定资源。 Chrome 显然没有及时注册它的使用并发出警告,这是我的猜测。

如果预取不起作用,请尝试 rel="dns-prefetch"。 rel="dns-prefetch"告诉浏览器解析 dns,以便在需要时可以快速加载。

我认为 prefetch 应该可以工作,因为它实际上请求和下载资源并将其存储在缓存中供以后使用,但如果不快速使用它不会导致浏览器警告。

[编辑]
根据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">

css 和字体都是预加载的,然后页面呈现,所以 css 不必在字体之后加载。

在您的 css 文件中添加“local('IconFont')”,如下所示,完整示例 here

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

关于所有我能想到的帮助。希望这可以帮助。

关于html - 针对使用的图标字体的 Chrome 未使用预加载警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55819473/

相关文章:

javascript - 即使没有表单验证,网址也会打开

javascript - 当我在 "choose file"之外单击时,我的输入文件正在打开,如何才能使其仅在单击实际 "button"时打开?

jquery - 输入 :checked & toggle() in jquery

macos - 开发者工具在 Chrome osx 中不起作用

google-chrome - 来自 Service Worker 缓存的资源的巨大内容下载时间

html - 标题栏不包含下拉菜单 (CSS)

jquery - 蜂窝边框

jquery - 访问存储在 Jquery 变量中的类名

javascript - 有什么方法可以覆盖 Angular 数据列表自动完成行为吗?

javascript - 定义 var 时抛出 undefined