You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: website/docs/advanced-topics/server-side-rendering.mdx
+21Lines changed: 21 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -128,6 +128,27 @@ suspended.
128
128
With this approach, developers can build a client-agnostic, SSR-ready
129
129
application with data fetching with very little effort.
130
130
131
+
## Rendering `<head>` Tags
132
+
133
+
A common need with SSR is rendering dynamic `<head>` content (e.g. `<title>`,
134
+
`<meta>`) so that crawlers and social previews see the right metadata on first
135
+
load.
136
+
137
+
`ServerRenderer` only renders your component tree (typically at the body of the document),
138
+
it has no access to `<head>`. Head tags must therefore be generated **on the server, outside of
139
+
Yew**, and spliced into the HTML template before it is sent to the client.
140
+
141
+
The [`ssr_router` example](https://github.com/yewstack/yew/blob/master/examples/ssr_router/src/bin/ssr_router_server.rs) demonstrates this pattern: the server recognizes the
142
+
route from the request URL, generates the appropriate `<title>` and `<meta>`
143
+
tags, and injects them into the Trunk-generated `index.html` before
144
+
`</head>`.
145
+
146
+
:::info
147
+
148
+
For a fully SSR-compatible third-party solution, use [the `<Helmet/>` component from Bounce](https://docs.rs/bounce/latest/bounce/helmet/index.html).
149
+
150
+
:::
151
+
131
152
## SSR Hydration
132
153
133
154
Hydration is the process that connects a Yew application to the
SSR でよく必要とされるのは、クローラーやソーシャルプレビューが最初のロード時に正しいメタデータを参照できるよう、動的な `<head>` コンテンツ(`<title>`、`<meta>` など)をレンダリングすることです。
90
+
91
+
`ServerRenderer` はコンポーネントツリー(通常はドキュメントの body 部分)のみをレンダリングし、`<head>` にはアクセスできません。そのため、head タグは **Yew の外部でサーバー側に**生成し、クライアントに送信する前に HTML テンプレートに埋め込む必要があります。
SSR でよく必要とされるのは、クローラーやソーシャルプレビューが最初のロード時に正しいメタデータを参照できるよう、動的な `<head>` コンテンツ(`<title>`、`<meta>` など)をレンダリングすることです。
90
+
91
+
`ServerRenderer` はコンポーネントツリー(通常はドキュメントの body 部分)のみをレンダリングし、`<head>` にはアクセスできません。そのため、head タグは **Yew の外部でサーバー側に**生成し、クライアントに送信する前に HTML テンプレートに埋め込む必要があります。
SSR 中的一个常见需求是渲染动态 `<head>` 内容(例如 `<title>`、`<meta>`),使爬虫和社交预览在首次加载时能看到正确的元数据。
90
90
91
-
注水是将 Yew 应用程序连接到服务端生成的 HTML 文件的过程。默认情况下,`ServerRender` 打印可注水的 HTML 字符串,其中包含额外的信息以便于注水。当调用 `Renderer::hydrate` 方法时,Yew 不会从头开始渲染,而是将应用程序生成的虚拟 DOM 与服务器渲染器生成的 HTML 字符串进行协调。
91
+
`ServerRenderer` 只渲染组件树(通常对应文档的 body 部分),无法访问 `<head>`。因此,head 标签必须**在服务端、Yew 之外**生成,并在发送给客户端之前拼接到 HTML 模板中。
如需完全兼容 SSR 的第三方解决方案,请使用 [Bounce 的 `<Helmet/>` 组件](https://docs.rs/bounce/latest/bounce/helmet/index.html)。
98
+
99
+
:::
100
+
101
+
## SSR 水合(SSR Hydration)
102
+
103
+
水合是将 Yew 应用程序连接到服务端生成的 HTML 文件的过程。默认情况下,`ServerRender` 打印可水合的 HTML 字符串,其中包含额外的信息以便于水合。当调用 `Renderer::hydrate` 方法时,Yew 不会从头开始渲染,而是将应用程序生成的虚拟 DOM 与服务器渲染器生成的 HTML 字符串进行协调。
92
104
93
105
:::caution
94
106
95
-
为了成功对由`ServerRenderer` 创建的 HTML 标记注水,客户端必须生成一个虚拟 DOM 布局,它与用于 SSR 的布局完全匹配,包括不包含任何元素的组件。如果您有任何只在一个实现中有用的组件,您可能希望使用 `PhantomComponent` 来填充额外组件的位置。
107
+
要成功对由`ServerRenderer` 创建的 HTML 标记进行水合,客户端必须生成一个虚拟 DOM 布局,它与用于 SSR 的布局完全匹配,包括不包含任何元素的组件。如果您有任何只在一个实现中有用的组件,您可能希望使用 `PhantomComponent` 来填充额外组件的位置。
96
108
:::
97
109
98
110
:::warning
99
111
100
-
只有在浏览器初始渲染 SSR 输出(静态 HTML)后,真实 DOM 与预期 DOM 匹配时,注水才能成功。如果您的 HTML 不符合规范,注水可能会失败。浏览器可能会更改不正确的 HTML 的 DOM 结构,导致实际 DOM 与预期 DOM 不同。例如,[如果您有一个没有 `<tbody>` 的 `<table>`,浏览器可能会向 DOM 添加一个 `<tbody>`](https://github.com/yewstack/yew/issues/2684)
112
+
只有在浏览器初始渲染 SSR 输出(静态 HTML)后,真实 DOM 与预期 DOM 匹配时,水合才能成功。如果您的 HTML 不符合规范,水合可能会失败。浏览器可能会更改不正确的 HTML 的 DOM 结构,导致实际 DOM 与预期 DOM 不同。例如,[如果您有一个没有 `<tbody>` 的 `<table>`,浏览器可能会向 DOM 添加一个 `<tbody>`](https://github.com/yewstack/yew/issues/2684)
0 commit comments