Run python script after hugo build

I have a Hugo website for myself which works perfectly fine.
After the Hugo build I run a script to encrypt sections of the public directory using GitHub - Li4n0/hugo_encryptor: A tools for encrypting hugo posts

What I am doing:

  1. On terminal → hugo server. (can see the website on localhost)
  2. 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)
  3. 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.

Not enough information :slight_smile:

It might be, that your hugo run uploads the files already, where the encryption script does encrypt only the local files. How are your files deployed?

Trying to articulate a little better by describing the steps I follow:

  1. On terminal → “hugo server”. (can see the website on localhost)
  2. 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)
  3. Unable see the script modifications in the website using localhost.

I will use Netlify to eventually deploy it, but at the moment I am trying to get it to work locally.

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.

I do not run the hugo server again.

Is it possible to run a script on the files ‘stored it in memory’? Or otherwise force localhost:1313 to pick from the public folder.

How are you serving your files on your localhost?

Edit:

I think you would have a better chance at getting help by asking the author of the encryptor tool directly.

Have you tried executing hugo --cleanDestinationDir before hugo server ?

How are you serving your files on your localhost ?

I don’t understand this question but I am going to say hugo server handles it.

My understanding of the situation:

  • 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 :slight_smile:

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.

2 Likes

I think you summarised my dilemma perfectly.

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. :slight_smile:

Not sure why you’d want this when running a local server, given it’s a part of your build process, not your development process.

Deploy it, let your script run after hugo build. Move on with life.

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 :slight_smile:

1 Like

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