admin管理员组文章数量:1314411
Some text in my page appears a few seconds later compared to another text.
I've different font files for different font-weights (taken from font squirrel's web font generator). When I load component on some click event semi-bold font appears after regular font. After some research I understood that browser only loads fonts that are needed. Para font was being used before that component was loaded so it was already loaded in the browser for use, therefore the perceived delay of semi-bold font.
I preload
the semi-bold font in index.html (it's a react/vite app.
<link rel="preload" href="/src/assets/barlow/barlow-semibold-webfont.woff2" as="font" type="font/woff2" crossorigin />
This loads the font before it's used(can be seen on network tab of dev tools) but the text still appears after regular font :(I think it's a little fast though).
Then I just set an element that uses that font and put it out of the screen like this in my App.jsx
<p
id="preload-fonts"
style={{
position: 'absolute',
left: '-100vw',
opacity: '0',
fontFamily: 'barlowsemibold',
}}
>
voila it works.
I want to know why it works and why preload didn't even though it still downloaded the font before the component is loaded.
My repo component name (LoadingScreen.jsx)
Some text in my page appears a few seconds later compared to another text.
I've different font files for different font-weights (taken from font squirrel's web font generator). When I load component on some click event semi-bold font appears after regular font. After some research I understood that browser only loads fonts that are needed. Para font was being used before that component was loaded so it was already loaded in the browser for use, therefore the perceived delay of semi-bold font.
I preload
the semi-bold font in index.html (it's a react/vite app.
<link rel="preload" href="/src/assets/barlow/barlow-semibold-webfont.woff2" as="font" type="font/woff2" crossorigin />
This loads the font before it's used(can be seen on network tab of dev tools) but the text still appears after regular font :(I think it's a little fast though).
Then I just set an element that uses that font and put it out of the screen like this in my App.jsx
<p
id="preload-fonts"
style={{
position: 'absolute',
left: '-100vw',
opacity: '0',
fontFamily: 'barlowsemibold',
}}
>
voila it works.
I want to know why it works and why preload didn't even though it still downloaded the font before the component is loaded.
My repo component name (LoadingScreen.jsx)
Share Improve this question edited Feb 2 at 19:58 VLAZ 29.1k9 gold badges63 silver badges84 bronze badges asked Feb 2 at 7:22 vatsal chauhanvatsal chauhan 999 bronze badges1 Answer
Reset to default 1Using preload, our custom font is downloaded, but it is not used or rendered yet. Fonts will only be fully activated when they are actually used on the page (i.e. used by actual text or invisible text [like your hack]).
Your hack forces the browser to apply the font and render it on an invisible text, so next time when actual text appears, our font is loaded, cached and activated so it’s available to be applied instantly without any delay.
Extra point: We can use {font-display: swap} to reduce the delay to load the text with custom font but the hack works fine and I think is the best way to ensure the text is loaded with custom font without any delay.
本文标签: reactjstext loads slowly even on font preloadStack Overflow
版权声明:本文标题:reactjs - text loads slowly even on font preload - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741854377a2401252.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论