Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i18n(zh-cn): Update netlify.mdx #11016

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 91 additions & 11 deletions src/content/docs/zh-cn/guides/integrations-guide/netlify.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ category: adapter
i18nReady: true
---
import PackageManagerTabs from '~/components/tabs/PackageManagerTabs.astro'
import Since from '~/components/Since.astro';

此适配器可以部署你的 [按需渲染路由](/zh-cn/guides/on-demand-rendering/) 到 [Netlify](https://www.netlify.com/)。

如果你正在使用 Astro 作为 静态站点生成器,则不需要适配器。
如果你正在使用 Astro 作为静态站点生成器,则不需要适配器。

在我们的 [Netlify 部署指南](/zh-cn/guides/deploy/netlify/) 中学习如何部署你的 Astro 网站。

Expand All @@ -22,7 +23,7 @@ import PackageManagerTabs from '~/components/tabs/PackageManagerTabs.astro'

## 安装

Astro 包含了一个 `astro add` 命令,用于自动设置官方集成。如果你愿意,可以改为[手动安装集成](#手动安装)。
Astro 包含了一个 `astro add` 命令,用于自动设置官方集成。如果你愿意,可以改为 [手动安装集成](#手动安装)。

在 Astro 项目中使用以下 `asrto add` 命令添加 Netlify 适配器,以启用 SSR。这将安装 `@astrojs/netlify` 并一步到位地对你的 `astro.config.mjs` 文件进行相应的更改。

Expand Down Expand Up @@ -66,7 +67,7 @@ Astro 包含了一个 `astro add` 命令,用于自动设置官方集成。如
</Fragment>
</PackageManagerTabs>

然后,将适配器和你所需的[按需渲染模式](/zh-cn/guides/on-demand-rendering/)添加到你的 `astro.config.*` 文件中:
然后,将适配器和你所需的 [按需渲染模式](/zh-cn/guides/on-demand-rendering/) 添加到你的 `astro.config.*` 文件中:

```js title="astro.config.mjs" ins={2, 6-7}
import { defineConfig } from 'astro/config';
Expand Down Expand Up @@ -142,14 +143,13 @@ export default defineConfig({
});
```

当 `edgeMiddleware` 启用时,边缘函数将为所有请求执行你的中间件代码,包括静态资产、预渲染页面和按需渲染页面。
当 `edgeMiddleware` 启用时,边缘函数将为所有请求执行你的中间件代码,包括静态资源、预渲染页面和按需渲染页面。

对于按需渲染的页面,`context.locals` 对象会被 JSON 序列化并通过标头发送给无服务器函数,由该函数执行渲染。作为安全措施,除非请求来自生成的边缘函数,否则无服务器函数将拒绝服务,并返回 `403 Forbidden` 响应。

### Netlify 图片 CDN 支持

这个适配器默认使用 [Netlify 图片 CDN](https://docs.netlify.com/image-cdn/overview/) 来实时转换图片,因而不会影响构建时间。
它背后是使用 [Astro 图片服务](/zh-cn/reference/image-service-reference/) 实现的。
这个适配器默认使用 [Netlify 图片 CDN](https://docs.netlify.com/image-cdn/overview/) 来实时转换图片,因而不会影响构建时间。它背后是使用 [Astro 图片服务](/zh-cn/reference/image-service-reference/) 实现的。

如果你想退出 Netlify 的图片 CDN 远程图片优化,可以使用 `imageCDN` 选项:

Expand Down Expand Up @@ -180,7 +180,7 @@ export default defineConfig({
},
});
```
要了解更多信息,请参阅[授权远程图片的指南](/zh-cn/guides/images/#授权远程图像)。如果图片托管在与你的站点相同的域名上,这不是必需的。
要了解更多信息,请参阅 [授权远程图片的指南](/zh-cn/guides/images/#授权远程图像)。如果图片托管在与你的站点相同的域名上,这不是必需的。

### 使用 Netlify 适配器的静态站点

Expand Down Expand Up @@ -211,8 +211,7 @@ export default defineConfig({

### 缓存页面

可以缓存没有任何动态内容的按需渲染页面,以提高性能并降低资源使用率。
在适配器中启用 `cacheOnDemandPages` 选项将会缓存所有服务器渲染的页面,最长可达一年:
可以缓存没有任何动态内容的按需渲染页面,以提高性能并降低资源使用率。在适配器中启用 `cacheOnDemandPages` 选项将会缓存所有服务器渲染的页面,最长可达一年:

```ts title="astro.config.mjs"
export default defineConfig({
Expand All @@ -237,8 +236,89 @@ Astro.response.headers.set('CDN-Cache-Control', 'public, max-age=45, must-revali
</Layout>
```

使用[细粒度缓存控制](https://www.netlify.com/blog/swr-and-fine-grained-cache-control/),Netlify 支持标准缓存头,例如 `CDN-Cache-Control` 或 `Vary`。
请参阅文档,了解如何实现例如 TTL 或 SWR 缓存:https://docs.netlify.com/platform/caching
使用 [细粒度缓存控制](https://www.netlify.com/blog/swr-and-fine-grained-cache-control/),Netlify 支持标准缓存头,例如 `CDN-Cache-Control` 或 `Vary`。请参阅文档,了解如何实现例如 TTL 或 SWR 缓存:https://docs.netlify.com/platform/caching

### 包括或排除 Netlify Functions 中的文件

当使用 Netlify 部署按需渲染的 Astro 网站时,生成功能会自动跟踪并包含服务器依赖。然而,你可能需要自定义哪些文件应该被包含在你的 Netlify Funcitons 中。

#### `includeFiles`

<p>
**类型:** `string[]`<br />
**默认值:** `[]`<br />
<Since v="5.3.0" />
</p>

`includeFiles` 属性允许你明确指定哪些附加的文件是需要和功能一同打包的。该属性对于那些未自动检测到依赖项的文件很有用,例如:
- 使用 `fs` 模块执行加载的数据文件
- 配置文件
- 模板文件

请提供一个数组,其中包含需附加的文件路径,路径需要是相对于项目 [`root`](/zh-cn/reference/configuration-reference/#root) 的相对路径。绝对路径可能会无法正常工作。

```js title="astro.config.mjs" ins={8}
import { defineConfig } from 'astro/config';
import netlify from '@astrojs/netlify';

export default defineConfig({
// ...
output: 'server',
adapter: netlify({
includeFiles: ['./my-data.json'], // 相对于 `root`
}),
});
```

#### `excludeFiles`

<p>
**类型:** `string[]`<br />
**默认值:** `[]`<br />
<Since v="5.3.0" />
</p>

你可以使用 `excludeFiles` 属性来避免特定的文件被一同打包,否则其将会被包含在内。该属性有利于:
- 减小打包产物的体积
- 排除大型的二进制文件
- 避免不必要的文件被部署

请提供一个数组,其中包含需要排除的文件路径,路径需要是相对于项目 [`root`](/zh-cn/reference/configuration-reference/#root) 的相对路径。绝对路径可能会无法正常工作。

```js title="astro.config.mjs" ins={8}
import { defineConfig } from 'astro/config';
import netlify from '@astrojs/netlify';

export default defineConfig({
// ...
output: 'server',
adapter: netlify({
excludeFiles: ['./src/some_big_file.jpg'], // 相对于 `root`
}),
});
```

#### 使用 glob 模式

不论是 `includeFiles` 还是 `excludeFiles` 都支持使用 [glob 模式](/zh-cn/guides/imports/#glob-模式) 来匹配多项文件:

```js title="astro.config.mjs" ins={8, 11-12}
import { defineConfig } from 'astro/config';
import netlify from '@astrojs/netlify';

export default defineConfig({
output: 'server',
adapter: netlify({
includeFiles: [
'./data/**/*.json'
],
excludeFiles: [
'./node_modules/package/**/*',
'./src/**/*.test.js'
]
}),
});
```

## 示例

Expand Down