Hosting a Hugo site on a Cloudflare Worker

Following a discussion on hosting Hugo on a Cloudflare worker, @jmooring came up with an excellent solution.

Here’s an example project hosted on a Cloudflare Worker:

This is the build script:

And the wrangler.toml file

Those are the only two files you need to copy to your project and configure to your liking. For example–

  • change Dart SASS version or if you don’t transpile SASS to CSS, you can remove Dart SASS code from the build.sh file.
  • change the time zone.
  • change Hugo version
  • change Node JS version

I was able to get the deployment working without a custom build script by using the following simplified wrangler.jsonc configuration:

{
  "name": "example-com",
  "compatibility_date": "2025-07-20",
  "assets": {
    "directory": "./public",
    "html_handling": "auto-trailing-slash",
    "not_found_handling": "404-page",
    "run_worker_first": false
  },
  "build": {
    "command": "hugo build --gc --minify",
  },
  "workers_dev": true,
  "preview_urls": true
}

Not sure it will work for every environment as I have mine themes defined in go modules

This example is shared by a Hugo support/maintainer person. So, for a user, it is the way I would recommend to go with.

The build script:

  1. Installs a specific version of Hugo
  2. Installs a specific version of Dart Sass to transpile Sass to CSS
  3. Sets the system timezone
  4. Ensures that Page.GitInfo returns the expected results
  5. Addresses https://github.com/gohugoio/hugo/issues/9810

In my view all but #2 are required for every site.

1 Like

@jmooring Hi Jo I had a go with your build.sh script but fails.
I get this in the cloudflare worker build logs:

stderr: ‘/bin/sh: 1: ./build.sh: Permission denied’,

Have I missed something real simple?

Did you copy the code directly from Joe’s file or the file itself? See Hugo support in Cloudflare Workers - #6 by tyco and Hugo support in Cloudflare Workers - #7 by jmooring.

I copied the code from hosting-cloudflare-worker/build.sh at main · jmooring/hosting-cloudflare-worker · GitHub raw and pasted it into a file build.sh in my project.

Doh!
I I did a
git add --chmod=+x build.sh
instead of
chmod +x build.sh

All good now

In our Host on Cloudflare documentation I’ve change the build command from this:

[build]
command = "./build.sh'"

To this:

[build]
command = "chmod a+x build.sh && ./build.sh"

That makes the permissions of the file in the repository irrelevant.

2 Likes

I tried the worker, but I have to remember every time to rebase my project before pushing changes (mostly markdown content).