Skip to content
This repository was archived by the owner on May 8, 2025. It is now read-only.

Commit 6d407bc

Browse files
committed
docs: update ssr-csr.md
1 parent 7085919 commit 6d407bc

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

docs/guide/ssr-csr.md

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
## csr模式下自己diy模版的生成内容
1515

16-
借助React官方api我们可以将一个React组件编译为html字符串
16+
借助React官方Api我们可以将一个React组件编译为html字符串
1717

1818
### 本地开发
1919

@@ -23,24 +23,31 @@
2323

2424
``` js
2525
// yk-cli/renderLayout.js
26-
const Layout = require(cwd + '/web/layout').default
27-
28-
const reactToString = (Component, props) => {
29-
return renderToString(React.createElement(Component, props))
30-
}
26+
const renderLayout = async () => {
27+
let Layout
28+
// 我们调用webpack的api来用应用目录下的webpack配置来编译layout组件,使其可以在Node环境中运行
29+
try {
30+
// serverless 场景我们从事先构建好的应用目录下的dist文件夹中读取layout
31+
Layout = isServerless ? require('../../../dist/Layout.server').default : require('../dist/Layout.server').default
32+
} catch (error) {
33+
// 非serverless场景首次读取失败我们先调用webpack api构建一遍在ykcli的目录下再读取
34+
const webpackWithPromise = require('./util')
35+
await webpackWithPromise(serverConfig)
36+
Layout = require('../dist/Layout.server').default
37+
}
3138

32-
// 此时props.children的值为undefined,我们只需要渲染一个空的layout骨架即可
33-
const props = {
34-
layoutData: {
35-
app: {
36-
config: config
39+
// 此时props.children 为 undefined 我们只需要传染一个空的layout骨架即可
40+
const props = {
41+
layoutData: {
42+
app: {
43+
config: config
44+
}
3745
}
3846
}
39-
}
40-
41-
const string = reactToString(Layout, props)
4247

43-
module.exports = string
48+
const str = reactToString(Layout, props)
49+
return str
50+
}
4451
```
4552

4653
然后启动服务,将string返回
@@ -167,8 +174,9 @@ return stream
167174
// ykfe-utils/renderToStream.js
168175

169176
if (config.type !== 'ssr') {
170-
const string = require('yk-cli/bin/renderLayout')
171-
return string
177+
const renderLayout = require('yk-cli/bin/renderLayout').default
178+
const str = await renderLayout()
179+
return str
172180
}
173181
```
174182

0 commit comments

Comments
 (0)