Problem/Motivation
The "Inter" font is loaded twice. Once for the Navigation module, and once for the Admin theme.
Changes
- Added a
core/fontlibrary. It provides Inter v4 (variable), taken from the Navigation module. - Split the font into unicode-range subsets in
core/misc/fonts/: latin, latin-ext, cyrillic, cyrillic-ext, greek, greek-ext, vietnamese, and symbols. - Preloaded
inter-latin.woff2through the libraryfontskey. - Added
inter-variables.css. It defines--font-interand--font-inter-size-adjust. - Added metric-adjusted fallback faces for Helvetica Neue and Arial, regular and bold. They use
size-adjust,ascent-override,descent-override
andline-gap-override. - Navigation and Default Admin now depend on
core/font. They no longer ship their own Inter files.
Testing steps
- Walk through the theme as normal. There should be minimal visual differences (v4 of the font is oh-so-slightly-narrower than the previous version that the theme used)
- Look at DevTools. The font should be preloaded.
- Ensure the proper font is pulled down (this should be the latin variation for most people)
Performance improvements
(note these were generated by Claude.
Measured with Playwright and headless Chrome for Testing. Values are medians of 5 cold loads per page. The browser cache was off, and each run used a new session. Viewport: 1440×900. Network: Chrome DevTools "Slow 4G" preset. Before: main. After: the issue branch.
| Before | After | |
|---|---|---|
| Font files | 2: default_admin/font/inter.woff2 (219.5 KB), navigation/assets/fonts/inter-var.woff2 (337.5 KB) |
1: core/misc/fonts/inter-latin.woff2 (103.6 KB) |
| Font preloads | 2 | 1 |
| Font bytes | 557.0 KB | 103.6 KB (−81%) |
Total bytes, /admin/content |
868 KB | 416 KB (−52%) |
Total bytes, /node/add/page |
749 KB | 297 KB (−60%) |
| Page | Metric (Slow 4G) | Before | After | Change |
|---|---|---|---|---|
/admin/content |
FCP / LCP | 3756 ms | 3664 ms | −92 ms |
| Fonts loaded | 6825 ms | 4225 ms | −2600 ms (−38%) | |
| Load event | 6842 ms | 4972 ms | −1870 ms (−27%) | |
| CLS | 0.0087 | 0.0062 | −0.0025 | |
/node/add/page |
FCP / LCP | 2844 ms | 2764 ms | −80 ms |
| Fonts loaded | 5892 ms | 3308 ms | −2584 ms (−44%) | |
| Load event | 5904 ms | 3314 ms | −2590 ms (−44%) | |
| CLS | 0.0001 | 0.0000 | −0.0001 |
- LCP is a text element and equals FCP on both branches. With
font-display: swap, paint does not wait for fonts. - Without throttling, timings on the local ddev site are within noise on both branches.
- The test pages contain English text only, so only
inter-latin.woff2loads. Pages with Cyrillic, Greek or Vietnamese text also load those subsets (17–41 KB each). - CSS and JS aggregation was off. This adds the same bytes to both totals.
Visual differences
There are visual differences, which come from moving it from Inter v3 (which was used by default_admin) to Inter v4 (which was used by Navigation). Most of the text-width change comes from v4's new optical size axis. Navigation already used v4, so it looks the same as before.
In practice:
- Body and form text (13–14px) is almost unchanged.
- 16px text is about 1% narrower.
- Headings are noticeably narrower and tighter. A 32px page title is 6–9% narrower.
AI Disclosure
I used Claude for the majority of this work. I manually reviewed and tested the code personally
Issue fork drupal-3578936
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
godotislateCore Navigation uses inter-var as well, so it'd make sense to share between default_admin and navigation somehow and make it available to Mercury.
Per @catch in slack:
Comment #3
mherchelYeah, this definitely needs to be tackled.
We need some discussion on how, though.
Requirements
My first thought is to move the font file + accompanying CSS into core.libraries.yml. Then both the module and theme (and any other contrib) can reference them. Thoughts?
Comment #4
catchI'd thought about moving the font itself, but not the CSS yet, on the basis that if the font file is cached different font declarations would probably not result in a double load. But adding a core library for the CSS itself makes a lot of sense. If we subset the font it will increase the complexity of the CSS too so much easier to maintain in one place, will also mean preloading is centralised too etc.
Comment #5
pameeela commentedComment #6
bernardm28 commentedRecommended architecture
Instead of:
themes/contrib/gin/dist/media/font/inter.woff2themes/.../fonts/inter-var.woff2core/themes/default_admin/font/inter.woff2have:
core/assets/vendor/inter/
├── InterVariable.woff2
└── inter.css
or equivalent.
Then expose it through something like:
# core/core.libraries.yml
And:
Inter's current variable font supports the wght axis from 100–900, so that declaration is appropriate.
Then consumers depend on:
rather than defining their own copy.
So:
core/navigation
↓
core/inter
↑
default_admin
↑
Gin
↑
Mercury
The important benefit is that all of them end up requesting something like:
/core/assets/vendor/inter/InterVariable.woff2
the exact same URL.
That is what actually eliminates the network duplication.
Why merely changing Gin isn't enough
Suppose you change this: /gin/fonts/inter.woff2 to: /gin/fonts/inter-var.woff2
while Mercury has: /mercury/fonts/inter-var.woff2
You still have:
GET /themes/contrib/gin/fonts/inter-var.woff2
GET /themes/mercury/fonts/inter-var.woff2
Even if those files are byte-for-byte identical, they're different resources from the browser's perspective.
So you'd potentially still download Inter twice.
This is why the comment on the core issue suggesting a shared core.libraries.yml asset is stronger than simply standardizing filenames.
Then subset it
Once there is one canonical Inter library, then subsetting becomes very attractive.
The Drupal discussion mentions that Inter contains more than 2,000 glyphs and specifically proposes putting non-Roman glyphs into separate subsets.
For example:
core/assets/vendor/inter/
├── inter-latin.woff2
├── inter-latin-ext.woff2
├── inter-cyrillic.woff2
└── inter.css
With:
Then another @font-face handles Cyrillic, etc.
The browser only downloads the ranges needed for the page.
For example:
The browser handles the rest.
I'd consider this phase 2, though.
Phase 1 should be deduplication
The order I'd recommend for the Drupal issue is:
Standardize on Inter Variable.
Move the canonical font file into core.
Move the canonical @font-face CSS into core too.
Navigation/default_admin depend on that library.
Gin removes its bundled inter.woff2 and uses the core library.
Mercury uses the same library rather than its own copy.
Verify only one Inter request when combinations of those components are present.
Then add Unicode subsets.
That also satisfies the requirements raised in the issue:
same font when Navigation + Admin load, Navigation alone, or Admin alone.
Looks after chatting with AI.
The steps be:
Comment #7
mherchelcore/misc/fontsfontComment #8
mherchelComment #10
mherchelComment #11
mherchelComment #12
mherchelComment #13
mherchelComment #14
mherchelComment #15
mherchelComment #16
mherchelComment #17
mherchelComment #18
mherchelComment #19
catchIs there something we can link to for the ascent/descent/line-gap overrides? I've seen this general technique discussed (making a system font take the same horizontal/vertical space as a web font so that there is no render blocking from the font) but not sure where to point. Ideally we'd link to both the technique and how the numbers were derived.
Also is there a way to do screenshots for before/after Inter loads to see what the effect is like? Maybe deleting Inter from disk would work so it ends up being a 404?
Comment #20
mherchelThe way I tested this is to set the
font-intervariable to the fallback. If you add the following to line 84 of inter-variables.css, you can then use dev tools to toggle on/off inter, and also try the fallback fonts vs the non-modified fallback fonts.Comment #21
mherchelI pointed Claude at https://developer.chrome.com/blog/font-fallbacks to do this, and had it use Chrome MCP server to use devtools to adjust the fonts to be as close as possible.
I'm also attaching a python script that it used to derive the numbers.
Ready for re-review!
Comment #22
mherchelAnother option (that i've done before) is use
font-display: blockinstead ofswap.When using that, the browser will wait for the font for three seconds. Only then will it use the fallback instead of immediately showing the fallback.
Comment #23
markconroy commentedI haven’t tested this in a browser, but the MR code looks spot on to me.
If I had one gripe (but this might be more about all our CSS variables) it’s that instead of --font-inter we should preface all our variable names with --drupal-* such as --drupal-font-inter to lessen the possibility of variable name clashing.
I know with LocalGov Drupal, if we use Claro as a base theme, our --color-button clashes with Claro’s and ends up making node edit form buttons a dark purple with (unreadable) black text
Comment #24
mherchelChanges made! I also removed the unused
--font-inter-size-adjustvariable.Comment #25
markconroy commentedLGTM
Comment #26
catchOne comment nit and one real question on the MR.
Comment #27
mherchelChanges made!
Comment #28
markconroy commentedLGTM (again!)
Comment #29
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. The merge request has merge conflicts and cannot be merged. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #30
alexpottSpotted a double word in the MR comments that needs fixing too.
Comment #31
catchRebased for performance tests and resolved @alexpott's comment suggestion. Aiming to commit this once the pipeline is green again, moving to RTBC for now.
Comment #32
alexpottCommitted and pushed 3875e93cfd9 to main. Thanks!
Comment #35
mherchelGoing to backport this to 12.0.x, 11.x, and 11.5.x.
Running into conflicts with the perf tests, so I'm going to set up some MRs first to ensure the tests pass.
Comment #44
mherchelComment #46
catchOpened https://git.drupalcode.org/project/mercury/-/work_items/3592167
Comment #48
godotislateConsulted with other RMs, and this could use a CR documenting the new core/font library and how to use it.
Comment #49
godotislateComment #50
mherchelCreated CR at https://www.drupal.org/node/3642246
Thanks!