-
Notifications
You must be signed in to change notification settings - Fork 165
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
base: main
Are you sure you want to change the base?
Preload and optimally load local fonts #132
Conversation
773591e to
2e87263
Compare
2e87263 to
689ba70
Compare
|
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.
689ba70 to
bc2ea4c
Compare
| @@ -0,0 +1,6 @@ | |||
| {{- /* Generate preload tags for all fonts within assets/fonts directory */}} | |||
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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`. |
There was a problem hiding this comment.
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?
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-facerules.Preloading works by adding
<link rel="preload">tags into the head. Additionally, I've inlinedassets/css/fonts.cssinto 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 havingassets/fontsdirectory or overridingassets/css/fonts.cssas they'd all get the default fonts loaded anyway. But, it's fairly straightforward to disable that behavior.