How-to ·
Self-hosting web fonts on a static site
Loading fonts from Google’s servers sends every visitor’s IP address to a third party. For a personal site run from Germany, serving the fonts yourself is the simpler choice — and it only takes three steps.
Why bother
In January 2022, a regional court in Munich ruled that a website which embedded Google Fonts from Google’s servers, without the visitor’s consent, had passed on the visitor’s IP address unlawfully, and awarded damages. Since then, self-hosting has become the common recommendation for sites run from Germany. I am not a lawyer, but self-hosting removes the question entirely — and it is also slightly faster, because the browser no longer has to connect to another domain before it can render text.
1. Get the font files
The fonts used here — DM Sans, DM Mono and Playfair Display — are published under the SIL Open Font License in Google’s google/fonts repository on GitHub. The licence allows self-hosting, as long as the licence text travels with the files, so it sits next to them in assets/fonts/.
2. Subset and convert to WOFF2
The source files contain far more characters than an English-language site needs. The pyftsubset tool from fontTools keeps only the Latin character ranges plus a few symbols, and writes compressed WOFF2:
pip install fonttools brotli
pyftsubset "DMSans[opsz,wght].ttf" \
--unicodes="U+0000-017F,U+2000-206F,U+20AC,U+20B9,U+2190-2199" \
--layout-features="*" \
--flavor=woff2 \
--output-file=dm-sans-roman.woff2
DM Sans and Playfair Display are variable fonts, so a single file covers every weight the site uses. I also limited the weight axis to the range actually needed, which trims the files further.
3. Declare and load them
Each file gets an @font-face rule with a relative URL, so it works both on the GitHub Pages address and on a custom domain later:
@font-face {
font-family: 'DM Sans';
src: url('fonts/dm-sans-roman.woff2') format('woff2');
font-weight: 400 700;
font-display: swap;
}
font-display: swap shows text immediately in a fallback font while the web font loads. Every family also has a sensible system-font fallback, and the main body font is preloaded so the swap is rarely visible.
The result
Five WOFF2 files, about 190 KB together, and no requests to any third-party server. You can check this on any page: open the browser’s developer tools, switch to the Network tab and reload. Every request goes to this site and nowhere else.