How to host static site with Cloudflare Workers

Cloudflare Workers provides a serverless execution environment that allows you to create entirely new applications or augment existing ones without configuring or maintaining infrastructure.

For this guide, we will use static files generated by hugo. Hugo is a static site generator written in golang that allows you to create content using markdown.

# Workers CLI

Workers Sites require Wrangler to be installed locall, make sure to be on the latest version.

To install wrangler Ensure you have npm installed then use this command:

npm install -g @cloudflare/wrangler

# Generate static site

Generate static files for a hugo site:

hugo -D

Login to cloudflare:

wrangler login

This command creates a few things: wrangler.toml and a workers-site directory

wrangler init --site citizix

Add your site’s build directory to the wrangler.toml:

bucket = "./public" # <-- Add your build directory name here!
entry-point = "workers-site"

Add your account_id to your wrangler.toml. You can find your account_id on the right sidebar of the Workers or Overview Dashboard. Note: You may need to scroll down!

You can preview your site by running:

wrangler preview --watch

Decide where you’d like to publish your site to:
a. subdomain
b. personal domain registered with Cloudflare

Then, update your wrangler.toml:

Personal Domain: Add your zone_id and a route.

zone_id = "42ef.."
route = "*" Set workers_dev to true. This is the default.

comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy