diff --git a/core/fonts/private/GetPreloadTags.html b/core/fonts/private/GetPreloadTags.html index 8475937..b3b69ef 100644 --- a/core/fonts/private/GetPreloadTags.html +++ b/core/fonts/private/GetPreloadTags.html @@ -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 */}} - {{ 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 */}} + {{ 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 }} \ No newline at end of file diff --git a/core/fonts/private/GetTags.html b/core/fonts/private/GetTags.html index 5b968d3..5b83a3c 100644 --- a/core/fonts/private/GetTags.html +++ b/core/fonts/private/GetTags.html @@ -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" */}} diff --git a/core/fonts/private/ParseFont.html b/core/fonts/private/ParseFont.html index c527ad7..7aa2333 100644 --- a/core/fonts/private/ParseFont.html +++ b/core/fonts/private/ParseFont.html @@ -11,8 +11,9 @@ @return Map String (.family) String (.file) - Resources (.resources) Slice (.local) + Boolean (.preload) + Resources (.resources) String (.family)? String (.weight)? String (.style)? @@ -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...) */}} @@ -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 }} \ No newline at end of file