Skip to content
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

Fonts: Fine tune preload, retire prefetch #64

Merged
merged 4 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions core/fonts/private/GetFormat.html
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 }}
41 changes: 41 additions & 0 deletions core/fonts/private/GetPreloadTags.html
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 }}
29 changes: 10 additions & 19 deletions core/fonts/private/GetTags.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@
{{ $tags := slice }}
{{ $config := partialCached "huge/config/Get" "fonts" "fonts" }}


{{/* Preload default is "always" */}}
{{ $preload := $config.preload | default "always" }}
{{ if partialCached "huge/env/When" $preload $preload }}
{{ with partial "huge/fonts/private/GetPreloadTags" }}
{{ $tags = $tags | append . }}
{{ end }}
{{ end }}


{{/* We generate a resource from Template (to facilitate future manipulation/http request) */}}
{{ $fontface := resources.Get "/huge_only/fontface.css" | resources.ExecuteAsTemplate "fontface.css" "no_context" }}
{{/* Add the tag with Resource's .Content for "inner" */}}
Expand All @@ -26,23 +36,4 @@
}}
{{ $tags = $tags | append $fontface_tag }}

{{/* If prefetching is enabled on the package's config */}}
{{ $prefetch := $config.prefetch | default "always" }}
{{ if partialCached "huge/env/When" $prefetch $prefetch }}
{{/* 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" }}
{{ range .resources }}
{{ $tag := dict
"name" "link"
"attr" (dict
"href" .RelPermalink
"as" "font"
"crossorigin" " "
"rel" "prefetch"
)}}
{{ $tags = $tags | append $tag }}
{{ end }}
{{ end }}
{{ end }}

{{ return $tags }}
5 changes: 4 additions & 1 deletion core/fonts/private/ParseFont.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
{{ $font := . }}
{{ with .file }}
{{ with resources.Match (print "/" . ".*") }}
{{ $font = merge $font (dict "resources" .) }}
{{/* This ensures woff2 and woff are declared first, it seems we can't realy rely on SubType (cloufflare as '' for woff2...) */}}
{{ with sort . "Name" "desc" }}
{{ $font = merge $font (dict "resources" (sort . "Name" "desc")) }}
{{ end }}
{{ else }}
{{ partial "huge/console/warn" (printf "We did not find matching font files for basename `%s`.\nFont files should be added to the project's `assets` directory and match the relative path set in the font's settings." .) }}
{{ end }}
Expand Down
11 changes: 6 additions & 5 deletions core/fonts/private/ParseFontface.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@
{{ end }}

{{ range . }}
{{ $format := .MediaType.SubType }}
{{ $irregular := dict "vnd.ms-fontobject" "embedded-opentype" "ttf" "truetype" "font-sfnt" "truetype" "otf" "opentype" }}
{{ with index $irregular $format }}
{{ $format = . }}
{{ end }}
{{ $format := partialCached "huge/fonts/private/GetFormat" . .MediaType.SubType }}
{{ $css_srcs = $css_srcs | append (printf `url("%s") format("%s")` .RelPermalink $format) }}
{{ end }}
{{ with $css_srcs }}
Expand All @@ -62,6 +58,7 @@
{{ range $properties }}
{{ $properties = $properties | append (print "font-" .) }}
{{ end }}

{{ range $property := $properties }}
{{ with index $ . }}
{{ $key := $property }}
Expand All @@ -73,4 +70,8 @@
{{ end }}
{{ end }}

{{ if not (index ($s.Get "font") "font-display") }}
{{ $s.SetInMap "font" "font-display" "swap" }}
{{ end }}

{{ return $s.Get "font" }}