From 0235da7caa8734ca507a58717e5487e5cc689d3c Mon Sep 17 00:00:00 2001 From: somrat sorkar Date: Sat, 14 Oct 2023 10:16:12 +0600 Subject: [PATCH] added lazy loading for css and js --- exampleSite/hugo.toml | 14 ++++++++++++ layouts/partials/essentials/script.html | 22 ++++++++++++++++--- layouts/partials/essentials/style.html | 29 +++++++++++++++++++++---- 3 files changed, 58 insertions(+), 7 deletions(-) diff --git a/exampleSite/hugo.toml b/exampleSite/hugo.toml index 973f054..599696b 100755 --- a/exampleSite/hugo.toml +++ b/exampleSite/hugo.toml @@ -122,31 +122,45 @@ notAlternative = true # CSS Plugins [[params.plugins.css]] link = "plugins/swiper/swiper-bundle.css" +lazy = false [[params.plugins.css]] link = "plugins/glightbox/glightbox.css" +lazy = true [[params.plugins.css]] link = "plugins/font-awesome/v6/brands.css" +lazy = true [[params.plugins.css]] link = "plugins/font-awesome/v6/solid.css" +lazy = true [[params.plugins.css]] link = "plugins/font-awesome/v6/icons.css" +lazy = true # JS Plugins [[params.plugins.js]] link = "js/search.js" +lazy = false [[params.plugins.js]] link = "plugins/swiper/swiper-bundle.js" +lazy = false [[params.plugins.js]] link = "plugins/glightbox/glightbox.js" +lazy = true [[params.plugins.js]] link = "js/gallery-slider.js" +lazy = true [[params.plugins.js]] link = "js/accordion.js" +lazy = true [[params.plugins.js]] link = "js/tab.js" +lazy = true [[params.plugins.js]] link = "js/modal.js" +lazy = true [[params.plugins.js]] link = "plugins/cookie.js" +lazy = true [[params.plugins.js]] link = "plugins/youtube-lite.js" +lazy = true diff --git a/layouts/partials/essentials/script.html b/layouts/partials/essentials/script.html index fbca5a6..b6b4d9f 100755 --- a/layouts/partials/essentials/script.html +++ b/layouts/partials/essentials/script.html @@ -1,5 +1,6 @@ {{ $scripts := slice }} +{{ $scriptsLazy := slice }} {{ range site.Params.plugins.js }} {{ if findRE "^http" .link }} {{ else }} - {{ $scripts = $scripts | append (resources.Get .link) }} + {{ if not .lazy }} + {{ $scripts = $scripts | append (resources.Get .link) }} + {{ else }} + {{ $scriptsLazy = $scriptsLazy | append (resources.Get .link) }} + {{ end }} {{ end }} {{ end }} {{ $scripts = $scripts | append (resources.Get "js/main.js") }} -{{ $scripts = $scripts | resources.Concat "js/script.js" }} +{{ $scripts = $scripts | resources.Concat "js/scripts.js" }} + +{{ $scriptsLazy = $scriptsLazy | resources.Concat "js/scripts-lazy.js" }} + {{ if hugo.IsProduction }} {{ $scripts = $scripts | minify | fingerprint }} + {{ $scriptsLazy = $scriptsLazy | minify | fingerprint }} {{ end }} - +{{/* scripts */}} +{{/* scripts lazy */}} + + {{ partialCached "pwa.html" . }} diff --git a/layouts/partials/essentials/style.html b/layouts/partials/essentials/style.html index 8ad122e..744d35e 100755 --- a/layouts/partials/essentials/style.html +++ b/layouts/partials/essentials/style.html @@ -31,6 +31,7 @@ {{ $styles := slice }} +{{ $stylesLazy := slice }} {{ range site.Params.plugins.css }} {{ if findRE "^http" .link }} {{ else }} - {{ $styles = $styles | append (resources.Get .link) }} + {{ if not .lazy }} + {{ $styles = $styles | append (resources.Get .link) }} + {{ else }} + {{ $stylesLazy = $stylesLazy | append (resources.Get .link) }} + {{ end }} {{ end }} {{ end }} -{{ $styles := $styles | append (resources.Get "scss/main.scss" | toCSS) }} -{{ $styles := $styles | resources.Concat "css/style.css" }} + +{{/* main style */}} +{{ $styles = $styles | append (resources.Get "scss/main.scss" | toCSS) }} +{{ $styles = $styles | resources.Concat "css/style.css" }} {{ $styles = $styles | resources.PostCSS }} + +{{ $stylesLazy = $stylesLazy | resources.Concat "css/style-lazy.css" }} +{{ $stylesLazy = $stylesLazy | resources.PostCSS }} + {{ if hugo.IsProduction }} {{ $styles = $styles | resources.ExecuteAsTemplate "css/style.css" . | minify | fingerprint | resources.PostProcess }} + {{ $stylesLazy = $stylesLazy | resources.ExecuteAsTemplate "css/style-lazy.css" . | minify | fingerprint | resources.PostProcess }} {{ else }} {{ $styles = $styles | resources.ExecuteAsTemplate "css/style.css" . }} + {{ $stylesLazy = $stylesLazy | resources.ExecuteAsTemplate "css/style-lazy.css" . }} {{ end }} - +{{/* styles */}} + +{{/* styles lazy */}} +