Skip to content

Preload and optimally load local fonts #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

runofthemillgeek
Copy link
Contributor

@runofthemillgeek runofthemillgeek commented Apr 28, 2025

Adds template actions that will preload any local fonts used in the consuming Hugo project if present. Otherwise, will preload and use the default Typo fonts (Liberata, Monaspace).

In order to make this work while preserving theme fonts, I had to move the theme fonts into /assets/theme-fonts. If the user decides to add custom fonts into assets/fonts, then those will be preloaded and theme-fonts will not be preloaded. User will also have to create an override for assets/css/fonts.css with their own @font-face rules.

Preloading works by adding <link rel="preload"> tags into the head. Additionally, I've inlined assets/css/fonts.css into a <style> tag to help it get define/loaded earlier and not wait for the rest of the page CSS to load and render. This should hopefully improve perceived performance a bit on slower networks.

Wiki has been updated to include instructions on how to add custom fonts and use them with Typo.

Can be previewed at https://automagic.blog and the relevant changes I had to make at https://github.com/runofthemillgeek/typomagic.

NOTE: This can adversely impact users who might anyway be doing manual rel="preload"s without having assets/fonts directory or overriding assets/css/fonts.css as they'd all get the default fonts loaded anyway. But, it's fairly straightforward to disable that behavior.

@runofthemillgeek runofthemillgeek marked this pull request as ready for review April 28, 2025 23:37
@tomfran
Copy link
Owner

tomfran commented Apr 29, 2025

Really nice, I'll have a deeper look soon :)

Adds template actions that will preload any local fonts used in the
consuming Hugo project if present. Otherwise, will preload and use the
default Typo fonts (Liberata, Monaspace).

In order to make this work while preserving theme fonts, I had to move
the theme fonts into /assets/theme-fonts. If the user decides to add
custom fonts into assets/fonts, then those will be preloaded and
theme-fonts will not be preloaded. User will also have to create an
override for assets/css/fonts.css with their own `@font-face` rules.

Preloading works by adding `<link rel="preload">` tags into the head.
Additionally, I've inlined `assets/css/fonts.css` into a `<style>` tag
to help it get define/loaded earlier and not wait for the rest of the
page CSS to load and render. This should hopefully improve perceived
performance a bit on slower networks.

Wiki has been updated to include instructions on how to add custom fonts
and use them with Typo.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants