Challenge
Font subsetting is a web performance technique that removes unnecessary characters from fonts. It makes fonts smaller and faster.
Previously, we were doing font subsetting during every build:
|
"install-build-deps": "pip3 install fonttools brotli zopfli || pip install fonttools brotli zopfli", |
|
"build-gatsby": "gatsby build", |
|
"optimize-fonts": "yarn optimize-fonts:landings && yarn optimize-fonts:content", |
|
"optimize-fonts:landings": "cd public && find . -type f -name '*.html' | grep -v -E '(blog|talks)' | xargs subfont --root . --canonical-root https://3perf.com/ --formats woff2 --in-place --inline-css", |
|
"optimize-fonts:content": "cd public && find . -type f -name '*.html' | grep -E '(blog|talks)' | xargs subfont --root . --canonical-root https://3perf.com/ --formats woff2 --in-place --inline-css", |
However, that a) was slow (2+ minutes every time the site is built), b) got broken recently, even though we install all Python dependencies:
11:37:02 PM: ℹ INFO: Local subsetting is not possible because fonttools are not installed. Falling back to only subsetting Google Fonts. Run `pip install fonttools brotli zopfli` to enable local font subsetting
11:37:02 PM: ℹ INFO: Unoptimised fonts:
11:37:02 PM: - static/Montserrat-Bold-eed2cab78bd948a1822f8e19b79b1edc.woff2
11:37:02 PM: - ...
Due to this, I removed this font subsetting implementation in 433750e.
It’d be great to bring back font subsetting, even if simplified.
Suggested solution
Update: let’s just use Fontsource instead. Fontsource subsets fonts out of the box → #57 (comment)
Right now, we use Montserrat, Merriweather, and Fira Code fonts. Most pages use just normal, italic, and bold versions of these fonts, but there are exceptions. (Eg /
, /content
and maybe some other pages use Montserrat Black.) Also, 99-100% of the site content is latin.
Given this, I’d probably try subsetting fonts as follows:
Extra notes:
- It would be great if you could save the above steps as a script! With the script, if eg a new version of Montserrat is released, and we need to update the font files, we could just replace the source files and re-run the script.
- Feel free to remove the
eot
versions of font files. We don’t need to support IE.
Relevant links
Tools:
Guides/docs: