Skip to content

Commit 2b019f2

Browse files
i18n(zh-cn): Update /deploy/cloudflare.mdx (#11525)
Co-authored-by: Yan <[email protected]>
1 parent 8942e4a commit 2b019f2

File tree

1 file changed

+131
-69
lines changed

1 file changed

+131
-69
lines changed

src/content/docs/zh-cn/guides/deploy/cloudflare.mdx

+131-69
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,183 @@
11
---
2-
title: 部署你的 Astro 站点至 Cloudflare Pages
3-
description: 如何使用 Cloudflare Pages 将你的 Astro 网站部署到网络上
2+
title: 部署你的 Astro 站点至 Cloudflare
3+
description: 如何使用 Cloudflare 将你的 Astro 网站部署到网络上
44
sidebar:
55
label: Cloudflare
66
type: deploy
77
i18nReady: true
88
---
99
import ReadMore from '~/components/ReadMore.astro';
1010
import { Steps } from '@astrojs/starlight/components';
11+
import StaticSsrTabs from '~/components/tabs/StaticSsrTabs.astro';
1112

12-
你可以将你的 Astro 项目部署在 [Cloudflare Pages](https://pages.cloudflare.com/) 上。Cloudflare Pages 是一个供前端开发人员协作和部署静态 (JAMstack) 或 SSR 网站的平台
13+
你可以通过 [Cloudflare Workers](https://developers.cloudflare.com/workers/static-assets/)[Cloudflare Pages](https://pages.cloudflare.com/) 来部署全栈应用,包括前端的静态资源和后端的 API,以及按需渲染站点
1314

1415
本指南包含:
1516

16-
- [如何使用 Git 部署一个站点](#如何使用-git-部署一个站点)
17-
- [如何使用 Wrangler 部署一个站点](#如何使用-wrangler-部署一个站点)
18-
- [如何部署 SSR 站点](#如何部署-ssr-站点)
17+
- [如何部署至 Cloudflare Workers](#cloudflare-workers)
18+
- [如何部署至 Cloudflare Pages](#cloudflare-pages)
19+
20+
<ReadMore>阅读有关在 Astro 项目中 [使用 Cloudflare 运行时](/zh-cn/guides/integrations-guide/cloudflare/) 的更多内容。</ReadMore>
1921

2022
## 前提条件
2123

2224
开始之前,你需要:
2325

24-
- 一个 Cloudflare 账号。如果你暂时还没有,你可以现在免费去 Cloudflare 官网注册一个。
25-
- 你的源代码存储在一个 [GitHub](https://github.com/) 或者 [GitLab](https://about.gitlab.com/) 仓库里。
26-
27-
## 如何使用 Git 部署一个站点
28-
29-
<Steps>
30-
1. 将你的代码提交到一个 Git 仓库中 (GitHub, GitLab)。
31-
32-
2. 在 Cloudflare Pages 设置一个新项目。使用以下的构建设置:
33-
34-
- **Framework preset(框架预设):** `Astro`
35-
- **Build command(构建命令):** `npm run build`
36-
- **Build output directory(构建输出目录):** `dist`
26+
- 一个 Cloudflare 账号。如果你暂时还没有,可通过 Cloudflare 官网免费注册。
3727

38-
3. 点击 **Save and Deploy**(保存并部署)按钮。
39-
</Steps>
28+
## Cloudflare Workers
4029

41-
## 如何使用 Wrangler 部署一个站点
30+
### 如何使用 Wrangler 进行部署
4231

4332
<Steps>
4433
1. 安装 [Wrangler CLI](https://developers.cloudflare.com/workers/wrangler/get-started/)
4534

46-
2. 使用 `wrangler login` 在 Wrangler 登录 Cloudflare 账号并授权。
47-
48-
3. 运行你的构建命令(比如 `npm run build`)。
35+
```bash
36+
npm install wrangler@latest --save-dev
37+
```
4938

50-
4. 使用 `npx wrangler pages deploy dist` 进行部署。
51-
</Steps>
39+
2. 如果你的站点使用了按需渲染,安装 [`@astrojs/cloudflare` 适配器](/zh-cn/guides/integrations-guide/cloudflare/)
40+
41+
该步骤将会安装适配器,并对你的 `astro.config.mjs` 文件进行适当的调整。
42+
43+
```bash
44+
npx astro add cloudflare
45+
```
46+
47+
接下来,在你的 `public/` 文件夹中创建一个 `.assetsignore` 文件,并向其添加以下内容:
48+
```txt title="public/.assetsignore"
49+
_worker.js
50+
_routes.json
51+
```
52+
53+
<ReadMore>阅读更多有关 [Astro 中的按需渲染](/zh-cn/guides/on-demand-rendering/)的内容。</ReadMore>
54+
55+
3. 创建一个 [Wrangler 配置项文件](https://developers.cloudflare.com/workers/wrangler/configuration/)
56+
57+
<StaticSsrTabs>
58+
<Fragment slot="static">
59+
```jsonc
60+
// wrangler.jsonc
61+
{
62+
"$schema": "node_modules/wrangler/config-schema.json",
63+
"name": "my-astro-app",
64+
// 更新为今天的日期
65+
"compatibility_date": "2025-03-25",
66+
"assets": {
67+
"directory": "./dist"
68+
}
69+
}
70+
```
71+
</Fragment>
72+
<Fragment slot="ssr">
73+
```jsonc
74+
// wrangler.jsonc
75+
{
76+
"$schema": "node_modules/wrangler/config-schema.json",
77+
"name": "my-astro-app",
78+
"main": "./dist/_worker.js/index.js",
79+
// 更新为今天的日期
80+
"compatibility_date": "2025-03-25",
81+
"compatibility_flags": ["nodejs_compat"],
82+
"assets": {
83+
"binding": "ASSETS",
84+
"directory": "./dist"
85+
},
86+
"observability": {
87+
"enabled": true
88+
}
89+
}
90+
```
91+
</Fragment>
92+
</StaticSsrTabs>
5293

53-
```bash
54-
# 安装 Wrangler CLI(命令行)
55-
npm install -g wrangler
56-
# 在 CLI 中登录 Cloudflare 账号
57-
wrangler login
58-
# 运行你的构建命令
59-
npm run build
60-
# 创建新的部署
61-
npx wrangler pages deploy dist
62-
```
94+
4. 使用 Wrangler 对你的项目进行本地预览。
6395

64-
上传完所有的文件后,Wrangler 将为你提供一个预览 URL 以检查你的站点。当你登录 Cloudflare Pages 仪表板时,你将看到你的新项目。
96+
```bash
97+
npx astro build && npx wrangler dev
98+
```
6599

66-
### 使用 Wrangler 在本地启用预览
100+
5. 使用 `npx wrangler deploy` 命令进行部署。
67101

68-
要使预览版正常工作,你必须安装 `wrangler`
102+
```bash
103+
npx astro build && npx wrangler deploy
104+
```
105+
</Steps>
69106

70-
```bash
71-
pnpm add wrangler --save-dev
72-
```
107+
在你的资源上传之后,Wrangler 将会提供一个预览 URL 用以让你检视自己的网站。
73108

74-
然后就可以将预览脚本从 Astro 的内置预览命令更新为 `wrangler` 了:
109+
<ReadMore>阅读更多有关使用 [Cloudflare 运行时 API](/zh-cn/guides/integrations-guide/cloudflare/) 的内容,例如绑定。</ReadMore>
75110

76-
```json title="package.json"
77-
"preview": "wrangler pages dev ./dist"
78-
```
111+
### 如何使用 CI/CD 进行部署
79112

80-
## 如何部署 SSR 站点
113+
你可以使用一个 CI/CD 系统,例如 [Workers Builds (BETA)](https://developers.cloudflare.com/workers/ci-cd/builds/) 来通过推送,自动化构建并部署你的网站。
81114

82-
你可以使用 [`@astrojs/cloudflare` 适配器](/zh-cn/guides/integrations-guide/cloudflare/)将 Astro SSR 站点部署到 Cloudflare Pages。
115+
假设你正在使用 Workers Builds:
83116

84-
请按照以下步骤设置适配器。完成后,你可以使用上述文档中的任何方法进行部署。
117+
<Steps>
118+
1. 与上文中如何使用 Wrangler 进行部署的步骤 1-3 相同。
85119

86-
### 快速安装
120+
2. 登录至 [Cloudflare dashboard](https://dash.cloudflare.com/) 并导航至 `Workers & Pages` 页面。选择 `Create`(创建)。
87121

88-
使用以下 `astro add` 命令添加 Cloudflare 适配器以在你的 Astro 项目中启用 SSR。这将安装适配器并一步对你的文件 `astro.config.mjs` 进行适当的更改
122+
3.`Import a repository`(导入仓库)选项下,选择一个 Git 账号,然后选择包含你 Astro 项目的仓库
89123

90-
```bash
91-
npx astro add cloudflare
92-
```
124+
4. 配置项目:
125+
- Build command(构建命令):`npx astro build`
126+
- Deploy command(部署命令):`npx wrangler deploy`
127+
128+
5. 点击 `Save and Deploy`(保存并部署)。你现在可以在它提供的 `workers.dev` 子域名下,预览 Worker 部署的结果了。
129+
</Steps>
93130

94-
### 手动安装
131+
## Cloudflare Pages
95132

96-
如果你想要手动安装适配器,请完成以下两个步骤:
133+
### 如何使用 Wrangler 进行部署
97134

98135
<Steps>
99-
1. 使用你喜欢的包管理器将 `@astrojs/cloudflare` 添加到项目的依赖项中。如果你正在使用 npm 或不确定是哪个包管理器,请在终端中运行:
136+
1. 安装 [Wrangler CLI](https://developers.cloudflare.com/workers/wrangler/get-started/)
100137

101138
```bash
102-
npm install @astrojs/cloudflare
139+
npm install wrangler@latest --save-dev
103140
```
104141

105-
2. 将以下内容添加到你的 `astro.config.mjs` 文件中:
142+
2. 如果你的站点使用了按需渲染,安装 [`@astrojs/cloudflare` 适配器](/zh-cn/guides/integrations-guide/cloudflare/)
143+
144+
该步骤将会安装适配器,并对你的 `astro.config.mjs` 文件进行适当的调整。
145+
146+
```bash
147+
npx astro add cloudflare
148+
```
149+
150+
<ReadMore>阅读更多有关 [Astro 中的按需渲染](/zh-cn/guides/on-demand-rendering/) 的内容。</ReadMore>
106151

107-
```js title="astro.config.mjs" ins={2, 5-6}
108-
import { defineConfig } from 'astro/config';
109-
import cloudflare from '@astrojs/cloudflare';
152+
3. 使用 Wrangler 对你的项目进行本地预览。
110153

111-
export default defineConfig({
112-
output: 'server',
113-
adapter: cloudflare()
114-
});
154+
```bash
155+
npx astro build && npx wrangler pages dev ./dist
156+
```
157+
158+
4. 使用 `npx wrangler deploy` 命令进行部署。
159+
160+
```bash
161+
npx astro build && npx wrangler pages deploy ./dist
115162
```
116163
</Steps>
117164

118-
<ReadMore>阅读更多关于 [Astro 中的 SSR(服务端渲染)](/zh-cn/guides/on-demand-rendering/) 的信息。</ReadMore>
165+
在你的资源上传之后,Wrangler 将会提供一个预览 URL 用以让你检视自己的网站。
166+
167+
### 如何使用 Git 来部署站点
168+
169+
<Steps>
170+
1. 推送代码至 Git 仓库(例如:GitHub、GitLab)。
171+
172+
2. 登录至 [Cloudflare dashboard](https://dash.cloudflare.com/) 并导航至 `Workers & Pages`。选择 `Create`(创建)并选择 `Pages`。连接至你的 Git 仓库。
173+
174+
3. 配置项目:
175+
- **Framework preset(框架预设)**`Astro`
176+
- **Build command(构建命令)**`npm run build`
177+
- **Build output directory(构建输出目录)**`dist`
178+
179+
4. 点击 **Save and Deploy(保存并部署)** 按钮。
180+
</Steps>
119181

120182
## 故障排除
121183

@@ -125,7 +187,7 @@ npx astro add cloudflare
125187

126188
### Node.js 运行时 API
127189

128-
如果你正在构建一个使用 [Cloudflare 服务端渲染适配器](/zh-cn/guides/integrations-guide/cloudflare/) 的按需渲染项目,并且服务器在构建时失败,出现如 `[Error] Could not resolve "XXXX. The package "XXXX" wasn't found on the file system but is built into node.` 的错误信息:
190+
如果你正在构建一个使用 [Cloudflare 适配器](/zh-cn/guides/integrations-guide/cloudflare/) 的按需渲染项目,并且服务器在构建时失败,出现如 `[Error] Could not resolve "XXXX. The package "XXXX" wasn't found on the file system but is built into node.` 的错误信息:
129191

130192
- 这意味着你在服务器端环境中使用的某个包或导入项与 [Cloudflare 运行时 API](https://developers.cloudflare.com/workers/runtime-apis/nodejs/) 不兼容。
131193

0 commit comments

Comments
 (0)