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 middleware.mdx #7295

Merged
merged 12 commits into from
Mar 13, 2024
17 changes: 7 additions & 10 deletions src/content/docs/zh-cn/guides/middleware.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,9 @@ i18nReady: true
---
import PackageManagerTabs from '~/components/tabs/PackageManagerTabs.astro';

**中间件**允许你拦截请求和响应,并在即将渲染页面或端点时动态注入行为。
**中间件**允许你拦截请求和响应,并在即将渲染页面或端点时动态注入行为。对于所有预渲染的页面,这种渲染发生在构建时,但对于按需渲染的页面,这种渲染发生在请求路由时。

这也允许你通过修改在所有 Astro 组件和 API 端点中可用的 locals 对象,设置和共享跨端点和页面的请求特定信息。

在 Astro 项目中,无论是 SSG 还是 SSR,都可以使用中间件。

:::note
在所有预渲染的页面上,中间件仅在**构建过程**中运行以创建静态页面,并不会在部署的网站上运行。这适用于 `static`(SSG)项目中的所有页面。这也包括在 `hybrid` 模式(默认)中的预渲染页面和在 `server` 模式下设置了 `export const prerender = true` 的任何页面。

对于在 `server`(默认)或 `hybrid`(设置了 `export const prerender = false`)模式下的按需渲染页面,当请求路由时,中间件会在部署的网站上运行。
:::
这也允许你通过修改在所有 Astro 组件和 API 端点中可用的 `locals` 对象,设置和共享跨端点和页面的请求特定信息。即使在构建时运行这个中间件时,这个对象也是可用的。

## 基本用法

Expand Down Expand Up @@ -199,3 +191,8 @@ export const onRequest = sequence(validation, auth, greeting);
授权响应
验证响应
```
## 错误页面

即使找不到匹配的路由,中间件也会尝试为所有按需渲染的页面运行。这包括 Astro 的默认(空白)404 页面和任何自定义 404 页面。然而,是否运行该代码取决于[适配器](/zh-cn/guides/server-side-rendering/)。一些适配器可能会提供特定平台的错误页面。

在提供 500 错误页面之前,中间件也会尝试运行,包括自定义的 500 页面,除非服务器错误发生在中间件本身的执行中。如果你的中间件没有成功运行,那么你将无法访问 `Astro.locals` 来渲染你的 500 页面。
Loading