简体   繁体   English

如何在 React js 中预加载自定义字体?

[英]How to preload custom font face in react js?

I'm usinf otf fontface,it will take 819ms to load in deployed site.How we can preload font so that font loads early.我使用的是 otf 字体,在部署的站点中加载需要 819 毫秒。我们如何预加载字体以便字体尽早加载。

index.scss in /src/index.css /src/index.css 中的 index.scss

@font-face {
  font-family: 'Pro';
  font-weight: 400;
  font-display: fallback;
  src: url('./Fonts/Pro/Pro_Regular.otf');
  unicode-range: 'U+000-5FF';
}

font placed in /src/Fonts/Pro/Pro_Regular.otf folder.字体放置在 /src/Fonts/Pro/Pro_Regular.otf 文件夹中。

You can preload your fonts like this您可以像这样预加载 fonts

<link rel="preload" href="./Fonts/Pro/Pro_Regular.otf" as="font" crossorigin="anonymous" />

Also, you should use some CDN and preload your fonts此外,您应该使用一些 CDN 并预加载您的 fonts

You can try this:你可以试试这个:

useEffect(() => {
  document.fonts.load("12px Merriweather").then(() => setIsReady(true));
}, [])

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

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