Skip to content

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.
@@ -0,0 +1,6 @@
{{- /* Generate preload tags for all fonts within assets/fonts directory */}}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also generates preload tags for theme fonts right? Just to double check.

@font-face {
font-family: 'Literata';
src: url('/fonts/Literata/Literata-Light.woff2') format('woff2');
src: url('/theme-fonts/Literata/Literata-Light.woff2') format('woff2');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to split fonts I would use theme-fonts and user-fonts.

Typo can preload your local font files if you configure them appropriately.

1. Create `assets/themes/fonts` directory within your Hugo site project and copy over your custom fonts into this directory.
2. Create `assets/css/fonts.css` file and define [`@font-face` rules](https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face) for your custom fonts. You can refer to font paths like `/fonts/some-font-file-name.extension`.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct? assets/css/fonts.css already exists and contains the default fonts definitions. Am I missing something?

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