What is the best fix bundling js on page level?


{{- $fileName := .Slug | default .Title | urlize -}}
{{- if or (.IsHome) (eq .Layout "tool") -}}
    {{- $fileName = .Site.LanguageCode -}}
{{- end -}}

{{- $jsFiles := slice "main.js" "trending-api.js" "sw.js" -}}

{{ if .IsHome }}
    {{- $jsFiles = $jsFiles | append "api.js" "download.js" "zip.js" -}}
{{ end }}

{{- $jsLoc := slice -}}

{{ range $jsFiles }}
    {{ $jsLoc = $jsLoc | append (resources.Get (printf "/assets/js/%s" .)) }}
{{ end }}

{{- $mdName :=  $fileName -}}
{{- $jsName := printf "assets/js/%s.js" $mdName -}}

{{- $concatBundle := $jsLoc | resources.Concat "assets/js/bundle.js" -}}
{{- $processedBundle := $concatBundle | resources.ExecuteAsTemplate $jsName . -}}
{{- $finalBundle := $processedBundle -}}

<script src="{{ $finalBundle.RelPermalink }}" defer></script>

Please tell me the best solution to this. It’s not appending the js on homepage.