fix: Avoids layout jitter while fonts load
The headings jumped when Jersey 10 arrived after the first paint: the monospace fallback is much wider, so the heading chips changed their width, and the page transition made it visible on every language switch. Reproduced with revalidated or slow fonts (hugo server sends no cache headers, so it revalidates them on every navigation) in Chromium, WebKit and, on cold loads, Firefox. In Safari the body text shifted as well, because its ui-monospace is the wider SF Mono. - Embeds the Latin subset of Jersey 10 in the fonts stylesheet. - Preloads Hack Regular and Bold. - Generates the @font-face rules and the preload links from one fingerprinted template (assets/css/fonts.css.tmpl, head/fonts.html), so their URLs match and also work below a sub path like the PR previews. - Drops ui-monospace and SF Mono from the body font stack: Menlo has the same width as Hack. Frames with shifted elements while switching language on the tours page, with revalidated or slow fonts: 9 to 92 before, none after (Chromium and WebKit, desktop and mobile). Firefox cold loads: up to 48 before, none painted after. Rendering after loading is unchanged. Trade-off: the first paint of a cold load on a slow connection (Slow 4G) is about 0.26 s later; later navigations are not affected. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
59e7d66603
commit
97cb34b576
2 changed files with 91 additions and 54 deletions
|
|
@ -1,3 +1,4 @@
|
|||
{{ partial "head/fonts.html" . }}
|
||||
{{- /* Browser UI colours, the same as the page backgrounds in assets/css/_custom.css. */ -}}
|
||||
<meta name="theme-color" media="(prefers-color-scheme: light)" content="#ebedf0">
|
||||
<meta name="theme-color" media="(prefers-color-scheme: dark)" content="#001300">
|
||||
|
|
|
|||
25
layouts/_partials/head/fonts.html
Normal file
25
layouts/_partials/head/fonts.html
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
{{- /*
|
||||
Self-hosted fonts: Jersey 10 for headings and Hack for everything else (assets/fonts, licences in
|
||||
static/fonts). Their @font-face rules are generated from assets/css/fonts.css.tmpl instead of
|
||||
being part of the main stylesheet. A web font that arrives after the first paint swaps in and
|
||||
moves the page: Jersey 10 is far narrower than the monospace fallback, so the heading chips
|
||||
changed their width, and the page transition made that visible on every language switch.
|
||||
|
||||
- The Latin subset of Jersey 10 is embedded in the stylesheet, so it is there at the first paint
|
||||
whatever the connection, and without a request that the browser would have to revalidate.
|
||||
- Hack is preloaded, so the body text is not first drawn in a fallback font of a different
|
||||
character width (SF Mono in Safari, Consolas on Windows). Its URL here and in @font-face come
|
||||
from the same fingerprinted resource, so they are identical (and correct below a sub path, as
|
||||
on the PR previews).
|
||||
*/ -}}
|
||||
{{- with resources.Get "css/fonts.css.tmpl" | resources.ExecuteAsTemplate "css/fonts.tmpl.css" . }}
|
||||
{{- /* The executed template has no media type (from ".tmpl"), which minify needs. */ -}}
|
||||
{{- with resources.FromString "css/fonts.css" .Content | minify | fingerprint }}
|
||||
<link rel="stylesheet" href="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}" crossorigin="anonymous">
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- range slice "Hack-Regular" "Hack-Bold" }}
|
||||
{{- with resources.Get (printf "fonts/%s.woff2" .) | fingerprint }}
|
||||
<link rel="preload" href="{{ .RelPermalink }}" as="font" type="font/woff2" crossorigin>
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
Loading…
Reference in a new issue