Skip to content
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

Open
yawnoc opened this issue Apr 13, 2020 · 5 comments · May be fixed by #3940
Open

Use font-display: swap to prevent invisible text on load #2242

yawnoc opened this issue Apr 13, 2020 · 5 comments · May be fixed by #3940

Comments

@yawnoc
Copy link

yawnoc commented Apr 13, 2020

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 in katex.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?

@edemaine
Copy link
Member

edemaine commented Jul 9, 2020

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 swap behavior is probably less surprising. I believe you can customize this now with the following CSS:

.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!

@mbourne
Copy link

mbourne commented Jul 10, 2020

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.

yawnoc added a commit to yawnoc/yawnoc.github.io-archive-1 that referenced this issue Jul 14, 2020
@yawnoc
Copy link
Author

yawnoc commented Jul 14, 2020

I believe you can customize this now with the following CSS:

.katex { font-display: swap; }

Hmm, that doesn't validate:

1 | .katex | Property font-display doesn't exist : swap

I think font-display is only allowed when the font is loaded in the @font-face at-rule... so we'd need to provide 2 different versions of katex.css to offer the choice.

@rabinadk1
Copy link

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.

FreyaHolmer added a commit to FreyaHolmer/KaTeX that referenced this issue Mar 21, 2024
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!
@FreyaHolmer
Copy link

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 ?display=swap, but, I'm not sure how to set that up or if jsdelivr even supports it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants