Skip to content

Astro

In this guide, you will create a new Astro application and deploy to Cloudflare Workers (with the new Beta Workers Assets).

1. Set up a new project

Use the create-cloudflare CLI (C3) to set up a new project. C3 will create a new project directory, initiate Astro's official setup tool, and provide the option to deploy instantly.

To use create-cloudflare to create a new Astro project with Beta Workers Assets, run the following command:

Terminal window
npm create cloudflare@latest my-astro-app -- --framework=astro --experimental

For setup, select the following options:

  • For What would you like to start with?, choose Framework Starter.
  • For Which development framework do you want to use?, choose Astro.
  • Complete the framework's own CLI wizard.
  • For Do you want to use git for version control?, choose Yes.
  • For Do you want to deploy your application?, choose No (we will be making some changes before deploying).

After setting up your project, change your directory by running the following command:

Terminal window
cd my-astro-app

2. Develop locally

After you have created your project, run the following command in the project directory to start a local server. This will allow you to preview your project locally during development.

Terminal window
npm run dev

3. Deploy your Project

Your project can be deployed to a *.workers.dev subdomain or a Custom Domain, from your own machine or from any CI/CD system, including Cloudflare's own.

The following command will build and deploy your project. If you're using CI, ensure you update your "deploy command" configuration appropriately.

Terminal window
npm run deploy

Bindings

Your Astro application can be fully integrated with the Cloudflare Developer Platform, in both local development and in production, by using product bindings. The Astro documentation provides information about configuring bindings and how you can access them in your locals.

Static assets

You can serve static assets your Astro application by placing them in the ./public/ directory. This can be useful for resource files such as images, stylesheets, fonts, and manifests.

When using Workers Assets, Cloudflare will first attempt to serve any static assets which match the incoming request.

For example, if you have requests for /logo.png and /blog/hello-world.html in your assets directory, and make requests for /logo.png and /blog/hello-world, those files will be served respectively. The html_handling option allows you to customize the serving of HTML files if you have specific needs around redirects and trailing slashes.

If a request does not match a static asset, Cloudflare will then invoke your Worker script module, if one is present. This can be configured with the main property in wrangler.toml.

Finally, if a request does not match any static assets, and either a Worker script module is not present, or from within that Worker script module, the asset binding's fetch method is called (e.g. env.ASSETS.fetch(request)), Cloudflare will fall back to evaluating the not_found_handling behavior. By default, it will serve a null-body 404-status response, but this can be configured to instead serve custom HTML 404 pages, or to serve a single-page application (SPA).

At present, there is no way to customize this routing behavior beyond the html_handling and not_found_handling options. We plan to offer additional configuration controls, such as allowing you to always run the Worker script modules for certain paths, in the future.