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#
- Log in to the Cloudflare dashboard.
- Navigate to the “Pages” section.
- Click “Create a project”.
- Select “Connect to Git”.
- Authorize access to your repository.
- 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#
- Go to “Custom domains”.
- Click “Set up a custom domain”.
- Enter your domain name.
- 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#
- Use Hugo modules for better dependency management.
- Minify assets for better performance.
- Test locally before pushing to production.
- Use preview deployments to review changes.
- 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!

