Skip to content
Johith Iype
Go back

How to easily deploy Static Sites using Astro Web Framework and Cloudflare Workers

A brief intro to the tech stack

Cloudflare Workers

It’s an umbrella term for Cloudflare’s serverless computing platform that let’s you run your code on their widespread global resources. You can deploy web applications, back-end services, host databases, run LLMs and more. Their massive CDN and caching capability handles fast delivery to my visitors pretty much anywhere.

I’ve utilized their generous free offering to deploy this static site blog.I choose to go with a static site and workers to reduce the overhead of having to manage an entire backend infrastructure for a simple blog. I implemented a CI/CD flow (using GitHub Actions) in 2024 to host my blog on AWS but it was a hassle to maintain the site. Cloudflare Worker has been an easy and wonderful experience for me so far.

Cloudflare Wrangler

It’s a tool for developers to interact and configure their Cloudflare Workers environment from the CLI.

Astro Static Site Generator

This is a very brief overview of how the Astro Static Site Generator (SSG) works.

Astro is an independent web framework you can use to create static sites. It also gives you the flexibility to bring in components from your favorite frameworks like React and Angular.

Node.js (a JavaScript runtime environment) does all the heavy lifting of downloading the dependencies (for building the site) and compiling the Astro’s native code into standard code (HTML/CSS/JS) understandable by web servers and clients (including Cloudflare Worker - edge server).

Note that Node.js does not directly compile Astro code, instead it runs some custom JavaScript code (understandable by Node.js) that then compiles Astro code to standard code.

This standard code (HTML/CSS/JS) can now be served by most web servers including Cloudflare’s edge servers using its Workers product.

Where does GitHub come into play?

GitHub simply acts as the cloud code repository. You configure your Cloudflare Worker project to authenticate and pull your Astro project code from your GitHub repository. Cloudflare then uses npm (or pnpm depending on the project) to build and use it’s internal server engine (I think it’s Cloudflare V8 server-less engine) to serve your site.

Let’s deploy your first site …

  1. Clone an Astro theme of your choice from it’s repository to your development environement.

    The theme I currently use: https://github.com/satnaing/astro-paper

  2. Clean up the cloned project by removing placeholder and sample files.

  3. Add .gitignore file so you don’t push local build files and logs

  4. Install Node.js npm (node’s package manager)

    I was trying to build this particular Astro theme locally using the standard npm and it was giving me dependency errors.

    Later I realized this theme was build using the pnpm ecosystem and so I had to install pnpm package manager to configure this project.

    To install node: grab the latest LTS binary from https://nodejs.org/en/blog/release/ and throw that binary in your /usr/local/bin/ or create a soft link to a location in $PATH.

    To install pnpm run the official installation script: curl -fsSL https://get.pnpm.io/install.sh | sh -

  5. To install dependencies (nodejs packages) ,under the root of your project directory run:

    pnpm install

    (the package list comes from package.json file)

  6. Install Cloudflare Wrangler

    1. Inside your local project run: pnpm add -D wrangler@latest
    2. Verify if it’s installed: npx wrangler --version
  7. Create your Wrangler Configuration File wrangler.jsonc

    {
      "name": "mysite",
      "compatibility_date": "YYYY-MM-DD", // Update to the day you deploy
      "assets": {
        "directory": "./dist",      // build directory
      }
    }
  8. Specify your Node.JS runtime version for the worker in a version file. Create a file .node-version and enter in it your NodeJS version.

    The reason why I had to specify the node version was because I was getting the below build error. Specifying a version of 22 fixed it.

    What’s confusing me still is according to their docs (https://developers.cloudflare.com/workers/ci-cd/builds/build-image/#overriding-default-versions), Cloudflare’s default build runtime is Node.JS version 22.16.0 which in fact should support my Astro node file that was built on version 22.12.

    pcie-passthrough

    pcie-passthrough

  9. Let’s build and preview the project locally before deploying it to Cloudflare.

    Since my project was created and managed using pnpm , I’ll run the below build commands:

    pnpm build

    The “script” to build the project is described in package.json file under the root directory.

    To preview the project on localhost run:

    npx wrangler dev --ip <IP-Address> (served at your IP interface for external access)

  10. When you’re satisfied with your work, create a GitHub repo (or Gitlab) and push all the files to it. This is the cloud repository for your site files.

    Follow the below steps to configure a Cloudflare Worker, connect it to your GitHub repo and have it scan your default branch (which is main) for any changes you push.

    When changes are pushed to the default branch, a production build is automatically triggered by the worker and publishes (or deploys) the built site.

  11. Create a new Cloudflare worker project and connect it your GitHub repo

    1. Navigate to Compute > Workers & Pages. Select Create application.
    2. Under Import a repository, select a Git account and then the repository containing your Astro project.

    pcie-passthrough

    c. Since I’m the only person working on this blog and to keep things simple, I’ve used my main branch as the production branch.

    If you want to change the production branch or setup a preview build branch, checkout: https://developers.cloudflare.com/workers/ci-cd/builds/build-branches/

  12. Let’s add the Build configuration variables.

    Under the project Settings add the below:

    pcie-passthrough

    NOTE: You can actually specify all your build config parameter in wrangler.jsonc (step 7).

    Below is a screenshot of a successful build.

pcie-passthrough

  1. After a successful build and deploy, let’s see how people can reach your site.

    Cloudflare provides an endpoint URL to use. You can view it under the Domains tab of your worker project.

    However, if you like to bring your custom domain, you can do so easily by heading over to the Domains tab under your worker project and hit the Add Domain. Choose your domain from the list and confirm.

    If your domain’s primary nameserver is a different service, you may have to look into setting up a CNAME record to point to the worker endpoint.

pcie-passthrough

  1. A final touch is to redirect www to your root domain
    1. Head over to Rules > Overview
    2. Create Rule
    3. Add a Wildcard match for Request URL: https://www.*
    4. For Target URL: https://${1} with a status Code of 301 and enable Preserve Query String
    5. I’ll then ask you to create a DNS record for www. Just add a proxied A record for it and point it to 192.0.2.1 . This IP is just a dummy placeholder and acts as a front-end to Cloudflare’s routing engine so it redirects based on the set rules.

Share this post:

Next Post
Passthrough /dev/dri/renderD128 node to a containerized Jellyfin instance with docker usernd-remap enabled