CH 45 / 50Chapter 8: Hands-On

Publishing a Static Site on Cloudflare

On this page19 items

On the previous page, we built a Worker that runs code on every request. This time we’ll publish a site that serves files as they are.

This covers a wide range: from simple sites that are just HTML, CSS and images, to sites generated by static site generators such as Astro, Next.js, Hugo and Jekyll, to single-page applications built with React or Vue. Portfolios, tech blogs, corporate landing pages, documentation sites: a large share of the websites out there can be published this way.

And the important point is that requests for static files are free and unlimited. For a personal site, the cost really does come to zero.

What you’ll learn on this page#

  • The two ways to publish a static site on Cloudflare, and which to choose
  • How to publish with a single command
  • How to connect GitHub so that a push is all it takes to update
  • Assigning a custom domain, and configuring routing for SPAs
  • The scope of the free tier, and limits you should know about

Workers or Pages?#

There are two ways to publish a static site on Cloudflare. As mentioned in Cloudflare Pages in Chapter 5, the two are currently in the middle of being merged.

  • Cloudflare Pages: A service built specifically for static site hosting. It’s still fully in service
  • Workers static assets: Workers with the added ability to serve files. This is the recommended option for new projects

Pages isn’t being discontinued. But new features are being added on the Workers side, so if you’re building something now, Workers is the natural choice.

Here’s a rule of thumb for when you’re unsure.

Workers (static assets)Pages
New projectsRecommendedStill available
Serving static filesFree, unlimitedFree, unlimited
Adding dynamic processingWrite it directly in the same projectWrite it in Functions
Cron, Durable Objects, etc.AvailablePartly unavailable
Domains not managed by CloudflareNot possiblePossible via CNAME

This page covers the Workers approach.

Method 1: Publish with a command#

This is the fastest way. You can publish a folder that already has an index.html as-is.

Create a minimal setup#

Make a working folder and put the following two files in it.

First, public/index.html.

text10 lines
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>My first static site</title>
</head>
<body>
  <h1>Published on Cloudflare</h1>
</body>
</html>

Next, wrangler.jsonc directly under the folder.

text7 lines
{
  "name": "my-static-site",
  "compatibility_date": "2026-08-20",
  "assets": {
    "directory": "./public"
  }
}

That’s it. You don’t need a single line of JavaScript. You don’t even need to specify main.

Deploy#

text1 lines
npx wrangler deploy

Within seconds, https://my-static-site.your-subdomain.workers.dev is live. Go ahead and open it.

To check it locally, use the same command as on the previous page.

text1 lines
npx wrangler dev

Two ways to publish a static site

If you use a static site generator#

If you build with Astro, Hugo, Next.js or similar, just point directory at the build output folder.

text3 lines
"assets": {
  "directory": "./dist"
}

The output folder varies by tool: dist, out, build, _site and so on. Specify the folder name that appears when you build locally.

There are also dedicated templates for these frameworks.

text1 lines
npm create cloudflare@latest -- my-site --framework=astro

With this, the project is created with that framework’s configuration already included.

Method 2: Auto-deploy with GitHub#

In real-world use, this is the more convenient option. Just push to GitHub, and the build and deploy happen automatically.

In the Cloudflare dashboard, choose Workers & Pages → Create application → Import a repository, and connect a GitHub or GitLab repository.

There are three main settings.

SettingExampleDescription
Build commandnpm run buildLeave blank if no build is needed
Output directorydistWhere the generated files go
Production branchmainPushes here update production

Once connected, it works like this.

  • Push to main → it builds automatically and production is updated
  • Open a pull request → a dedicated preview URL is created, so you can actually open and check the changes
  • The build status shows up on GitHub → a comment is added to the pull request

This preview feature is especially handy. “Checking a fix at a real URL before it goes to production” works with no extra setup.

Note that self-hosted GitHub and GitLab are not supported (as of August 2026).

Assigning a custom domain#

It works fine on workers.dev, but a real site uses a custom domain.

Writing it in the config file works the same way as on the previous page.

text8 lines
{
  "name": "my-static-site",
  "compatibility_date": "2026-08-20",
  "assets": { "directory": "./public" },
  "routes": [
    { "pattern": "www.example.com", "custom_domain": true }
  ]
}

When you deploy, the DNS record and certificate are set up automatically.

