added lazy loading for css and js

This commit is contained in:
somrat sorkar
2023-10-14 10:16:12 +06:00
parent 0c9aef3e0f
commit 0235da7caa
3 changed files with 58 additions and 7 deletions

View File

@@ -1,5 +1,6 @@
<!-- JS Plugins + Main script -->
{{ $scripts := slice }}
{{ $scriptsLazy := slice }}
{{ range site.Params.plugins.js }}
{{ if findRE "^http" .link }}
<script
@@ -7,24 +8,39 @@
type="application/javascript"
{{ .attributes | safeHTMLAttr }}></script>
{{ 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 }}
<!-- main script -->
{{ $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 */}}
<script
crossorigin="anonymous"
integrity="{{ $scripts.Data.Integrity }}"
src="{{ $scripts.RelPermalink }}"></script>
{{/* scripts lazy */}}
<script
crossorigin="anonymous"
integrity="{{ $scriptsLazy.Data.Integrity }}"
src="{{ $scriptsLazy.RelPermalink }}"
defer></script>
<!-- progressive web app -->
{{ partialCached "pwa.html" . }}