Skip to content

Commit 2e87263

Browse files
Preload and optimally load local fonts
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.
1 parent fdfbe02 commit 2e87263

File tree

11 files changed

+53
-8
lines changed

11 files changed

+53
-8
lines changed

assets/css/fonts.css

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
11
@font-face {
22
font-family: 'Literata';
3-
src: url('/fonts/Literata/Literata-Light.woff2') format('woff2');
3+
src: url('/theme-fonts/Literata/Literata-Light.woff2') format('woff2');
44
font-weight: light;
55
font-style: normal;
66
font-display: swap;
77
}
88

99
@font-face {
1010
font-family: 'Literata';
11-
src: url('/fonts/Literata/Literata-LightItalic.woff2') format('woff2');
11+
src: url('/theme-fonts/Literata/Literata-LightItalic.woff2') format('woff2');
1212
font-weight: light;
1313
font-style: italic;
1414
font-display: swap;
1515
}
1616

1717
@font-face {
1818
font-family: 'Literata';
19-
src: url('/fonts/Literata/Literata-SemiBold.woff2') format('woff2');
19+
src: url('/theme-fonts/Literata/Literata-SemiBold.woff2') format('woff2');
2020
font-weight: bold;
2121
font-style: normal;
2222
font-display: swap;
2323
}
2424

2525
@font-face {
2626
font-family: 'Literata';
27-
src: url('/fonts/Literata/Literata-SemiBoldItalic.woff2') format('woff2');
27+
src: url('/theme-fonts/Literata/Literata-SemiBoldItalic.woff2') format('woff2');
2828
font-weight: bold;
2929
font-style: italic;
3030
font-display: swap;
3131
}
3232

3333
@font-face {
3434
font-family: 'Monaspace';
35-
src: url('/fonts/Monaspace/MonaspaceArgon-Regular.woff') format('woff');
35+
src: url('/theme-fonts/Monaspace/MonaspaceArgon-Regular.woff') format('woff');
3636
font-weight: normal;
3737
font-style: normal;
3838
font-display: swap;
3939
}
4040

4141
@font-face {
4242
font-family: 'Monaspace';
43-
src: url('/fonts/Monaspace/MonaspaceArgon-SemiBold.woff') format('woff');
43+
src: url('/theme-fonts/Monaspace/MonaspaceArgon-SemiBold.woff') format('woff');
4444
font-weight: bold;
4545
font-style: normal;
4646
font-display: swap;

layouts/partials/head.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
{{ $faviconPath := (.Site.Params.faviconPath | default "" | absURL) }}
88

9+
{{- partialCached "head/preload.html" . }}
910
<link rel="icon" type="image/ico" href="{{ $faviconPath }}/favicon.ico">
1011
<link rel="icon" type="image/png" sizes="16x16" href="{{ $faviconPath }}/favicon-16x16.png">
1112
<link rel="icon" type="image/png" sizes="32x32" href="{{ $faviconPath }}/favicon-32x32.png">

layouts/partials/head/css.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
(resources.Get "css/reset.css")
44
(resources.Get "css/vars.css")
55
(resources.Get "css/utils.css")
6-
(resources.Get "css/fonts.css")
76
(resources.Get "css/main.css")
87
(resources.Get "css/custom.css")
98
(resources.Get "css/colors/default.css")
@@ -23,4 +22,10 @@
2322
| fingerprint
2423
}}
2524

25+
<style>
26+
{{- $fontsCss := resources.Get "css/fonts.css"
27+
| resources.ExecuteAsTemplate "css/fonts.css" $
28+
}}
29+
{{- $fontsCss.Content | safeCSS }}
30+
</style>
2631
<link rel="stylesheet" href="{{ $combined.RelPermalink }}" media="all">

layouts/partials/head/preload.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{{- /* Generate preload tags for all fonts within assets/fonts directory */}}
2+
3+
{{- $fonts := or (resources.Match "fonts/**.{eot,ttf,otf,woff,woff2,svg}") (resources.Match "theme-fonts/**.{eot,ttf,otf,woff,woff2,svg}") }}
4+
{{- range $fonts }}
5+
<link rel="preload" as="font" href="{{ .RelPermalink }}" type="{{ .MediaType.Type }}" crossorigin>
6+
{{- end }}

0 commit comments

Comments
 (0)