You can also set it up from the dashboard. On the Worker’s details screen, go to Settings → Domains & Routes → Add and enter the hostname you want to use.

404 pages and SPAs#

A question that always comes up with static sites is “what happens when someone visits a URL that doesn’t exist?” You specify this with not_found_handling in the config file.

Ordinary sites (blogs, corporate sites, etc.)#

text4 lines
"assets": {
  "directory": "./dist",
  "not_found_handling": "404-page"
}

If you provide a 404.html, it’s shown for URLs that don’t exist.

SPAs (apps built with React, Vue, etc.)#

text4 lines
"assets": {
  "directory": "./dist",
  "not_found_handling": "single-page-application"
}

Without this setting, an SPA shows the symptom “the top page loads, but opening a URL directly gives a 404.” In an SPA, routing is handled by JavaScript in the browser, so the server needs to return index.html for every URL. This setting makes it behave that way.

When you want to add dynamic processing#

After publishing a static site, requests like “I want a contact form” or “I want to count visits” come up.

With Workers static assets, you just add code to the same project.

text9 lines
{
  "name": "my-static-site",
  "main": "src/index.js",
  "compatibility_date": "2026-08-20",
  "assets": {
    "directory": "./dist",
    "binding": "ASSETS"
  }
}

Add main and specify binding. Then write the code.

text13 lines
export default {
    async fetch(request, env) {
        const url = new URL(request.url);

        // Handle only URLs starting with /api/ ourselves
        if (url.pathname.startsWith("/api/")) {
            return Response.json({ message: "This is a response from the API" });
        }

        // Return static files for everything else
        return env.ASSETS.fetch(request);
    },
};

The static site and the API live together in one project. Connect D1 and it becomes a site with a database; connect Workers AI and it gains AI features.

If you add a form, the standard combination is Turnstile for spam protection and Email Routing if you want to receive submissions by email.

Cost and limits#

Free tier and limits for static assets

Access to static files is free#

Requests for static files are free and unlimited. Storing the files costs nothing either. Whether it’s a personal site or one with a fair amount of traffic, this part never generates a bill.

The only thing billed is requests where Worker code runs. On a purely static site, no code runs.

Limits you should know#

ItemFreePaid
Files per version20,000100,000
Size per file25MiB25MiB
Rules in _headers100100
Total in _redirects2,1002,100

The file count limit can become a problem on sites with lots of images. If you’re hosting tens of thousands of photos, as in a photo gallery, consider moving just the images to R2.

The 25MiB-per-file limit catches video files. Large videos shouldn’t be served as static assets in the first place.

Points beginners should watch out for#

  • directory points to the build output: If you specify the source folder, files like .md get published as-is. Specify the folder produced by the build
  • Don’t forget not_found_handling for SPAs: This is almost always the cause of “the top page loads, but direct links give a 404”
  • Don’t put secret files in it: Everything in the folder you specify as directory is published. Before deploying, check that no .env or backup files have slipped in
  • Check that caches have updated: If you see old content after an update, it’s either the browser cache or Cloudflare’s cache. Check in a private window, and purge from the dashboard if necessary
  • No need to rush migrating from Pages to Workers: Pages projects that are already running can keep being used as they are
  • Preview URLs are public too: The URL created for each pull request can be opened by anyone who knows it. If it contains unreleased information, protect it with Cloudflare Access
  • wrangler deploy goes straight to production: If you want an extra check step, using the preview feature of the GitHub integration is safer

Related services#

Summary#

  • For new static sites, Workers static assets is recommended. Pages remains available
  • Write assets.directory in wrangler.jsonc and run npx wrangler deploy, and you’re published. No code needed
  • Connect GitHub and pushes deploy automatically, with a preview URL created for each pull request
  • For a custom domain, just add custom_domain: true in routes. DNS and certificates are automatic
  • For SPAs, always specify not_found_handling: "single-page-application"
  • Requests for static files are free and unlimited. You’re billed only when Worker code runs
  • The file count limit is 20,000 on Free / 100,000 on Paid, with up to 25MiB per file
  • Add main and binding later and an API can live in the same project

No server contract, no FTP uploads, no certificate renewals, and your site is delivered worldwide. Publishing a static site is the lowest-barrier entry point to Cloudflare, with results that are easy to see.

On the next page, we’ll create an email address on your own domain without running a server.

Read next#