+
Skip to content

docs: update zh-hans docs and fix some document issues #1649

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

Merged
merged 2 commits into from
Mar 16, 2025
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions apps/docs-app/docs/features/deployment/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const appConfig: ApplicationConfig = {
};
```

3. In CI production build
2. In CI production build

```bash
# sets the base url for server-side data fetching
Expand All @@ -81,7 +81,7 @@ export const appConfig: ApplicationConfig = {
npx ng build --baseHref="/basehref/"
```

4. In production containers specify the env flag `NITRO_APP_BASE_URL`.
3. In production containers specify the env flag `NITRO_APP_BASE_URL`.

```bash
NITRO_APP_BASE_URL="/basehref/"
Expand Down
2 changes: 1 addition & 1 deletion apps/docs-app/i18n/de/docusaurus-theme-classic/footer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"message": "Beitragen",
"description": "The label of footer link with label=Contributing linking to docs/contributing"
},
"link.item.label.Sponsor": {
"link.item.label.Sponsoring": {
"message": "Unterstützen",
"description": "The label of footer link with label=Sponsor linking to docs/sponsoring"
},
Expand Down
2 changes: 1 addition & 1 deletion apps/docs-app/i18n/es/docusaurus-theme-classic/footer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"message": "Contribuyendo",
"description": "The label of footer link with label=Contributing linking to docs/contributing"
},
"link.item.label.Sponsor": {
"link.item.label.Sponsoring": {
"message": "Patrocinio",
"description": "The label of footer link with label=Sponsor linking to docs/sponsoring"
},
Expand Down
2 changes: 1 addition & 1 deletion apps/docs-app/i18n/fr/docusaurus-theme-classic/footer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"message": "Contribuer",
"description": "The label of footer link with label=Contributing linking to docs/contributing"
},
"link.item.label.Sponsor": {
"link.item.label.Sponsoring": {
"message": "Parrainage",
"description": "The label of footer link with label=Sponsor linking to docs/sponsoring"
},
Expand Down
2 changes: 1 addition & 1 deletion apps/docs-app/i18n/ko/docusaurus-theme-classic/footer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"message": "기여하기",
"description": "The label of footer link with label=Contributing linking to docs/contributing"
},
"link.item.label.Sponsor": {
"link.item.label.Sponsoring": {
"message": "후원",
"description": "The label of footer link with label=Sponsor linking to docs/sponsoring"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"message": "Contribuindo",
"description": "The label of footer link with label=Contributing linking to docs/contributing"
},
"link.item.label.Sponsor": {
"link.item.label.Sponsoring": {
"message": "Patrocinando",
"description": "The label of footer link with label=Sponsor linking to docs/sponsoring"
},
Expand Down
2 changes: 1 addition & 1 deletion apps/docs-app/i18n/tr/docusaurus-theme-classic/footer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"message": "Katkıda bulunma",
"description": "The label of footer link with label=Contributing linking to docs/contributing"
},
"link.item.label.Sponsor": {
"link.item.label.Sponsoring": {
"message": "Sponsorluk",
"description": "The label of footer link with label=Sponsor linking to docs/sponsoring"
},
Expand Down
8 changes: 8 additions & 0 deletions apps/docs-app/i18n/zh-hans/code.json
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,14 @@
"message": "Analog 免费,开源并由我们的支持者赞助",
"description": "The title of the sponsor section"
},
"homepage.partnerSection.zerops": {
"message": "Zerops - 官方部署合作伙伴",
"description": "The name of the partner Zerops"
},
"homepage.partnerSection.subtitle": {
"message": "合作伙伴",
"description": "The subtitle of the partner section on the homepage"
},
"homepage.sponsorSection.subtitle": {
"message": "赞助商",
"description": "The subtitle of the sponsor section on the homepage"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
"message": "服务端渲染(SSR)",
"description": "The label for category Server Side Rendering in sidebar docsSidebar"
},
"sidebar.docsSidebar.doc.Form Actions": {
"message": "表单行为",
"description": "The label for category Form Actions in sidebar docsSidebar"
},
"sidebar.docsSidebar.category.Code Generation": {
"message": "代码生成",
"description": "The label for category Code Generation in sidebar docsSidebar"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# WebSocket

Analog 还通过 Nitro 支持 `WebSockets` 和 `Server-Sent Events`。

## 启用 WebSockets

目前,[Nitro](https://nitro.unjs.io/guide/websocket) 中的 WebSocket 支持是实验性的,可以在 `analog` 插件中启用:

`vite.config.ts`

```typescript
import { defineConfig } from 'vite';
import analog from '@analogjs/platform';

export default defineConfig({
// ...
plugins: [
analog({
// ...
nitro: {
experimental: {
websocket: true,
},
},
}),
],
// ...
});
```

**注意:** 在开发过程中,Vite HMR WebSocket 服务器默认运行在与开发服务器相同的端口上。为了防止冲突,需要更改此端口。开发服务器端口通常在 `project.json`/`angular.json` 中定义,优先于 `vite.config.ts`。为了使 `vite.config.ts` 中的端口设置生效,请从 `project.json`/`angular.json` 中删除端口定义。此外,可以指定一个可选路径,以便在浏览器开发工具中轻松区分连接:

`vite.config.ts`

```typescript
import { defineConfig } from 'vite';
import analog from '@analogjs/platform';

export default defineConfig({
// ...
server: {
port: 3000, // 开发服务器端口
hmr: {
port: 3002, // hmr ws 端口
path: 'vite-hmr', // 可选
},
},
// ...
});
```

## 定义 WebSocket 处理程序

类似于 [API 路由](/docs/features/api/overview),WebSocket 处理程序定义在 `src/server/routes/api` 文件夹中。

```typescript
// src/server/api/routes/ws/chat.ts
import { defineWebSocketHandler } from 'h3';

export default defineWebSocketHandler({
open(peer) {
peer.send({ user: 'server', message: `Welcome ${peer}!` });
peer.publish('chat', { user: 'server', message: `${peer} joined!` });
peer.subscribe('chat');
},
message(peer, message) {
if (message.text().includes('ping')) {
peer.send({ user: 'server', message: 'pong' });
} else {
const msg = {
user: peer.toString(),
message: message.toString(),
};
peer.send(msg); // echo
peer.publish('chat', msg);
}
},
close(peer) {
peer.publish('chat', { user: 'server', message: `${peer} left!` });
},
});
```

### WebSocket 路由

WebSocket 路由与 API 路由的路径相同。例如,`src/server/routes/api/ws/chat` 暴露为 `ws://example.com/api/ws/chat`。

## 定义 Server-sent Event 处理程序

可以使用事件处理程序中的 `createEventStream` 函数创建 Server-sent 事件处理程序。

```typescript
// src/server/routes/api/sse.ts
import { defineEventHandler, createEventStream } from 'h3';

export default defineEventHandler(async (event) => {
const eventStream = createEventStream(event);

const interval = setInterval(async () => {
await eventStream.push(`Message @ ${new Date().toLocaleTimeString()}`);
}, 1000);

eventStream.onClosed(async () => {
clearInterval(interval);
await eventStream.close();
});

return eventStream.send();
});
```

## 更多信息

WebSockets 由 [Nitro](https://nitro.unjs.io/guide/websocket)、[h3](https://h3.unjs.io/guide/websocket) 和 [crossws](https://crossws.unjs.io/guide) 提供支持。有关更多详细信息,请参阅 Nitro、h3 和 crossws 文档。 WebSocket
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export const load = async ({

通过 `@analogjs/router` 包提供的 `injectLoad` 函数可以访问从服务端获取的数据。

`load` 函数是通过 Angular 的路由解析器来解析的,所以 `requireSync: false` 设置 和 `initialValue: {}` 不会带来任何好处,因为 load 是在组件实例化之前被调用的。

```ts
// src/app/pages/index.page.ts
import { Component } from '@angular/core';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import TabItem from '@theme/TabItem';

Node.js 部署是 Analog 为生产环境构建的默认预设输出。

在默认预设情况下运行 `npm run build` 时,编译结果就是一个 ready-to-run 的 Node 服务器器入口
在默认预设情况下运行 `npm run build` 时,编译结果就是一个 ready-to-run 的 Node 服务

要单独启动这个服务,可以运行:

Expand Down Expand Up @@ -51,20 +51,25 @@ export default defineConfig({

## 部署时自定义 URL 前缀

如果你部署是需要指定一个自定义的 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` 下正常工作。
如果你部署是需要指定一个自定义的 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` 下正常工作。

1. 指示 Angular 识别并生成对应的 URL。创建一个新的文件 `app.config.env.ts`。
1. 更新文件 `app.config.env.ts`。

这里告知 Angular 如何识别并生成对应的 URL

```ts
import { ApplicationConfig } from '@angular/core';
import { APP_BASE_HREF } from '@angular/common';

export const envConfig: ApplicationConfig = {
providers: [{ provide: APP_BASE_HREF, useValue: '/basehref/' }],
export const appConfig: ApplicationConfig = {
providers: [
[{ provide: APP_BASE_HREF, useValue: import.meta.env.BASE_URL || '/' }],
...
],
};
```

2. 更新 `app.config.ts` 去导入这个新的文件
1. 更新 `app.config.ts` 去导入这个新的文件

```ts
import { mergeApplicationConfig } from '@angular/core';
Expand All @@ -75,16 +80,19 @@ export const appConfig = mergeApplicationConfig(envConfig, {
});
```

3. 在 CI 生产环境构建
2. 在 CI 生产环境构建

```bash
# sets the base url for server-side data fetching
export VITE_ANALOG_PUBLIC_BASE_URL="https://domain.com/basehref"
# prefixes all assets and html with /basehref/
# Prefixes all assets and html with /basehref/
# if using nx:
npx nx run appname:build:production --baseHref='/basehref/'
# if using angular build directly:
npx ng build --baseHref="/basehref/"
```

4. 在生产环境的镜像指定新的环境变量
3. 在生产环境的镜像指定环境变量 `NITRO_APP_BASE_URL`

```bash
NITRO_APP_BASE_URL="/basehref/"
Expand All @@ -96,6 +104,18 @@ NITRO_APP_BASE_URL="/basehref/"
plugins: [
analog({
apiPrefix: 'api',
nitro: {
routeRules: {
'/': {
prerender: false,
},
},
},
prerender: {
routes: async () => {
return ['/'];
}
}
```

Nitro 所有的 API 路由将基于 `/basehref/api` 。
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载