From 361707f4fdcad5a2febc3f4a72b489ede49d2ed8 Mon Sep 17 00:00:00 2001 From: George Cushen Date: Wed, 18 Dec 2019 14:33:15 +0000 Subject: [PATCH] feat: lazy load figures and gallery images The proportion of image data in the typical payload of a website can be significant. Lazy loading is a solution that lowers initial page payload and load time, without removing content. Non-critical resources are instead loaded at the moment that they are needed. Also, load MathJax async in head if required, rather than at end of body. Close #1106 --- data/assets.toml | 6 ++++++ layouts/partials/site_head.html | 20 ++++++++++++++++++++ layouts/partials/site_js.html | 10 ---------- layouts/shortcodes/figure.html | 2 +- layouts/shortcodes/gallery.html | 4 ++-- 5 files changed, 29 insertions(+), 13 deletions(-) diff --git a/data/assets.toml b/data/assets.toml index 7b2f24e0..5a2bd9de 100644 --- a/data/assets.toml +++ b/data/assets.toml @@ -19,6 +19,7 @@ version = "3" sri = "" # No SRI as dynamically generated. url = "https://cdn.jsdelivr.net/npm/mathjax@%s/es5/tex-chtml.js" + async = true [js.isotope] version = "3.0.6" sri = "sha256-CBrpuqrMhXwcLLUd5tvQ4euBHCdh7wGlDfNz8vbu/iI=" @@ -59,6 +60,11 @@ version = "8.4.3" sri = "" # SRI unavailable. url = "https://cdnjs.cloudflare.com/ajax/libs/mermaid/%s/mermaid.min.js" +[js.lazysizes] + version = "5.1.2" + sri = "sha256-Md1qLToewPeKjfAHU1zyPwOutccPAm5tahnaw7Osw0A=" + url = "https://cdnjs.cloudflare.com/ajax/libs/lazysizes/%s/lazysizes.min.js" + async = true # CSS diff --git a/layouts/partials/site_head.html b/layouts/partials/site_head.html index ec777cfd..349ddeb3 100644 --- a/layouts/partials/site_head.html +++ b/layouts/partials/site_head.html @@ -47,6 +47,12 @@ {{ end }} + {{/* Config LaTeX math rendering. */}} + {{ if or .Params.math site.Params.math }} + {{ $mathjax_config := resources.Get "js/mathjax-config.js" }} + + {{ end }} + {{/* Attempt to load local vendor CSS, otherwise load from CDN. */}} {{ $scr.Set "vendor_css_filename" "main.min.css" }} {{ $scr.Set "vendor_js_filename" "main.min.js" }} @@ -92,6 +98,20 @@ {{ printf "" (printf $css.instantsearchTheme.url $css.instantsearchTheme.version) $css.instantsearchTheme.sri | safeHTML }} {{ end }} + {{/* Load async scripts. */}} + {{ range $k, $v := site.Data.assets.js }}{{/* TODO: investigate why `where ... "async" true` does not work. */}} + {{ $load := $v.async }} + + {{/* Only load MathJax if required. */}} + {{ if (eq $k "mathJax") | and (not (or $.Params.math site.Params.math)) }} + {{ $load = false }} + {{end}} + + {{ if $load }} + {{ printf "" (printf $v.url $v.version) $v.sri | safeHTML }} + {{end}} + {{end}} + {{ end }} {{/* We cannot use SRI with Google Fonts because the CSS is dynamically generated according to the user agent. */}} diff --git a/layouts/partials/site_js.html b/layouts/partials/site_js.html index c5ba3165..d81da467 100644 --- a/layouts/partials/site_js.html +++ b/layouts/partials/site_js.html @@ -1,11 +1,5 @@ {{ $scr := $.Scratch }} - {{/* Config LaTeX math rendering. */}} - {{ if or .Params.math site.Params.math }} - {{ $mathjax_config := resources.Get "js/mathjax-config.js" }} - - {{ end }} - {{/* Attempt to load local vendor JS, otherwise load from CDN. */}} {{ $js := site.Data.assets.js }} {{ if not ($scr.Get "use_cdn") }} @@ -28,10 +22,6 @@ {{ end }} {{ end }} - {{/* LaTeX math rendering. */}} - {{ if or .Params.math site.Params.math }} - {{ printf "" (printf $js.mathJax.url $js.mathJax.version) $js.mathJax.sri | safeHTML }} - {{ end }} {{ end }} {{/* Maps JS. */}} diff --git a/layouts/shortcodes/figure.html b/layouts/shortcodes/figure.html index 3d406b62..eff1c44e 100644 --- a/layouts/shortcodes/figure.html +++ b/layouts/shortcodes/figure.html @@ -21,7 +21,7 @@ {{ end -}} -{{ with .Get +{{ with .Get {{- if or $lightbox (.Get "link") }}{{ end }} diff --git a/layouts/shortcodes/gallery.html b/layouts/shortcodes/gallery.html index 04d9942d..dc8bc55c 100644 --- a/layouts/shortcodes/gallery.html +++ b/layouts/shortcodes/gallery.html @@ -29,7 +29,7 @@ {{ end }} {{ end }} - + {{end}} @@ -46,7 +46,7 @@ {{ end }} {{ end }} - + {{end}} {{else}}