HUGO

Hugo server returns 404 in files added in the destination folder after the build

Hi,

I’m running gulp workbox-build alongside Hugo. I use it to generate a service worker.

I basically follow this tutorial (https://andeers.com/2019/07/service-worker-refresh/).

What it does:

  • Cleans the public/ directory.
  • Rebuilds the destination directory upon running hugo.
  • Runs grunt workbox-build on the public/ folder (to create a “precache manifest” of all the files for the script it’ll render).
  • Renders resulting sw.js file on the same public/ folder. Just at the root (i.e. ./sw.js).

Running it on Github Pages causes no problems. All files are detected. No 404s.

But when I run it with hugo serve, sw.js isn’t detected and returns a 404 in the console.

I also find that this is true for all files manually added to the public/ folder after running hugo. (Example: I manually add new-file.txt to the public/ folder and reference it in a template partial. This forces a live reload. new-file.txt is still 404 on the localhost.)

This also comes after I check the path references multiple times and verify that they are correct.

Is there any workaround here so that any file in the manually added to the public/ folder after hugo will not return a 404 in the localhost?

Thanks!

The development server isn’t aware of the public directory when run normally. From https://gohugo.io/commands/hugo_server/ :

‘hugo server’ will avoid writing the rendered and served content to disk, preferring to store it in memory.

One thing that might work is using --renderToDisk; I presume it reads from disk, so might work as you expect.

1 Like

It worked! Gold!

Thanks a bunch! :+1:

1 Like

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