Skip to content

Commit fc9e738

Browse files
authored
docs: sync zh-hans from the latest English version (analogjs#2066)
1 parent 784f03f commit fc9e738

12 files changed

Lines changed: 1175 additions & 202 deletions

File tree

‎apps/docs-app/i18n/zh-hans/docusaurus-plugin-content-docs/current/features/deployment/overview.md‎

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,31 @@ export default defineConfig({
5151

5252
## 部署时自定义 URL 前缀
5353

54-
如果你部署是需要指定一个自定义的 URL 前缀,例如 https://domain.com/ `basehref`/ 你必须确保 [服务端数据获取](https://analogjs.org/docs/features/data-fetching/server-side-data-fetching),[HTML 标记和资源](https://angular.io/api/common/APP_BASE_HREF) 和 [动态 API 路由](https://analogjs.org/docs/features/api/overview) 能在 `basehref` 下正常工作。
54+
如果你部署时需要指定一个自定义的 URL 前缀,例如 https://domain.com/ `basehref`/ 你必须确保 [服务端数据获取](https://analogjs.org/docs/features/data-fetching/server-side-data-fetching),[HTML 标记和资源](https://angular.io/api/common/APP_BASE_HREF) 和 [动态 API 路由](https://analogjs.org/docs/features/api/overview) 能在 `basehref` 下正常工作。
5555

56-
1. 更新文件 `app.config.env.ts`。
56+
1. 更新你的 `vite.config.ts` 文件。
5757

58-
这里告知 Angular 如何识别并生成对应的 URL
58+
```ts
59+
export default defineConfig(({ mode }) => ({
60+
base: '/basehref',
61+
plugins: [
62+
analog({
63+
...(mode === 'production'
64+
? { apiPrefix: 'basehref' }
65+
: { apiPrefix: 'basehref/api' }),
66+
prerender: {
67+
routes: async () => {
68+
return [];
69+
},
70+
},
71+
}),
72+
],
73+
}));
74+
```
75+
76+
2. 更新 `app.config.ts` 文件。
77+
78+
这里告知 Angular 如何识别并生成对应的 URL。
5979

6080
```ts
6181
import { ApplicationConfig } from '@angular/core';
@@ -69,42 +89,34 @@ export const appConfig: ApplicationConfig = {
6989
};
7090
```
7191

72-
2. 在 CI 生产环境构建
92+
3. HttpClient 使用 `injectAPIPrefix()`。
93+
94+
```ts
95+
const response = await firstValueFrom(
96+
this.httpClient.get<{ client: string }>(`${injectAPIPrefix()}/v1/hello`),
97+
);
98+
```
99+
100+
4. 在 CI 生产环境构建
101+
102+
**不要**设置 `VITE_ANALOG_PUBLIC_BASE_URL`,它应该使用 `vite.config.ts` 中的设置。
103+
如果构建期间存在 `VITE_ANALOG_PUBLIC_BASE_URL`,ssr 数据将在服务器和客户端上重新获取。
73104

74105
```bash
75-
# sets the base url for server-side data fetching
76-
export VITE_ANALOG_PUBLIC_BASE_URL="https://domain.com/basehref"
77-
# Prefixes all assets and html with /basehref/
78106
# if using nx:
79-
npx nx run appname:build:production --baseHref='/basehref/'
107+
npx nx run appname:build:production
80108
# if using angular build directly:
81-
npx ng build --baseHref="/basehref/"
109+
npx vite build && NITRO_APP_BASE_URL='/basehref/' node dist/analog/server/index.mjs
82110
```
83111

84-
3. 在生产环境的镜像指定环境变量 `NITRO_APP_BASE_URL`。
112+
5. 在生产环境的镜像指定环境变量 `NITRO_APP_BASE_URL`。
85113

86114
```bash
87115
NITRO_APP_BASE_URL="/basehref/"
88116
```
89117

90-
`vite.config.ts` 文件类似的配置:
118+
6. 本地预览:
91119

92-
```ts
93-
plugins: [
94-
analog({
95-
apiPrefix: 'api',
96-
nitro: {
97-
routeRules: {
98-
'/': {
99-
prerender: false,
100-
},
101-
},
102-
},
103-
prerender: {
104-
routes: async () => {
105-
return ['/'];
106-
}
107-
}
120+
```bash
121+
npx vite build && NITRO_APP_BASE_URL='/basehref/' node dist/analog/server/index.mjs
108122
```
109-
110-
Nitro 所有的 API 路由将基于 `/basehref/api` 。

‎apps/docs-app/i18n/zh-hans/docusaurus-plugin-content-docs/current/features/routing/middleware.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,25 @@ export default defineEventHandler(async (event) => {
5050
}
5151
});
5252
```
53+
54+
## 访问环境变量
55+
56+
使用 `process.env` 全局变量在中间件函数中访问环境变量。在 `.env` 文件中定义的仅服务器和可公开访问的环境变量都可以从中读取。
57+
58+
```ts
59+
import { defineEventHandler, getRequestURL } from 'h3';
60+
61+
export default defineEventHandler((event) => {
62+
console.log('Path:', getRequestURL(event).pathname);
63+
console.log(
64+
'Server Only Environment Variable:',
65+
process.env['SERVER_ONLY_VARIABLE'],
66+
);
67+
console.log(
68+
'Public Environment Variable:',
69+
process.env['VITE_EXAMPLE_VARIABLE'],
70+
);
71+
});
72+
```
73+
74+
在 Vite 文档中了解更多关于 [环境变量](https://vite.dev/guide/env-and-mode.html#env-variables) 的信息。

‎apps/docs-app/i18n/zh-hans/docusaurus-plugin-content-docs/current/features/routing/overview.md‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,85 @@ src/
337337
| `/products/1` | `products/[productId].page.ts` (layout: `products.page.ts`) |
338338
| `/products/1/edit` | `products/[productId].edit.page.ts` (layout: `products.page.ts`) |
339339
| `/unknown-url` | `[...not-found].md` |
340+
341+
## 提供额外路由
342+
343+
除了通过文件系统发现的路由外,还可以手动添加路由。使用带有路由数组的 `withExtraRoutes`,将其预置到发现的路由数组中。所有路由将合并到一个数组中。
344+
345+
```ts
346+
import { ApplicationConfig } from '@angular/core';
347+
import { Routes } from '@angular/router';
348+
import { provideFileRouter, withExtraRoutes } from '@analogjs/router';
349+
350+
const customRoutes: Routes = [
351+
{
352+
path: 'custom',
353+
loadComponent: () =>
354+
import('./custom-component').then((m) => m.CustomComponent),
355+
},
356+
];
357+
358+
export const appConfig: ApplicationConfig = {
359+
providers: [provideFileRouter(withExtraRoutes(customRoutes))],
360+
};
361+
```
362+
363+
## 可视化和调试路由
364+
365+
在构建应用程序页面时,可视化查看基于文件系统结构的路由会很有帮助。你可以使用 `withDebugRoutes()` 函数提供一个调试路由,显示应用程序的页面和布局。
366+
367+
在 `app.config.ts` 中使用 `withDebugRoutes` 函数:
368+
369+
```ts
370+
import { ApplicationConfig } from '@angular/core';
371+
import { provideFileRouter, withDebugRoutes } from '@analogjs/router';
372+
373+
export const appConfig: ApplicationConfig = {
374+
providers: [provideFileRouter(withDebugRoutes())],
375+
};
376+
```
377+
378+
在浏览器中导航到 `__analog/routes` URL 以查看路由表。
379+
380+
![debug routes page](/img/debug-routes.png)
381+
382+
## 提供额外路由
383+
384+
除了通过文件系统发现的路由外,还可以手动添加路由。使用带有路由数组的 `withExtraRoutes`,将其预置到发现的路由数组中。所有路由将合并到一个数组中。
385+
386+
```ts
387+
import { ApplicationConfig } from '@angular/core';
388+
import { Routes } from '@angular/router';
389+
import { provideFileRouter, withExtraRoutes } from '@analogjs/router';
390+
391+
const customRoutes: Routes = [
392+
{
393+
path: 'custom',
394+
loadComponent: () =>
395+
import('./custom-component').then((m) => m.CustomComponent),
396+
},
397+
];
398+
399+
export const appConfig: ApplicationConfig = {
400+
providers: [provideFileRouter(withExtraRoutes(customRoutes))],
401+
};
402+
```
403+
404+
## 可视化和调试路由
405+
406+
在构建应用程序页面时,可视化查看基于文件系统结构的路由会很有帮助。你可以使用 `withDebugRoutes()` 函数提供一个调试路由,显示应用程序的页面和布局。
407+
408+
在 `app.config.ts` 中使用 `withDebugRoutes` 函数:
409+
410+
```ts
411+
import { ApplicationConfig } from '@angular/core';
412+
import { provideFileRouter, withDebugRoutes } from '@analogjs/router';
413+
414+
export const appConfig: ApplicationConfig = {
415+
providers: [provideFileRouter(withDebugRoutes())],
416+
};
417+
```
418+
419+
在浏览器中导航到 `__analog/routes` URL 以查看路由表。
420+
421+
![debug routes page](/img/debug-routes.png)

‎apps/docs-app/i18n/zh-hans/docusaurus-plugin-content-docs/current/features/server/server-side-rendering.md‎

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,46 @@ import analog from '@analogjs/platform';
1616
export default defineConfig(({ mode }) => ({
1717
ssr: {
1818
noExternal: [
19-
'apollo-angular', // npm package import
20-
'apollo-angular/**', // npm package import along with sub-packages
21-
'@spartan-ng/**', // libs under the npmScope inside an Nx workspace
19+
'apollo-angular', // npm 包导入
20+
'apollo-angular/**', // npm 包及其子包导入
21+
'@spartan-ng/**', // Nx 工作空间下 npmScope 的库
2222
],
2323
},
24-
// ...other config
24+
// ...其他配置
2525
}));
2626
```
2727

28-
要了解更多关于 SSR 的外部依赖,请移步[Vite 文档](https://vitejs.dev/guide/ssr.html#ssr-externals)。
28+
关于 SSR 的外部依赖项更多信息,请参阅 [Vite 文档](https://vitejs.dev/guide/ssr.html#ssr-externals)。
29+
30+
## 混合渲染与仅客户端路由
31+
32+
SSR 默认是启用的。对于混合方法,你可以指定一些路由仅在客户端渲染,而不进行服务端渲染。这是通过 `routeRules` 配置对象指定 `ssr` 选项来完成的。
33+
34+
```ts
35+
import { defineConfig } from 'vite';
36+
import analog from '@analogjs/platform';
37+
38+
// https://vitejs.dev/config/
39+
export default defineConfig(({ mode }) => ({
40+
// ...其他配置
41+
plugins: [
42+
analog({
43+
prerender: {
44+
routes: ['/', '/404.html'],
45+
},
46+
nitro: {
47+
routeRules: {
48+
// 所有 admin URL 仅在客户端渲染
49+
'/admin/**': { ssr: false },
50+
51+
// 渲染 404 页面作为后备页面
52+
'/404.html': { ssr: false },
53+
},
54+
},
55+
}),
56+
],
57+
}));
58+
```
2959

3060
## 禁用 SSR
3161

@@ -37,8 +67,15 @@ import analog from '@analogjs/platform';
3767

3868
// https://vitejs.dev/config/
3969
export default defineConfig(({ mode }) => ({
40-
// ...other config
41-
plugins: [analog({ ssr: false })],
70+
// ...其他配置
71+
plugins: [
72+
analog({
73+
ssr: false,
74+
prerender: {
75+
routes: [],
76+
},
77+
}),
78+
],
4279
}));
4380
```
4481

@@ -53,7 +90,7 @@ import { defineConfig } from 'vite';
5390
import analog from '@analogjs/platform';
5491
// https://vitejs.dev/config/
5592
export default defineConfig(({ mode }) => ({
56-
// ...other config
93+
// ...其他配置
5794
plugins: [
5895
analog({
5996
prerender: {
@@ -71,17 +108,10 @@ import { defineConfig } from 'vite';
71108
import analog from '@analogjs/platform';
72109
// https://vitejs.dev/config/
73110
export default defineConfig(({ mode }) => ({
74-
// ...other config
111+
// ...其他配置
75112
plugins: [
76113
analog({
77114
ssr: true,
78-
nitro: {
79-
routeRules: {
80-
'/': {
81-
prerender: false,
82-
},
83-
},
84-
},
85115
prerender: {
86116
routes: async () => {
87117
return [];

0 commit comments

Comments
 (0)