In that JS file is a hard-coded array of images that are currently located at /static/image/shuffle.
Easy-enough and works, but it is fragile in that if I add more images to /static/image/shuffle, I have to also edit the array in the javascript file to match it.
Obviously, what I’d really like is someway to rebuild the javascript file based on the changes in the directory, recreating the simple array of paths to these images something like:
let images = [];
{{ range resources.Match "images/gallery/*" }}
images.push("{{ .RelPermalink }}");
{{ end }}
My hang up is I am not sure how to tell Hugo to parse over a Javascript file (where should it go? is there something I have to invoke with pipes?). I am thinking it is probably pretty simple but I am new to some of this and the section on “Javascript Building” has examples but doesn’t really explain them or what they are examples of to a novice.
I’m not using the javascript file yet, as I am trying to figure out how to use it using the shuffled partial (/themes/nta/layouts/partials/shuffled.html) - as this is simpler to see if the resource fetch is working.
It is not.
The images are stored in /assets/shuffle.
The partial is asking for them:
{{ range resources.Match "/shuffle/*" }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ end }}
And coming up empty.
I have included the shuffled.html partial in 404.html, so have a look at:
OK, we are 99% of the way there. It is almost working 100% except for one little thing.
I have updated the config to explicitly look for /assets, as recommended, and moved the images up under /assets. This is working.
The javascript file has the following code:
let deck = [];
{{ range resources.Match "images/shuffle/{*.jpg,*.png,*.webp}" }}
deck.push("{{ .RelPermalink }}");
{{ end }}
And this is working. The resulting code produced is correct, for example:
let deck = [];
deck.push("/images/shuffle/Bokken%201x1%20500w.jpg");
deck.push("/images/shuffle/kata-bijak-boruto.jpg.webp");
So far so good.
The one thing that is not working is the site, when running “hugo -D --disableFastRender” locally does not refresh as images are added to the asset directory. It detects that they are added, but does not appear to update the javascript (shuffle.js). This can be tested by beginning the test with only 1 image in the directory and adding another. At that point, the page does refresh, but only the original image is ever displayed and never the new image, implying the array (deck[]) was not actually updated.
This is a not a huge problem because a quick inspection of the production /public (eg, execute “hugo”) shows that the array is properly populated and thus the release site will work as expected.
Could we please confirm that the following code for parsing and generating the javascript file is correct:
As I was guessing as to the proper calling parameters for this and perhaps this is where the problem lies (because I don’t have a similar refresh problem with scss files).
If you search this forum and github.com/gohugoio/hugo/issues you will find similar cases where hugo server doesn’t behave as expected. It’s not a single issue; there are variants.
Some thoughts:
Run hugo server with the --noHTTPCache flag
Disable cache in your brower’s dev tool preferences. For example, with Chrome:
@jmooring I am happy with the progress we made on this little problem today, and I learned more than I knew when I woke up today. Thank you for the help.