Optimise loading times with a custom font subset
Learn how to create a custom subset that only includes the necessary glyphs. Custom fonts cause flashing text in Firefox. For a gif and more details, see this issue. To fix this, tabi loads a subset of glyphs for the header. Since this (slightly) increases the initial load time, it’s a good idea to try and minimise the size of this subset. By default, there are subset files for English and Spanish characters (with a few symbols). These files are loaded when the Zola page/site is set to that language. If you’re using a custom font, either create your custom subset (see below) or disable the built-in subsets completely with Here’s how you can create a custom font subset that only includes the characters used in your header, for maximum efficiency. Install these tools: Run The script below takes a Save the script somewhere like Now you can run it with the required By default, this generates a You should place this You might change the title or menu options of your site, making the custom subset no longer useful. To automate the process of creating this file, you can integrate the script into a Git pre-commit hook that checks for changes in the Create a Make it executable with Add the following code to the file: Make sure to modify the script to match the path where you stored the Now, every time you commit changes to your Git project, the pre-commit hook will check for modifications in the By the way, if you’re interested in a way to automatically update the date of your Zola posts or compress your PNG files, check out this post. If you want to use all scripts at once (compressing PNG files, updating the date, and creating the font subset), combine their code into a single The problem
The solution
enable_subset = false
in your config.toml
. Requirements
pip install fonttools brotli
to install both. The script
config.toml
file and a font file as input, extracts the necessary characters, creates a subset of the font, and generates a CSS file containing the base64 encoded subset.#!/usr/bin/env bash
# Default output is current directory.
output_path="."
# Parse command line options
while [; do
done
# Check if -c and -f options are provided
if [; then
fi
if [; then
fi
# Check if config and font files exist.
if [; then
fi
if [; then
fi
# Extract the title and menu names from the config file.
title=
menu_names=
language_names=
# If the site is multilingual, get the menu translations.
if [; then
for; do
# Find the line with the menu name inside a [languages.*.translations] section and get the translated menus.
menu_translation=
# Add the found menu value to the translations string
menu_names+=""
done
fi
# Combine the extracted strings.
combined=""
# Get unique characters.
unique_chars=
# Create a temporary file for subset.woff2.
temp_subset=
# Create the subset.
# Remove trailing slash from output path, if present.
output_path=
# Base64 encode the temporary subset.woff2 file and create the CSS file.
base64_encoded_font=
# Remove the temporary subset.woff2 file.
Usage
~/bin/subset_font
. Make it executable with chmod +x ~/bin/subset_font
.--config
and --font
options:
custom_subset.css
file in the current directory. Use -o
or --output
to specify a different path:
custom_subset.css
file inside the static/
directory. Automating with Pre-commit Hook
config.toml
file, runs the script, and stores the resulting CSS file in the static/
directory of your site..git/hooks/pre-commit
file in your Git project, if it doesn’t already exist.chmod +x .git/hooks/pre-commit
.# Check if config.toml has been modified.
if | ; then
# Call the subset_font script.
# Add the generated subset.css file to the commit.
fi
subset_font
script. The config and font paths should work fine with tabi’s default setup.config.toml
file and automatically run the subset_font
script to update the custom_subset.css
file..git/hooks/pre-commit
file.