Skip to main content
Deploying Hugo on Cloudflare Pages
  1. Posts/

Deploying Hugo on Cloudflare Pages

·514 words·3 mins·
Artur Tyloch
Author
Artur Tyloch
AI | Startup | SaaS
The Modern Web - This article is part of a series.
Part : This Article

Hugo on Cloudflare Pages
#

Cloudflare Pages is an excellent platform for hosting static websites, making it a perfect fit for Hugo projects.

Why Cloudflare Pages?
#

Advantages
#

  • Free hosting for personal projects
  • Global CDN for fast loading worldwide
  • Automatic SSL for out-of-the-box security
  • Git integration for automatic deployments from GitHub/GitLab
  • Custom domains support
  • Edge functions for serverless computing at the edge

Prerequisites
#

Before you begin, make sure you have:

  • A Hugo project ready for deployment
  • A Cloudflare account (the free plan is sufficient)
  • A Git repository (GitHub, GitLab, or Bitbucket)

Step-by-Step: Deployment
#

1. Prepare your Hugo project
#

# Check if the project builds locally
hugo server

# Create a production version
hugo --minify

2. Push to Git
#

# Initialize the repository (if it doesn't exist yet)
git init
git add .
git commit -m "Initial Hugo site"

# Connect to GitHub
git remote add origin https://github.com/username/your-hugo-site.git
git push -u origin main

3. Configure Cloudflare Pages
#

  1. Log in to the Cloudflare dashboard.
  2. Navigate to the “Pages” section.
  3. Click “Create a project”.
  4. Select “Connect to Git”.
  5. Authorize access to your repository.
  6. Choose the repository with your Hugo project.

4. Build settings
#

Build command: hugo --minify
Build output directory: public
Root directory: /

5. Environment variables
#

Add environment variables in the project settings:

HUGO_VERSION=0.120.0
NODE_VERSION=18

Advanced Configuration
#

Custom domain
#

  1. Go to “Custom domains”.
  2. Click “Set up a custom domain”.
  3. Enter your domain name.
  4. Configure the DNS records according to the instructions.

Automatic deployments
#

Cloudflare Pages automatically builds and deploys your site with every push to the main branch:

# Every change automatically triggers a new deployment
git add .
git commit -m "Update content"
git push origin main

Preview deployments
#

Branches other than the main one create preview deployments:

# Create a preview branch
git checkout -b feature/new-post
git add .
git commit -m "Add new blog post"
git push origin feature/new-post

Troubleshooting
#

Common issues
#

Problem: Build fails with a Hugo version error. Solution: Set the HUGO_VERSION environment variable to the correct version.

Problem: 404 error after deployment. Solution: Check if the baseURL in your config.toml is correct.

Problem: CSS/JS not loading. Solution: Ensure you are using relative paths or the correct baseURL.

Build logs
#

Check the logs in the “Deployments” section for detailed error information.

Best Practices
#

  1. Use Hugo modules for better dependency management.
  2. Minify assets for better performance.
  3. Test locally before pushing to production.
  4. Use preview deployments to review changes.
  5. Configure a 404 page for a better user experience.

Monitoring and Analytics
#

Cloudflare offers built-in monitoring tools:

  • Analytics for visitor statistics.
  • Speed insights for performance metrics.
  • Security events for security monitoring.

Summary
#

Cloudflare Pages is an ideal platform for Hugo projects—it’s free, fast and easy to configure. The integration with Git makes the deployment process fully automated.

Benefits
#

  • Free hosting with a global CDN
  • Automatic SSL and security
  • Zero-config deployment for Hugo
  • Preview environments for every branch
  • Custom domains and DNS management

Cloudflare Pages is an a great choice for both personal and commercial projects!

The Modern Web - This article is part of a series.
Part : This Article

Found this helpful? Share it with others!