Hugo not serving one particular static file

I am having a weird situation where in hugo is not serving one of the static files (a javascript file).

  • It is serving all other files including the ones in nested folders. But not this file.
  • If I run another web server like python http server over the public folder, the said file is picked up. So the file definitely exists in the public folder path.
  • Have tried deleting public folder and running hugo. But no luck.

Anybody seen something like this?

How did you discover this?

Because my page breaks. And I see in the network tab in the browser that the file records a 404 not found. Taking the URL from the network tab for the file and hitting it in the browser also gives the same 404.

What’s your baseURL?
What’s the bad link you see in the console?

If the (somewhat broken) site is live, please share the URL.

The base url in config.toml is baseUrl = "http://localhost:51234", and I see the same base url in the browser network tab too.

Say the breaking URL is http://localhost:51234/abc/abc.js, where abc.js exists in the path, and another file in the same path loads ok.

Site is not yet live. But you can clone it from here to test on local machine. Search for the term first or third in the search box. You will see that it does not work because of the js not loading.

I tried on another machine just to make sure that it is not something sticky in this machine. But same issue. :frowning:

I requested access to your repo last night.

I thought the repo was public :thinking: Let me check

Granted.

But I don’t why it was requesting access. I checked and the repo visibility is public.

OK, now I understand the problem. This only happens when running hugo server. And it happens because the create_search_index.py script writes to public directory. By default, when you run hugo server, everything is rendered to memory. It has no idea what’s in the public directory.

When you’re working with this locally, create the search index in the project’s static directory:

static/tipuesearch/tipuesearch_content.js

Maybe make a copy of the script and change the destination.

There’s a stray file, in the static directory, named search_content.js instead of tipuesearch_content.js. That might have added to the confusion.

1 Like

Wow! wonderful catch @jmooring :saluting_face:

Did not occur to me that the entire thing is rendered from memory.

In fact I did not mention in the original post that I had tried it from static/ folder which worked but I did not go that route because:

  • I was just making use of an existing theme (just tinkering a bit for my needs). The author had written the index generation script to write to the public folder, which I did not want to change. Not sure if they knew about this issue. Or how did they handle the issue I have been facing.
  • Things like this index or css out of scss is generated, we may not have to commit the index itself into the repo. I wasn’t sure if it was acceptable in practice to have generated items in the static folder.

Is there a way to make Hugo serve resources from the files and folders on disk ? Either exclusively from disk or when not available in memory?

At least for now I am going to update the index generator script to write to the static folder instead of public folder.

BTW I had created search_content.js for testing.

Thanks again.:pray:

This seems to work.

hugo
./create_search_index.py 
hugo server --renderToDisk 
1 Like

Out of curiosity … Say these are the commands I use:

hugo
./create_search_index.py
hugo server

I am running hugo server after creating the index in the public folder from where hugo should be able to read and load into memory for serving the files. Right?

If not, what I am missing?

From the root of your project directory:

hugo
./create_search_index.py
hugo server --renderToDisk 

If the --renderToDisk flag isn’t behaving, then you must create the search index in the static folder as previously discussed.

I have tested this multiple times after cloning your repo. It works perfectly.

No no, let me clarify.

renderToDisk works fine. What I was trying to understand is when I run hugo server after creating the index (as seen in the commands above), hugo should be able to load the index js too along with other files in the public folder. But it does not. Was trying to understand the reason. May be when we run just hugo, that decides what files or folders will be considered to load into memory for serving (unless we use renderToDisk)

Works for me.

No. When you run “just hugo” it publishes your site, writing to disk.

Are you saying that the following works for you. By that I mean the index JS is loaded?

hugo 
./create_search_index.py
hugo server    # note: not using renderToDisk

That was my question and wanted to correct my understanding which is … The first hugo command publishes the site to disk. The last hugo server command should take the published site under the public folder, loads into memory and serves on request. It does so for rest of the files except the index JS, which is written to the public folder before starting Hugo (last command above). Were you saying it is also working for you without renderToDisk option?

I am following your earlier suggestion to use renderToDisk which works fine and I am currently moving ahead with that. At least for now before I decide if I should write the index JS to the static directory.

hugo 
./create_search_index.py
hugo server —renderToDisk    # 👍

Thanks for your patience and help :pray:

No, it should not. I don’t know how to make this any clearer.

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