Engineering & AI
Deploy a static website to Cloudflare Pages for free, on your own domain
Put your built website on Cloudflare Pages for free: connect GitHub or drag and drop the build folder, fix common build errors, add your own domain.
Summary
- Cloudflare Pages hosts static websites for free: the account needs only an email, and requests for static files are not billed.
- Connect GitHub so every push updates the site; without Git, drag and drop the build folder, up to 1,000 files.
- The parts that go wrong are the build settings, API keys exposed in the code, and the domain setup; each has its own check.

You built a website with Cursor, Claude Code or Lovable, it runs fine on your machine, and now it needs to be online on your own domain without renting a server. This guide shows how to put it on Cloudflare Pages for free, fix the usual build errors and connect a domain.
Compiled from the official Cloudflare, Vite and Next.js documentation, checked on 27 November 2026. Cloudflare may rename buttons in its dashboard over time.
When does Cloudflare Pages fit your site?
- Static sites: after the build, only HTML, CSS, JS and images are left. Examples: a landing page, a portfolio, a React app built with Vite, Astro, a static Next.js export.
- The free plan covers most small sites: 500 builds a month, up to 20,000 files per site, 25 MiB per file, 100 custom domains per project.
- Not a fit when the site needs a server running all the time: your own login, your own database, sending email from a server, or Next.js with Server Actions and API routes that read the request.
- A Cloudflare account needs only an email, no card. A card is needed only to buy a domain.
1. Not sure whether to connect GitHub or drag and drop
Problem
Cloudflare offers three ways to deploy. Pick the wrong one and every change to the site means repeating several manual steps.
Evaluate
- Connect to Git: Cloudflare builds on every push, and branches other than
mainget their own preview link. Fits when the code is already on GitHub or GitLab, private repositories included. - Drag and drop: build on your machine, then drop the build folder or a zip file into the browser. No Git needed, but at most 1,000 files, and every change means building and uploading again.
- Wrangler CLI:
npx wrangler pages deployuploads the build folder from your machine, up to 20,000 files. Fits people at home in a terminal.
Solution
- On GitHub, choose Connect to Git. Go to Workers & Pages → Create application → Pages tab → Connect to Git, pick the repository, then Begin setup.
- Without Git: run
npm run buildlocally, then go to Workers & Pages → Create application → Get started → Drag and drop your files, name the project and drop the build folder in. - From a terminal: run these two commands in the project folder, replacing
distwith your build folder.
npx wrangler login
npx wrangler pages deploy dist- Check: open the
<project-name>.pages.devlink Cloudflare gives you, click through a few pages and open it on a phone.
2. The Cloudflare build fails or shows a blank page
Problem
When you connect GitHub, Cloudflare asks for a build command and an output directory. Get them wrong and the build fails, or it passes but the site is blank because Cloudflare published the wrong folder.
Evaluate
- Each framework writes to a different folder. Cloudflare ships presets per framework; the right preset is enough for most projects.
- Next.js does not export a static site by default. Without
output: 'export'innext.configthere is nooutfolder to publish. - The Node version differs from your machine. Cloudflare's v3 build image defaults to Node 22 and ignores
enginesinpackage.json.
Solution
- Pick the preset or fill in from this table (from Cloudflare's build configuration docs):
| Framework | Build command | Output directory |
|---|---|---|
| React or Vue with Vite | npm run build | dist |
| Astro | npm run build | dist |
| Next.js (Static HTML Export) | npx next build | out |
| Plain HTML, no build | leave empty | the folder holding index.html |
- For Next.js: add
output: 'export'tonext.config. If you usenext/imagewith the default loader, setimages: { unoptimized: true }or use a custom loader, since a static export does not support the default image optimization. - Pin the Node version: add an
.nvmrcfile with the version you use (for example22), or aNODE_VERSIONenvironment variable in the project Settings. - A repository with several projects: enter the subfolder under Root directory (advanced).
- Check: run
npm run buildlocally and confirm the output folder hasindex.htmlat its root. On Cloudflare, open the build log to read any error.
3. Images and CSS do not load, the page shows bare text
Problem
The site is up but unstyled: CSS, JS and image files return 404 in DevTools.
Evaluate
- The usual cause is the base path. Many GitHub Pages guides set
base: '/<repo-name>/'invite.config. On Cloudflare Pages the site runs at the root of the domain, so that path is wrong.
Solution
- Set
base: '/'or remove thebaseline invite.config. In Next.js, removebasePathunless the site runs under a subpath. - Check: run
npx vite preview(or serve theoutfolder with any web server) locally, open DevTools → Network, and confirm no file returns 404.
4. Reloading a subpage fails, or a wrong link shows the home page
Problem
A single-page app (SPA) routes in the browser. A visitor who opens /about directly or reloads the page can get a 404.
Evaluate
- Cloudflare Pages handles SPAs on its own: with no
404.htmlat the root of the build folder, any path without a matching file returnsindex.htmlfor your router to handle. - A root
404.htmlturns this off. If you or an AI agent add a 404 page at the root, the SPA starts returning 404 when a subpage is reloaded. - The flip side: a mistyped link also shows the home page with status 200, so the app must show its own "Not found" page for unknown paths.
Solution
- SPA (Vite with React Router or Vue Router): keep
404.htmlout of the build folder root. Add a "not found" route in the router. - Multi-page sites (Astro, static Next.js): keep
404.html, since every page has its own HTML file. - Check: open a subpage directly on the
.pages.devlink, reload it, then type a path that does not exist.
5. API keys sit in the site's code for anyone to read
Problem
The site calls an AI, payment or database service with an API key from the .env file. After the build, the key is in a JS file anyone can read in DevTools.
Evaluate
- A static site has nowhere to hide secrets. Vite puts every variable starting with
VITE_into the code that runs in the browser, and the Vite docs advise against putting API keys there. Next.js does the same withNEXT_PUBLIC_variables. - Keys designed to be public are fine, such as the public key of a service with per-user access rules turned on. Secret keys (OpenAI, payment gateways, database accounts) are not.
- The quick fix is the leak: when the site complains about a missing variable, adding the
VITE_prefix makes it work, and sends the key straight into browser code.
Solution
- List every variable in
.envand mark which ones are secret. - Move calls that need a secret key to a server: your own backend, or Cloudflare Workers/Pages Functions (out of scope here).
- Revoke and recreate the key if it was ever on the site or in a public repository.
- Check: after the build, search the output folder for the key (for example
grep -r "sk-" dist) and get no match.
6. Connect your own domain
Problem
The <project-name>.pages.dev link works, but you want visitors to see your own domain.
Evaluate
- An apex domain (
example.com) must be added to Cloudflare as a zone, which means pointing the domain's nameservers to Cloudflare. - A subdomain (
shop.example.com) needs no nameserver change: add one CNAME record pointing to<project-name>.pages.devwherever your DNS is managed. - Buying the domain on Cloudflare: Cloudflare Registrar charges what the registry charges, and the domain is already in your account, so it connects to Pages fastest. Cloudflare does not sell
.vn; buy.vnfrom a Vietnamese registrar and point it over.
Solution
- Find a free name: check it under 18 popular endings with HubCom's domain checker, then buy it from the registrar you choose.
- Add the domain to the project first: Workers & Pages → your project → Custom domains → Set up a domain, and enter the domain.
- Follow the steps Cloudflare shows: change nameservers for an apex domain; create a CNAME for a subdomain at another registrar.
- Check: the domain shows Active under Custom domains, and
https://opens the site with no certificate warning.
Preparing the AI agent
- Framework and version: Vite, Astro or Next.js; the Node version on your machine.
- Output directory and a build command that already succeeds locally.
- Routing: an SPA with browser routing, or one HTML file per page.
- Environment variables, marked secret or public.
- The domain you will use: apex or subdomain, and where its DNS is managed.
- One clear instruction: "Configure the project to export a static site with base path
/, use no server-only features, and keep secret keys out of browser code."
Out of scope
Writing APIs with Pages Functions or Workers, running full server-side Next.js on Cloudflare, moving a project from Pages to Workers static assets, Bulk Redirects, and measuring traffic with Cloudflare Web Analytics.
Terms in this article (7)
- Cloudflare Pages
- Cloudflare's service that serves websites from its global network, building from GitHub or taking an uploaded build folder.
- Static website
- A site that, once built, is only HTML, CSS, JS and image files, with no server running code for each visit.
- Build output directory
- The folder the build command produces, holding the files that go online, such as dist for Vite or out for Next.js.
- SPA (Single-page application)
- A web app with a single index.html that changes pages with JavaScript in the browser.
- Wrangler
- Cloudflare's command-line tool for creating projects and deploying from your machine.
- CNAME
- A DNS record that points a subdomain to another domain, such as shop.example.com to project-name.pages.dev.
- Nameserver
- The DNS server in charge of a domain's records; pointing nameservers to Cloudflare hands DNS management to Cloudflare.
Frequently asked questions
Is Cloudflare Pages really free?
Yes, for static sites. The free plan gives 500 builds a month and 20,000 files per site, and requests for static files are not billed. Only a custom domain costs money.
Should I connect GitHub or drag and drop the build folder?
Connect GitHub if the code is already there: every push updates the site. Drag and drop fits when you do not use Git and the site has fewer than 1,000 files.
Can I use a .vn domain with Cloudflare Pages?
Yes. Cloudflare does not sell .vn, so buy it from a Vietnamese registrar, then point its nameservers to Cloudflare or create a CNAME for a subdomain.
Why is my site blank after deploying?
Usually a wrong output directory, a wrong base path or a JavaScript error. Open DevTools, check the Console and Network tabs, and compare your build settings with the table above.
Sources
- Limits, Cloudflare Docs
- Direct Upload, Cloudflare Docs
- Git integration, Cloudflare Docs
- Build configuration, Cloudflare Docs
- Build image, Cloudflare Docs
- Serving Pages, Cloudflare Docs
- Custom domains, Cloudflare Docs
- Cloudflare Registrar, Cloudflare
- Env Variables and Modes, Vite
- How to create a static export of your Next.js application, Next.js