Deploy your Astro Site to Cloudflare
यह कंटेंट अभी तक आपकी भाषा में उपलब्ध नहीं है।
You can deploy full-stack applications, including front-end static assets and back-end APIs, as well as SSR sites, to both Cloudflare Workers and Cloudflare Pages.
This guide includes:
Prerequisites
Section titled PrerequisitesTo get started, you will need:
- A Cloudflare account. If you don’t already have one, you can create a free Cloudflare account during the process.
Cloudflare Workers
Section titled Cloudflare WorkersHow to deploy with Wrangler
Section titled How to deploy with Wrangler-
Install Wrangler CLI.
Terminal window npm install wrangler@latest --save-dev -
If your site uses on demand rendering, install the
@astrojs/cloudflare
adapter.This will install the adapter and make the appropriate changes to your
astro.config.mjs
file in one step.Terminal window npx astro add cloudflareThen, create a
.assetsignore
file in yourpublic/
folder, and add the following lines to it:public/.assetsignore _worker.js_routes.jsonRead more about on demand rendering (also known as SSR) in Astro. -
Create a Wrangler configuration file.
wrangler.jsonc {"$schema": "node_modules/wrangler/config-schema.json","name": "my-astro-app",// Update to today's date"compatibility_date": "2025-03-25","assets": {"directory": "./dist"}}wrangler.jsonc {"$schema": "node_modules/wrangler/config-schema.json","name": "my-astro-app","main": "./dist/_worker.js/index.js",// Update to today's date"compatibility_date": "2025-03-25","compatibility_flags": ["nodejs_compat"],"assets": {"binding": "ASSETS","directory": "./dist"},"observability": {"enabled": true}} -
Preview your project locally with Wrangler.
Terminal window npx astro build && npx wrangler dev -
Deploy using
npx wrangler deploy
.Terminal window npx astro build && npx wrangler deploy
After your assets are uploaded, Wrangler will give you a preview URL to inspect your site.
How to deploy with CI/CD
Section titled How to deploy with CI/CDYou can also use a CI/CD system such as Workers Builds (BETA) to automatically build and deploy your site on push.
If you’re using Workers Builds:
-
Follow Steps 1-3 from the Wrangler section above.
-
Log in to the Cloudflare dashboard and navigate to
Workers & Pages
. SelectCreate
. -
Under
Import a repository
, select a Git account and then the repository containing your Astro project. -
Configure your project with:
- Build command:
npx astro build
- Deploy command:
npx wrangler deploy
- Build command:
-
Click
Save and Deploy
. You can now preview your Worker at its providedworkers.dev
subdomain.
Cloudflare Pages
Section titled Cloudflare PagesHow to deploy with Wrangler
Section titled How to deploy with Wrangler-
Install Wrangler CLI.
Terminal window npm install wrangler@latest --save-dev -
If your site uses on demand rendering, install the
@astrojs/cloudflare
adapter.This will install the adapter and make the appropriate changes to your
astro.config.mjs
file in one step.Terminal window npx astro add cloudflareRead more about on demand rendering in Astro. -
Preview your project locally with Wrangler.
Terminal window npx astro build && npx wrangler pages dev ./dist -
Deploy using
npx wrangler deploy
.Terminal window npx astro build && npx wrangler pages deploy ./dist
After your assets are uploaded, Wrangler will give you a preview URL to inspect your site.
How to deploy a site with Git
Section titled How to deploy a site with Git-
Push your code to your git repository (e.g. GitHub, GitLab).
-
Log in to the Cloudflare dashboard and navigate to
Workers & Pages
. SelectCreate
and then select thePages
tab. Connect your git repository. -
Configure your project with:
- Framework preset:
Astro
- Build command:
npm run build
- Build output directory:
dist
- Framework preset:
-
Click the Save and Deploy button.
Troubleshooting
Section titled TroubleshootingClient-side hydration
Section titled Client-side hydrationClient-side hydration may fail as a result of Cloudflare’s Auto Minify setting. If you see Hydration completed but contains mismatches
in the console, make sure to disable Auto Minify under Cloudflare settings.
Node.js runtime APIs
Section titled Node.js runtime APIsIf you are building a project that is using on-demand rendering with the Cloudflare SSR adapter and the server fails to build with an error message such as [Error] Could not resolve "XXXX. The package "XXXX" wasn't found on the file system but is built into node.
:
-
This means that a package or import you are using in the server-side environment is not compatible with the Cloudflare runtime APIs.
-
If you are directly importing a Node.js runtime API, please refer to the Astro documentation on Cloudflare’s Node.js compatibility for further steps on how to resolve this.
-
If you are importing a package that imports a Node.js runtime API, check with the author of the package to see if they support the
node:*
import syntax. If they do not, you may need to find an alternative package.