New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use font-display: swap to prevent invisible text on load #2242
Comments
I've always wondered why my math appears blank on initial page load. 😄 I'm honestly not sure which behavior would feel best (I should experiment), but .katex { font-display: swap; } If we added a rule to base KaTeX, it could still be overridden via .katex { font-display: block !important; } Either way, this should probably be documented. The question is whether to change the default... Other votes would be welcome! |
Google's page speed suggestion, in general, is to use swap, but this is not the default for all browsers: https://developers.google.com/web/updates/2016/02/font-display Personally, I hate text jumping around as fonts are loading, but I'm not a big fan of the flash of "wrong" font with swap, either. It may be best to work towards an option where if the font is already in cache, then use block; if not, use swap. I conducted some experiments where I did lazy-load for math processing, making sure the above-the-fold content had most-used fonts ready to roll (using preload) and then loaded other fonts as the user scrolled down the page. Probably too fiddly for default KaTeX behavior, but something the browsers should do for us, I feel. |
as suggested by edemaine in <KaTeX/KaTeX#2242>.
Hmm, that doesn't validate:
I think |
You need to specify it in the fonts CSS like @font-face {
font-family: 'Lato';
font-style: italic;
font-weight: 100;
font-display: swap;
src: url(https://fonts.gstatic.com/s/lato/v17/S6u-w4BMUTPHjxsIPx-mPCLQ7A.woff2) format('woff2');
unicode-range: U+0100-024F, U+0259, U+1E00-1EFF, U+2020, U+20A0-20AB, U+20AD-20CF, U+2113, U+2C60-2C7F, U+A720-A7FF;
} It is better to show things in Times New Roman font, which is the default fallback font than to make users wait for the font to load and display nothing. |
Google fonts seem to default to swap, and mixing them with katex makes it so that katex is invisible briefly on page load, unlike google fonts which make use of fallbacks while loading. This would unify their behavior. Ideally there'd be an option for this, and a ?display=swap in the cdn url (like google fonts: https://developers.google.com/fonts/docs/css2#use_font-display), but I don't know if jsdelivr supports that!
Seconding this! Made a PR for changing the default behavior. It'd be nice to have an option similar to how google fonts lets you specify the display behavior as part of the cdn url parameter, with |
Google PageSpeed Insights suggests that I should "ensure text remains visible during webfont load".
For KaTeX this would simply involve including
font-display: swap;
in all of the@font-face
entries inkatex.css
.I think this a good idea, but how would we then cater for people who don't want to temporarily show a fallback font?
The text was updated successfully, but these errors were encountered: