📖 Scenario: You have created a simple Astro website and want to share it with the world by deploying it to Cloudflare Pages, a platform that hosts websites with fast global delivery.
🎯 Goal: Build a basic Astro project and deploy it to Cloudflare Pages by setting up the project, configuring the deployment, building the site, and connecting it to Cloudflare Pages.
📋 What You'll Learn
Create a new Astro project with a homepage
Add a build script to package.json
Configure the astro.config.mjs file for Cloudflare Pages
Deploy the site using Cloudflare Pages settings
💡 Why This Matters
🌍 Real World
Deploying static or server-rendered websites quickly and globally using Cloudflare Pages is common for personal blogs, portfolios, and small business sites.
💼 Career
Knowing how to deploy modern web frameworks like Astro to popular platforms like Cloudflare Pages is valuable for frontend developers and web engineers working on fast, scalable web projects.
Progress0 / 4 steps
1
Create a basic Astro project with a homepage
Create a new Astro project folder called my-astro-site. Inside it, create a file src/pages/index.astro with the exact content: <h1>Welcome to Astro on Cloudflare Pages</h1>.
Astro
Hint
Make sure the file is named exactly index.astro inside src/pages and contains the heading text.
2
Add a build script to package.json
In the root folder my-astro-site, create a package.json file. Add a scripts section with a build script that runs astro build.
Astro
Hint
The package.json file must have a scripts object with a build key set to astro build.
3
Configure astro.config.mjs for Cloudflare Pages
Create a file astro.config.mjs in the root folder. Export a default config object that sets output to 'server' and adapter to @astrojs/cloudflare. Import the adapter from @astrojs/cloudflare at the top.
Astro
Hint
Use defineConfig from astro/config and set output and adapter correctly.
4
Deploy the site using Cloudflare Pages settings
In your Cloudflare Pages dashboard, create a new project linked to your my-astro-site repository. Set the build command to npm run build and the build output directory to dist. Confirm and deploy your site.
Astro
Hint
Use the exact build command npm run build and output directory dist in Cloudflare Pages settings.
Practice
(1/5)
1. What is the first step to deploy an Astro site to Cloudflare Pages?
easy
A. Push your Astro project code to a GitHub repository
B. Write a custom server script for Cloudflare
C. Install Cloudflare Pages CLI locally
D. Create a Docker container for the site
Solution
Step 1: Prepare your Astro project
You need to have your Astro site code ready and pushed to a GitHub repository because Cloudflare Pages connects to GitHub to get your code.
Step 2: Connect GitHub repo to Cloudflare Pages
Cloudflare Pages uses your GitHub repository to build and deploy your site automatically.
Final Answer:
Push your Astro project code to a GitHub repository -> Option A
Quick Check:
First step is pushing code to GitHub [OK]
Hint: Remember: Cloudflare Pages pulls from GitHub first [OK]
Common Mistakes:
Trying to deploy without a GitHub repo
Assuming local CLI is mandatory
Thinking Docker is required
2. Which build command is typically used in Cloudflare Pages settings for an Astro project?
easy
A. astro dev
B. npm run start
C. npm run build
D. npm install
Solution
Step 1: Understand Astro build process
Astro projects use npm run build to create the static files for deployment.
Step 2: Set build command in Cloudflare Pages
Cloudflare Pages needs the build command to generate the site files; npm run build is the correct command.
Final Answer:
npm run build -> Option C
Quick Check:
Build command = npm run build [OK]
Hint: Build command for Astro is always npm run build [OK]
Common Mistakes:
Using npm run start which runs a dev server
Using astro dev which is for local development
Confusing install with build
3. Given this Cloudflare Pages config snippet:
build:
command: "npm run build"
directory: "dist"
What will Cloudflare Pages do after you push your Astro site code?
medium
A. Run npm install and deploy files from node_modules
B. Run npm run build and deploy files from the dist folder
C. Ignore build and deploy all files in the repo
D. Run npm start and deploy files from the root folder
Solution
Step 1: Read the build command
The config says to run npm run build, which builds the Astro site into static files.
Step 2: Check the output directory
The directory dist is where Astro puts the built site files, so Cloudflare Pages deploys from there.
Final Answer:
Run npm run build and deploy files from the dist folder -> Option B
Quick Check:
Build command + output folder = deploy [OK]
Hint: Build command runs, output folder is deployed [OK]
Common Mistakes:
Confusing npm start with build
Deploying from wrong folder
Thinking node_modules is deployed
4. You connected your GitHub repo to Cloudflare Pages but the deployment fails with "Build command not found" error. What is the likely cause?
medium
A. You pushed code to the wrong GitHub branch
B. You used the wrong Cloudflare account
C. You did not enable GitHub Actions
D. You forgot to add a build script in package.json
Solution
Step 1: Understand the error message
"Build command not found" means Cloudflare tried to run the build command but it does not exist in your project.
Step 2: Check package.json scripts
The build command must be defined in the scripts section of package.json, usually as "build": "astro build" or similar.
Final Answer:
You forgot to add a build script in package.json -> Option D
Hint: Check package.json scripts for build command [OK]
Common Mistakes:
Assuming branch or account causes build command error
Thinking GitHub Actions must be enabled manually
Ignoring package.json scripts
5. You want to deploy an Astro site with environment variables on Cloudflare Pages. Which step is necessary to ensure your variables are available during build?
hard
A. Add environment variables in Cloudflare Pages dashboard under Settings > Environment Variables
B. Include variables in a .env file and push it to GitHub
C. Hardcode variables directly in your Astro source code
D. Set variables only in your local machine before pushing
Solution
Step 1: Understand environment variable usage in Cloudflare Pages
Cloudflare Pages requires environment variables to be set in its dashboard to inject them during build securely.
Step 2: Avoid pushing .env files with secrets
For security, do not push .env files with secrets to GitHub; instead, configure variables in Cloudflare Pages settings.
Final Answer:
Add environment variables in Cloudflare Pages dashboard under Settings > Environment Variables -> Option A
Quick Check:
Set env vars in Cloudflare dashboard for build [OK]
Hint: Set env vars in Cloudflare dashboard, not in repo [OK]
Common Mistakes:
Pushing .env files with secrets publicly
Hardcoding sensitive info in code
Assuming local env vars affect cloud build
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.