From e237cf2a0670ec9aa8f55d688fca6633c564f876 Mon Sep 17 00:00:00 2001 From: Vincent Mahnke Date: Sun, 8 Nov 2026 13:11:35 +0100 Subject: [PATCH] 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> --- assets/css/_custom.css | 4 ++- assets/css/_fonts.css | 55 ++-------------------------- assets/css/fonts.css.tmpl | 60 +++++++++++++++++++++++++++++++ layouts/_partials/head/extra.html | 1 + layouts/_partials/head/fonts.html | 25 +++++++++++++ 5 files changed, 91 insertions(+), 54 deletions(-) create mode 100644 assets/css/fonts.css.tmpl create mode 100644 layouts/_partials/head/fonts.html diff --git a/assets/css/_custom.css b/assets/css/_custom.css index a9759e9..fbd0ff6 100644 --- a/assets/css/_custom.css +++ b/assets/css/_custom.css @@ -36,7 +36,9 @@ /* Jersey 10 is a small pixel font; it needs about this much more size to match Hack. */ --display-scale: 1.45; - --ff-body: 'Hack', ui-monospace, 'SF Mono', Menlo, Consolas, monospace; + /* On macOS, Menlo has the same character width as Hack, so text doesn't move when Hack takes + over. Not ui-monospace: that is SF Mono in Safari, which is wider. */ + --ff-body: 'Hack', Menlo, Consolas, monospace; --ff-headings: var(--ff-body); --ff-alt: var(--ff-body); --ff-monospace: var(--ff-body); diff --git a/assets/css/_fonts.css b/assets/css/_fonts.css index 504481b..783d6ee 100644 --- a/assets/css/_fonts.css +++ b/assets/css/_fonts.css @@ -1,53 +1,2 @@ -/* 40C3 "MODEL_CITIZENS" styleguide fonts, self-hosted (SIL OFL / MIT, see static/fonts). - Jersey 10: headings. Hack: body text and subheads. - Hack is subset to Latin (Latin-1, Latin Extended-A, general punctuation). */ - -@font-face { - font-family: 'Jersey 10'; - src: url('../fonts/Jersey10-latin-ext.woff2') format('woff2'); - font-weight: 400; - font-style: normal; - font-display: swap; - unicode-range: U+0100-02BA, U+02BD-02C5, U+02C7-02CC, U+02CE-02D7, U+02DD-02FF, U+0304, U+0308, U+0329, U+1D00-1DBF, U+1E00-1E9F, U+1EF2-1EFF, U+2020, U+20A0-20AB, U+20AD-20C0, U+2113, U+2C60-2C7F, U+A720-A7FF; -} - -@font-face { - font-family: 'Jersey 10'; - src: url('../fonts/Jersey10-latin.woff2') format('woff2'); - font-weight: 400; - font-style: normal; - font-display: swap; - unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD; -} - -@font-face { - font-family: 'Hack'; - src: url('../fonts/Hack-Regular.woff2') format('woff2'); - font-weight: 400; - font-style: normal; - font-display: swap; -} - -@font-face { - font-family: 'Hack'; - src: url('../fonts/Hack-Italic.woff2') format('woff2'); - font-weight: 400; - font-style: italic; - font-display: swap; -} - -@font-face { - font-family: 'Hack'; - src: url('../fonts/Hack-Bold.woff2') format('woff2'); - font-weight: 700; - font-style: normal; - font-display: swap; -} - -@font-face { - font-family: 'Hack'; - src: url('../fonts/Hack-BoldItalic.woff2') format('woff2'); - font-weight: 700; - font-style: italic; - font-display: swap; -} +/* The @font-face rules are not part of this bundle: head/fonts.html generates them from + assets/css/fonts.css.tmpl, and explains why. */ diff --git a/assets/css/fonts.css.tmpl b/assets/css/fonts.css.tmpl new file mode 100644 index 0000000..1830d9a --- /dev/null +++ b/assets/css/fonts.css.tmpl @@ -0,0 +1,60 @@ +{{- /* Rendered by layouts/_partials/head/fonts.html, which explains why. */ -}} +{{- $url := dict -}} +{{- range slice "Jersey10-latin-ext" "Hack-Regular" "Hack-Italic" "Hack-Bold" "Hack-BoldItalic" -}} + {{- $url = merge $url (dict . (resources.Get (printf "fonts/%s.woff2" .) | fingerprint).RelPermalink) -}} +{{- end -}} +{{- $jersey := (resources.Get "fonts/Jersey10-latin.woff2").Content | base64Encode -}} +/* 40C3 "MODEL_CITIZENS" styleguide fonts, self-hosted (SIL OFL / MIT, see static/fonts). + Jersey 10: headings. Hack: body text and subheads. + Hack is subset to Latin (Latin-1, Latin Extended-A, general punctuation). */ + +/* The Latin subset of Jersey 10 is embedded, so German and English headings are never drawn in the fallback font. */ +@font-face { + font-family: 'Jersey 10'; + src: url('data:font/woff2;base64,{{ $jersey }}') format('woff2'); + font-weight: 400; + font-style: normal; + font-display: swap; + unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD; +} + +@font-face { + font-family: 'Jersey 10'; + src: url('{{ index $url "Jersey10-latin-ext" }}') format('woff2'); + font-weight: 400; + font-style: normal; + font-display: swap; + unicode-range: U+0100-02BA, U+02BD-02C5, U+02C7-02CC, U+02CE-02D7, U+02DD-02FF, U+0304, U+0308, U+0329, U+1D00-1DBF, U+1E00-1E9F, U+1EF2-1EFF, U+2020, U+20A0-20AB, U+20AD-20C0, U+2113, U+2C60-2C7F, U+A720-A7FF; +} + +@font-face { + font-family: 'Hack'; + src: url('{{ index $url "Hack-Regular" }}') format('woff2'); + font-weight: 400; + font-style: normal; + font-display: swap; +} + +@font-face { + font-family: 'Hack'; + src: url('{{ index $url "Hack-Italic" }}') format('woff2'); + font-weight: 400; + font-style: italic; + font-display: swap; +} + +@font-face { + font-family: 'Hack'; + src: url('{{ index $url "Hack-Bold" }}') format('woff2'); + font-weight: 700; + font-style: normal; + font-display: swap; +} + +@font-face { + font-family: 'Hack'; + src: url('{{ index $url "Hack-BoldItalic" }}') format('woff2'); + font-weight: 700; + font-style: italic; + font-display: swap; +} diff --git a/layouts/_partials/head/extra.html b/layouts/_partials/head/extra.html index ee579c3..957c2d7 100644 --- a/layouts/_partials/head/extra.html +++ b/layouts/_partials/head/extra.html @@ -1,3 +1,4 @@ +{{ partial "head/fonts.html" . }} {{- /* Browser UI colours, the same as the page backgrounds in assets/css/_custom.css. */ -}} diff --git a/layouts/_partials/head/fonts.html b/layouts/_partials/head/fonts.html new file mode 100644 index 0000000..0318665 --- /dev/null +++ b/layouts/_partials/head/fonts.html @@ -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 }} + + {{- end }} +{{- end }} +{{- range slice "Hack-Regular" "Hack-Bold" }} + {{- with resources.Get (printf "fonts/%s.woff2" .) | fingerprint }} + + {{- end }} +{{- end }}