Jus*_*ney 7 html performance fonts
我一直在尝试预加载字体,无论我如何编写,Chrome 或 Firefox 都会抛出某种控制台错误。
\n情况1
\n<link rel="preload" href="/fonts/open-sans-v26-latin-regular.woff2" as="font" type="font/woff2" crossorigin>\n <link rel="preload" href="/fonts/RobotoCondensed-Regular.ttf" as="font" type="font/truetype" crossorigin>\n
Run Code Online (Sandbox Code Playgroud)\n火狐浏览器:
\nError 1\nPreload of /fonts/RobotoCondensed-Regular.ttf was ignored due to unknown \xe2\x80\x9cas\xe2\x80\x9d or \xe2\x80\x9ctype\xe2\x80\x9d values, or non-matching \xe2\x80\x9cmedia\xe2\x80\x9d attribute.\n\nError 2\nThe resource at \xe2\x80\x9c/fonts/open-sans-v26-latin-regular.woff2\xe2\x80\x9d preloaded with link preload was not used within a few seconds. Make sure all attributes of the preload tag are set correctly.\n\nThe resource at \xe2\x80\x9c/fonts/RobotoCondensed-Regular.ttf\xe2\x80\x9d preloaded with link preload was not used within a few seconds. Make sure all attributes of the preload tag are set correctly.\n
Run Code Online (Sandbox Code Playgroud)\n铬合金:
\n<link rel=preload> has an unsupported `type` value\n\nThe resource /fonts/open-sans-v26-latin-regular.woff2 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.\n
Run Code Online (Sandbox Code Playgroud)\n情况 2(按照此处许多线程的建议删除类型)
\n <link rel="preload" href="/fonts/open-sans-v26-latin-regular.woff2" as="font" crossorigin>\n <link rel="preload" href="/fonts/RobotoCondensed-Regular.ttf" as="font" crossorigin>\n
Run Code Online (Sandbox Code Playgroud)\n火狐浏览器:
\nThe resource at \xe2\x80\x9c/fonts/open-sans-v26-latin-regular.woff2\xe2\x80\x9d preloaded with link preload was not used within a few seconds. Make sure all attributes of the preload tag are set correctly.\n\nThe resource at \xe2\x80\x9c/fonts/RobotoCondensed-Regular.ttf\xe2\x80\x9d preloaded with link preload was not used within a few seconds. Make sure all attributes of the preload tag are set correctly.\n
Run Code Online (Sandbox Code Playgroud)\n铬合金:
\nThe resource /fonts/open-sans-v26-latin-regular.woff2 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.\n\nThe resource /fonts/RobotoCondensed-Regular.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.\n
Run Code Online (Sandbox Code Playgroud)\n如何让这些控制台错误消失?页面加载时,字体会立即显示。
\n为了预加载字体,托管字体的服务器必须使用 CORS,即使您的 html 文件位于同一源/域。
(这没有什么充分的理由,这只是一个任意的限制)
使用 时<link>
,如果crossorigin
存在该属性,服务器必须返回正确的 CORS 标头才能使链接正常工作。
(请参阅https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link)
通常,如果您的资源与 html 文件托管在同一源上,则不需要此属性。
然而,预加载字体是一种特殊情况,始终需要跨域模式。
(请参阅https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types/preload)