Skip to content

Commit 0ec4eb1

Browse files
committed
docs: add ssr docs
1 parent d49c04c commit 0ec4eb1

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

site/src/vueDocs/extract-ssr.zh-CN.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
## SSR 静态样式导出
2+
3+
我们在思考是否可以如 v3 版本一样,预先烘焙组件的样式来使前端消费,所以提出了 [\[RFC\] Static Extract style](https://github.com/ant-design/ant-design/discussions/40985)。它的思路很简单,我们只需要提前将所有的组件进行一次渲染就可以从 cache 中获得完整的样式,然后将其写入到 css 文件中即可。
4+
5+
```tsx
6+
const cache = createCache();
7+
8+
// HTML Content
9+
renderToString(
10+
<StyleProvider cache={cache}>
11+
<Button />
12+
<Switch />
13+
<Input />
14+
{/* Rest antd components */}
15+
</StyleProvider>,
16+
);
17+
18+
// Style Content
19+
const styleText = extractStyle(cache);
20+
```
21+
22+
当然,这对于开发者而言稍微有点麻烦。所以我们提供了一个方法来实现该需求:
23+
24+
```tsx
25+
import { extractStyle } from 'ant-design-vue/lib/_util/static-style-extract';
26+
import fs from 'fs';
27+
28+
// `extractStyle` containers all the antd component
29+
// excludes popup like component which is no need in ssr: Modal, message, notification, etc.
30+
const css = extractStyle();
31+
32+
fs.writeFile(...);
33+
```
34+
35+
如果开发者使用了混合主题,也可以自行实现混合需求:
36+
37+
```tsx
38+
// `node` is the components set we prepared
39+
const css = extractStyle(node => (
40+
<>
41+
<ConfigProvider theme={theme1}>{node}</ConfigProvider>
42+
<ConfigProvider theme={theme2}>{node}</ConfigProvider>
43+
<ConfigProvider theme={theme3}>{node}</ConfigProvider>
44+
</>
45+
));
46+
```

0 commit comments

Comments
 (0)