On terminal → hugo server. (can see the website on localhost)
Run the python script to make some changes to the public directory that Hugo saved on the disk. (the files are updated when I check the HTML)
Unable to see the script modifications in the website using localhost.
How do I force using the modified public files from the disk? I doubt if this is because the localhost:1313 is picking from the memory and not the copy saved on the disk.
If by this, you mean while running hugo server again, see from docs:
‘hugo server’ will avoid writing the rendered and served content to disk, preferring to store it in memory.
It is not using the files in public. You can see this behaviour clearly by making changes to your site (layout/content/etc) which, if running hugo server, will update localhost:1313 immediately, while the public folder will be untouched.
hugo server builds your project in memory and serves to localhost:1313 from memory. It won’t write to disk (unless --renderToDisk is set, but I’m not sure if this would work for your use case either).
Your script works on the files in public and is like a “post-processing” step.
So what you want is a build process that does the following:
Runs hugo build (to write everything to public/)
Runs your postprocessing script.
This means you’ll lose the hot reloading functionality that hugo serve gives you, but maybe that’s an acceptable tradeoff for you
Then, you need to run a separate web server to serve the files from public/ directly. It’s hard to give advice on how to do this because it depends on your machine, but here’s a list of options.
The key insight here is that if you need to do any postprocessing on the output of Hugo, you can’t rely on hugo server to serve the files anymore, and need to use another solution for serving.
The key insight here is that if you need to do any postprocessing on the output of Hugo, you can’t rely on hugo server to serve the files anymore, and need to use another solution for serving.
I have been wanting to get the hot reloading functionality + postprocessing and trying to see if that were possible. Thanks for the insight, I will try to rationalize between the two options available.
I’d normally agree with @nternetinspired on this, but it really depends on the script you’re running. For example, my blog uses a script to clean things up after build, and I don’t run or think about that when doing normal development. Hugo Encryptor is different in that it modifies the site in a way that makes it hard to iterate upon (here’s the shortcode it uses, note the display: none).
Re: the tradeoff between hot-reloading and postprocessing, it’s also possible to set up a watcher system using something like Watchman – so you wouldn’t get auto-reloading, but it’s at least possible to have the system rebuild itself automatically on file change, so all you would need to do is switch over to your browser and hit reload. Again, it’s a tradeoff between convenience and complexity