Skip to content

Commit

Permalink
Allow per-declaration preload setting (#66)
Browse files Browse the repository at this point in the history
Fixes #65
  • Loading branch information
regisphilibert authored Mar 30, 2022
1 parent 3e88d49 commit c7ac985
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 25 deletions.
37 changes: 20 additions & 17 deletions core/fonts/private/GetPreloadTags.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,28 @@
{{ $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 }}
{{ if .preload }}
{{/* 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 }}
{{ end }}


{{ return $tags }}
10 changes: 3 additions & 7 deletions core/fonts/private/GetTags.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,11 @@
{{ $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 }}
{{/* We add the preload tags first */}}
{{ with partialCached "huge/fonts/private/GetPreloadTags" "GetPreloadTags" }}
{{ $tags = $tags | append . }}
{{ 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 Down
18 changes: 17 additions & 1 deletion core/fonts/private/ParseFont.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
@return Map
String (.family)
String (.file)
Resources (.resources)
Slice (.local)
Boolean (.preload)
Resources (.resources)
String (.family)?
String (.weight)?
String (.style)?
Expand All @@ -24,6 +25,8 @@

*/}}
{{ $font := . }}
{{ $config := partialCached "huge/config/Get" "fonts" "fonts" }}

{{ with .file }}
{{ with resources.Match (print "/" . ".*") }}
{{/* This ensures woff2 and woff are declared first, it seems we can't realy rely on SubType (cloufflare as '' for woff2...) */}}
Expand All @@ -39,4 +42,17 @@
{{ $font = merge $font (dict "local" .) }}
{{ end }}

{{/* We'll charge the parsed font with its `preload` preference. */}}
{{ $preload := false }}
{{/* First we check for the global setting */}}
{{ $preload_setting := $config.preload | default "always" }}
{{ if partialCached "huge/env/When" $preload_setting $preload_setting }}
{{ $preload = . }}
{{ end }}
{{/* if preload is set (can be `false`) on the declaration itself, it overwrites global's */}}
{{ if isset $ "preload" }}
{{ $preload = $.preload }}
{{ end }}
{{ $font = merge $font (dict "preload" $preload) }}

{{ return $font }}

0 comments on commit c7ac985

Please sign in to comment.