Deploying Docusaurus to Cloudflare Pages
Cloudflare Pages is one of the fastest and easiest ways to host a static website like Docusaurus. It's completely free for most personal and small projects, offers lightning-fast global CDN delivery, and provides automatic continuous deployment from GitHub.
In this tutorial, we will deploy this exact Docusaurus project to Cloudflare Pages!
Step 1: Push your code to GitHub
Before Cloudflare can deploy your site, it needs to read your code from a Git repository.
-
Initialize Git (If you haven't already): Open your terminal in your project directory and run:
git initgit add .git commit -m "Initial commit of Docusaurus project" -
Create a GitHub Repository: Go to GitHub, create a new repository (public or private), and do not initialize it with a README.
-
Push your code: Link your local project to GitHub and push your code:
git branch -M maingit remote add origin https://github.com/YOUR_USERNAME/YOUR_REPO_NAME.gitgit push -u origin main
Step 2: Set up Cloudflare Pages
-
Log into Cloudflare: Head over to the Cloudflare Dashboard and create a free account if you don't have one.
-
Navigate to Pages: In the left sidebar, click on Workers & Pages. Then, click the blue Create application button, and select the Pages tab.
-
Connect GitHub: Click on Connect to Git. Cloudflare will ask for permission to access your GitHub repositories. Choose either "All repositories" or specifically select the Docusaurus repository you just created.
-
Select your Repository: Once authorized, you will see a list of your repositories. Select your Docusaurus project and click Begin setup.
Step 3: Configure Build Settings
This is the most important step. Cloudflare needs to know how to build your Docusaurus site and where to find the finished HTML files.
In the Set up builds and deployments screen, fill out the following settings:
- Project name: (You can leave this as the default or name it anything you like. This will become your
.pages.devsubdomain). - Production branch:
main(ormasterdepending on what you pushed). - Framework preset: Select
None(orCreate React Appdoesn't matter, we will configure it manually). - Build command:
npm run build - Build output directory:
build
[!IMPORTANT] Docusaurus automatically places all compiled static HTML, CSS, and JS files into a folder named
build. If you get the output directory wrong, Cloudflare will deploy an empty site or fail the build!
Environment Variables (Important for Node versions)
Docusaurus v3 requires Node.js 18.0 or newer. To ensure Cloudflare Pages uses a modern version of Node.js:
- Under Environment variables (advanced), click Add variable.
- Variable name:
NODE_VERSION - Value:
20(or18.17.0)
Step 4: Save and Deploy!
Click the Save and Deploy button!
Cloudflare will now spin up a build environment, pull your code from GitHub, run npm run build, and deploy the contents of the build folder to their global network.
What happens next?
- Continuous Deployment: Every time you push new markdown files or code changes to the
mainbranch on GitHub, Cloudflare will automatically trigger a new build and update your live website within minutes. - Custom Domains: Once your site is live on the
.pages.devsubdomain, you can easily attach your own custom domain (e.g.,hashcode-learn.com) from the Cloudflare Pages settings dashboard.
Congratulations! Your site is now live on the edge!