-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from theNewDynamic/63-preload-fonts
Fonts: Fine tune `preload`, retire `prefetch`
- Loading branch information
Showing
5 changed files
with
94 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{{/* | ||
GetFormat | ||
Retrieves the font format of a given resource | ||
|
||
@author @regisphilibert | ||
|
||
@context Resource (.) | ||
|
||
@access private | ||
|
||
@returns String | ||
|
||
*/}} | ||
{{ $format := "woff2" }} | ||
{{/* It appears CloudFlare produces an empty string when calling .MediaType.SubType on a woff2... | ||
As this is by far the best font format these days in terms of support and optimization, | ||
making it default to ensure it is not botched by cloudflare is "ok". | ||
*/}} | ||
{{ with .MediaType.SubType }} | ||
{{ $format = . }} | ||
{{ end }} | ||
{{ $irregular := dict | ||
"vnd.ms-fontobject" "embedded-opentype" | ||
"font-woff" "woff" | ||
"ttf" "truetype" | ||
"font-sfnt" "truetype" | ||
"otf" "opentype" | ||
}} | ||
{{ with index $irregular $format }} | ||
{{ $format = . }} | ||
{{ end }} | ||
|
||
{{ return $format }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{{/* | ||
huge/fonts/private/GetPreloadTags | ||
Construct the tags data based on preload settings. | ||
|
||
@author @regisphilibert | ||
|
||
@context Any (.) | ||
|
||
@access private | ||
|
||
@returns Slice of Maps (complying to https://github.com/theNewDynamic/hugo-module-tnd-tags/blob/main/README.md) | ||
|
||
@uses | ||
- huge/fonts/private/GetFonts | ||
- huge/fonts/private/GetFormat | ||
*/}} | ||
{{ $tags := slice }} | ||
{{/* We list all the fonts in order to produce the data for the "prefetch" tags on all its required files */}} | ||
{{ range partialCached "huge/fonts/private/GetFonts" "GetFonts" }} | ||
{{/* We want to print <link rel="preload" href="font.woff2" as="font" type="font/woff2" crossorigin> */}} | ||
{{ range .resources }} | ||
{{ $format := partialCached "huge/fonts/private/GetFormat" . .MediaType.SubType }} | ||
{{/* For now it makes sens to restrict preload to the most supported file format. | ||
Future release will allow other file formats to be preloaded. | ||
*/}} | ||
{{ if eq $format "woff2" }} | ||
{{ $tag := dict | ||
"name" "link" | ||
"attr" (dict | ||
"href" .RelPermalink | ||
"as" "font" | ||
"type" "font/woff2" | ||
"rel" "preload" | ||
"crossorigin" "anonymous" | ||
)}} | ||
{{ $tags = $tags | append $tag }} | ||
{{ end }} | ||
{{ end }} | ||
{{ end }} | ||
|
||
{{ return $tags }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters