Skip to content
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

fix: ssr stream helmet bug and node runtime engines #5486

Merged
merged 4 commits into from
Oct 8, 2020
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: 4 additions & 0 deletions docs/docs/ssr.md
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,10 @@ export default () => {
}
```

### Helmet 结合 stream 渲染无法显示 title

因为 react-helmet 暂不支持 stream 渲染,如果使用 Helmet ,请使用 `mode: 'string'` 方式渲染。[nfl/react-helmet#322](https://github.com/nfl/react-helmet/issues/322)

### antd pro 怎样使用服务端渲染?

首先,[antd pro](https://github.com/ant-design/ant-design-pro/) 作为中后台项目,没有 SEO 需求,不适合做服务端渲染;
Expand Down
4 changes: 4 additions & 0 deletions docs/docs/ssr.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,10 @@ export default () => {
}
```

### Helmet 结合 stream 渲染无法显示 title

因为 react-helmet 暂不支持 stream 渲染,如果使用 Helmet ,请使用 `mode: 'string'` 方式渲染。[nfl/react-helmet#322](https://github.com/nfl/react-helmet/issues/322)

### antd pro 怎样使用服务端渲染?

首先,[antd pro](https://github.com/ant-design/ant-design-pro/) 作为中后台项目,没有 SEO 需求,不适合做服务端渲染;
Expand Down
3 changes: 3 additions & 0 deletions examples/ssr-koa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
"prettier --parser=typescript --write"
]
},
"engines": {
"node": ">=10.0.0"
},
"dependencies": {
"@ant-design/pro-layout": "^5.0.12",
"@umijs/preset-react": "^1.5.17",
Expand Down
3 changes: 1 addition & 2 deletions examples/ssr-koa/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@ app.use(async (ctx, next) => {
if (!render) {
render = require('./dist/umi.server');
}
// 这里默认是流失渲染
// 这里默认是字符串渲染
ctx.type = 'text/html';
ctx.status = 200;
const { html, error } = await render({
path: ctx.request.url,
mode: 'stream',
});
if (error) {
console.log('----------------服务端报错-------------------', error);
Expand Down
40 changes: 22 additions & 18 deletions examples/ssr-koa/src/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
import React from 'react';
import { connect, useIntl, getLocale, setLocale } from 'umi';
import { connect, useIntl, getLocale, setLocale, Helmet } from 'umi';
import { Button } from 'antd';

const Home = (props) => {
const { title } = props;
console.log('renderd', title);
const changeLangs = () => {

const lang = getLocale()
const lang = getLocale();
console.log('changeLangs', lang);
const change = lang === 'zh-CN' ? 'en-US' : 'zh-CN'
const change = lang === 'zh-CN' ? 'en-US' : 'zh-CN';
// // 刷新页面
// setLocale('zh-TW', true);
// // 不刷新页面
setLocale(change, false);
}
};
const intl = useIntl();
return (
<div>
<h1>{title}</h1>
<h2>{intl.formatMessage(
{
<Helmet>
<title>{title}</title>
</Helmet>
<h2>
{intl.formatMessage({
id: 'umi',
}
)}</h2>
})}
</h2>
<Button onClick={changeLangs}>切换语言</Button>
</div>
)
}
Home.getInitialProps = (async ({ store, isServer, history, match, route }) => {
);
};
Home.getInitialProps = async ({ store, isServer, history, match, route }) => {
// console.log(ctx);
if (!isServer) { return }
await store.dispatch({ type: 'test/test' })
const { test } = store.getState()
return { test }
})
if (!isServer) {
return;
}
await store.dispatch({ type: 'test/test' });
const { test } = store.getState();
return { test };
};

export default connect((({ test }) => ({ title: test.title })))(Home)
export default connect(({ test }) => ({ title: test.title }))(Home);
3 changes: 3 additions & 0 deletions examples/ssr-normal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"dev": "umi dev",
"build": "umi build"
},
"engines": {
"node": ">=10.0.0"
},
"private": true,
"peerDependencies": {
"umi": "latest"
Expand Down
1 change: 0 additions & 1 deletion examples/ssr-with-eggjs/app/controller/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class HomeController extends Controller {
// 将 html 模板传到服务端渲染函数中
const { error, html } = await this.serverRender({
path: ctx.url,
mode: 'stream',
getInitialPropsCtx: {},
htmlTemplate,
});
Expand Down
3 changes: 3 additions & 0 deletions examples/ssr-with-eggjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
"ci": "npm run lint && npm run cov",
"autod": "autod"
},
"engines": {
"node": ">=10.0.0"
},
"dependencies": {
"@umijs/preset-react": "^1.5.17",
"cross-env": "^5.2.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/preset-built-in/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,10 @@ test('ssr using stream', (done) => {
}).then(({ html, rootContainer }) => {
expect(rootContainer instanceof Stream).toBeTruthy();
expect(html instanceof Stream).toBeTruthy();
const expectBytes = new Buffer(
const expectBytes = Buffer.from(
'<div><ul><li>hello</li><li>world</li></ul></div>',
);
let bytes = new Buffer('');
let bytes = Buffer.from('');
rootContainer.on('data', (chunk) => {
bytes = Buffer.concat([bytes, chunk]);
});
Expand Down
1 change: 1 addition & 0 deletions packages/preset-built-in/src/plugins/features/ssr/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ export default (api: IApi) => {

config.output
.filename(OUTPUT_SERVER_FILENAME)
// avoid using `require().default`, just using `require()`
.libraryExport('default')
.chunkFilename('[name].server.js')
.libraryTarget('commonjs2');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@ test('renderServer stream', (done) => {
{ path: '/bar', component: () => <h1>bar</h1> },
],
}).then(({ pageHTML }) => {
const expectBytes = new Buffer('<div data-reactroot=""><h1>foo</h1></div>');
let bytes = new Buffer('');
const expectBytes = Buffer.from('<div data-reactroot=""><h1>foo</h1></div>');
let bytes = Buffer.from('');
expect(pageHTML instanceof Stream).toBeTruthy();
pageHTML.on('data', (chunk) => {
bytes = Buffer.concat([bytes, chunk]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ test('handleHTML stream', (done) => {
}).then(html => {
expect(html instanceof Stream).toBeTruthy();
expect(html instanceof Stream).toBeTruthy();
let bytes = new Buffer('');
let bytes = Buffer.from('');
html.on('data', (chunk) => {
bytes = Buffer.concat([bytes, chunk]);
});
Expand Down Expand Up @@ -141,7 +141,7 @@ test('handleHTML dynamicImport', async () => {

test('ReadableString', (done) => {
const wrapperStream = new ReadableString('<div></div>');
let bytes = new Buffer('');
let bytes = Buffer.from('');
wrapperStream.on('data', (chunk) => {
bytes = Buffer.concat([bytes, chunk]);
});
Expand Down