Why can't I add a JS file executed as a template to a JS bundle?

I’ve got a JS file called random.js which is supposed to open a random page of my website when the user clicks a Random article button. Obviously, the JS file needs to know about the pages that exist, so I made Hugo print the URLs into a JS array, using the resources.ExecuteAsTemplate function.

This works fine when adding the rendered JS file as a stand-alone script to the HTML.

However, when trying to bundle the random.js file with other JS files into bundle.js, it isn’t included.

This works fine (but it’s a separate file):

{{ $jquery := resources.Get "js/jquery.js" }}
{{ $menuJS := resources.Get "js/menu.js" }}
{{ $randomJS := resources.Get "js/random.js" | resources.ExecuteAsTemplate "js/random-ready.js" . }}

{{ $minifiedJS := slice $jquery $menuJS | resources.Concat "js/bundle.js" | resources.Minify }}

<script src="{{ $minifiedJS.Permalink }}" defer></script>
<script src="{{ $randomJS.Permalink }}" defer></script>

Here, random-ready.js isn’t included in bundle.js:

{{ $jquery := resources.Get "js/jquery.js" }}
{{ $menuJS := resources.Get "js/menu.js" }}
{{ $randomJS := resources.Get "js/random.js" | resources.ExecuteAsTemplate "js/random-ready.js" . }}

{{ $minifiedJS := slice $jquery $menuJS $randomJS | resources.Concat "js/bundle.js" | resources.Minify }}

<script src="{{ $minifiedJS.Permalink }}" defer></script>

Why doesn’t this work? How to do it correctly?

Thanks for your help!

Which version of Hugo are you using?
Does this happen when running hugo?
Does this happen when running hugo server?
Is this an intermittent problem?

Which version of Hugo are you using?

Thanks for the hint, that’s it. I haven’t checked my GitHub notifications for a while; after installing v0.104.3, the issue was gone. :+1:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.