将你的 Astro 网站部署到 Cloudflare
你可以将全栈应用(包括前端静态资源和后端 API)以及按需渲染的网站部署到 Cloudflare Workers 和 Cloudflare Pages。
¥You can deploy full-stack applications, including front-end static assets and back-end APIs, as well as on-demand rendered sites, to both Cloudflare Workers and Cloudflare Pages.
本指南包括:
¥This guide includes:
¥Prerequisites
首先,你将需要:
¥To get started, you will need:
- Cloudflare 账户。如果你还没有,你可以在此过程中创建一个免费的 Cloudflare 账户。
Cloudflare Workers
Section titled “Cloudflare Workers”如何使用 Wrangler 部署
Section titled “如何使用 Wrangler 部署”¥How to deploy with Wrangler
-
Install Wrangler CLI.
终端窗口 npm install wrangler@latest --save-dev -
If your site uses on-demand rendering, install the
@astrojs/cloudflareadapter.This will install the adapter and make the appropriate changes to your
astro.config.mjsfile in one step.终端窗口 npx astro add cloudflare终端窗口 pnpm astro add cloudflare终端窗口 yarn astro add cloudflareRead more about on-demand rendering in Astro. -
Create a Wrangler configuration file.
Running
astro add cloudflarewill create this for you; if you are not using the adapter, you’ll need to create it yourself.wrangler.jsonc {"name": "my-astro-app","compatibility_date": "YYYY-MM-DD", // Update to the day you deploy"assets": {"directory": "./dist",}}wrangler.jsonc {"main": "dist/_worker.js/index.js","name": "my-astro-app","compatibility_date": "YYYY-MM-DD", // Update to the day you deploy"compatibility_flags": ["nodejs_compat","global_fetch_strictly_public"],"assets": {"binding": "ASSETS","directory": "./dist"},"observability": {"enabled": true}} -
Preview your project locally with Wrangler.
终端窗口 npx astro build && npx wrangler dev -
Deploy using
npx wrangler deploy.终端窗口 npx astro build && npx wrangler deploy
上传资源后,Wrangler 将为你提供一个预览 URL 以检查你的网站。
¥After your assets are uploaded, Wrangler will give you a preview URL to inspect your site.
如何使用 CI/CD 部署
Section titled “如何使用 CI/CD 部署”¥How to deploy with CI/CD
你还可以使用 CI/CD 系统(例如 Workers 构建(BETA 版))在推送时自动构建和部署你的网站。
¥You can also use a CI/CD system such as Workers Builds (BETA) to automatically build and deploy your site on push.
如果你使用的是 Workers Builds:
¥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.devsubdomain.
Cloudflare Pages
Section titled “Cloudflare Pages”如何使用 Wrangler 部署
Section titled “如何使用 Wrangler 部署”¥How to deploy with Wrangler
-
Install Wrangler CLI.
终端窗口 npm install wrangler@latest --save-dev终端窗口 pnpm add wrangler@latest --save-dev终端窗口 yarn add wrangler@latest --dev -
If your site uses on-demand rendering, install the
@astrojs/cloudflareadapter.This will install the adapter and make the appropriate changes to your
astro.config.mjsfile in one step.终端窗口 npx astro add cloudflare终端窗口 pnpm astro add cloudflare终端窗口 yarn astro add cloudflare -
Create a Wrangler configuration file.
Because Cloudflare recommends new projects use Workers instead of Pages, the
astro add cloudflarecommand creates awrangler.jsoncandpublic/.assetsignorefile, which are specific to Workers projects. You will need to delete thepublic/.assetsignorefile and change yourwrangler.jsoncfile. If you are not using the adapter you’ll need to create it yourself.Ensure your
wrangler.jsoncfile is structured like this:wrangler.jsonc {"name": "my-astro-app","compatibility_date": "YYYY-MM-DD", // Update to the day you deploy"pages_build_output_dir": "./dist"}wrangler.jsonc {"name": "my-astro-app","compatibility_date": "YYYY-MM-DD", // Update to the day you deploy"compatibility_flags": ["nodejs_compat","disable_nodejs_process_v2"],"pages_build_output_dir": "./dist"}Read more about on-demand rendering in Astro. -
Preview your project locally with Wrangler.
终端窗口 npx astro build && wrangler pages dev ./dist终端窗口 pnpm astro build && wrangler pages dev ./dist终端窗口 yarn astro build && wrangler pages dev ./dist -
Deploy using
npx wrangler deploy.终端窗口 npx astro build && wrangler pages deploy ./dist终端窗口 pnpm astro build && wrangler pages deploy ./dist终端窗口 yarn astro build && wrangler pages deploy ./dist
上传资源后,Wrangler 将为你提供一个预览 URL 以检查你的网站。
¥After your assets are uploaded, Wrangler will give you a preview URL to inspect your site.
如何使用 CI/CD 部署网站
Section titled “如何使用 CI/CD 部署网站”¥How to deploy a site with CI/CD
-
Push your code to your git repository (e.g. GitHub, GitLab).
-
Log in to the Cloudflare dashboard and navigate to Compute (Workers) > Workers & Pages. Select Create and then select the Pages 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
404 错误行为
Section titled “404 错误行为”¥404 behavior
对于 Workers 项目,如果你想要提供自定义 404 页面,则需要设置 not_found_handling。你可以在 Cloudflare 文档的 路由行为部分 部分了解更多信息。
¥For Workers projects, you will need to set not_found_handling if you want to serve a custom 404 page. You can read more about this in the Routing behavior section of Cloudflare’s documentation.
{ "assets": { "directory": "./dist", "not_found_handling": "404-page" }}对于 Pages 项目,如果你包含自定义 404 页面,则默认情况下会显示该页面。否则,页面将默认使用 Cloudflare 单页应用渲染行为 并重定向到主页,而不是显示 404 页面。
¥For Pages projects, if you include a custom 404 page, it will be served by default. Otherwise, Pages will default to Cloudflare’s single-page application rendering behavior and redirect to the home page instead of showing a 404 page.
¥Client-side hydration
由于 Cloudflare 的自动缩小设置,客户端水合作用可能会失败。如果你在控制台中看到 Hydration completed but contains mismatches,请确保在 Cloudflare 设置下禁用 Auto Minify。
¥Client-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 运行时 API
Section titled “Node.js 运行时 API”¥Node.js runtime APIs
如果你正在构建使用 Cloudflare 适配器 进行按需渲染的项目,并且服务器无法构建并显示错误消息(如 [Error] Could not resolve "XXXX. The package "XXXX" wasn't found on the file system but is built into node.):
¥If you are building a project that is using on-demand rendering with the Cloudflare 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.:
-
这意味着你在服务器端环境中使用的包或导入与 Cloudflare 运行时 API 不兼容。
-
如果你直接导入 Node.js 运行时 API,请参阅 Astro 文档中有关 Cloudflare 的 Node.js 兼容性 的更多步骤以了解如何解决此问题。
-
如果你导入的包会导入 Node.js 运行时 API,请与包的作者核实他们是否支持
node:*导入语法。如果没有,你可能需要寻找替代包。