Skip to content

Commit 5ca3166

Browse files
author
yangfan19
committed
21-服务器的SSR配置
1 parent 85cdd23 commit 5ca3166

File tree

6 files changed

+21
-65
lines changed

6 files changed

+21
-65
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ webpack-cli: "^3.3.2"
3333
- [`webpack-18`](./webpack-18/README.md) — 提取页面的公共资源
3434
- [`webpack-19`](./webpack-19/README.md) — 代码分割-动态 import
3535
- [`webpack-20`](./webpack-20/README.md) — 配置 eslint
36+
- [`webpack-21`](./webpack-21/README.md) — 简易的服务器 SSR 配置

webpack-21/README.md

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,7 @@
22

33
## 服务器渲染 SSR
44

5-
推荐使用 github 上的一些开源好的规则仓库,比如:[standard](https://github.com/standard/standard)
6-
7-
<br />
8-
9-
## 配置
10-
11-
/src/search/index.js
12-
13-
```shell
14-
npm install standard --save-dev
15-
16-
```
5+
SSR 的本质就是在[webpack 进行打包](./webpack.ssr.js)的时候,`libraryTarget:umd`,打包完成的`/dist/search-server.js`,在[node](./server/index.js)端进行读取,然后利用`react-dom/server``renderToString`进行转成虚拟 DOM 数,在通过读取`/dist/search.html`使用插槽的方式进行整个模板的替换。
176

187
<br />
198

@@ -22,10 +11,8 @@ package.json
2211
```shell
2312
"scripts": {
2413
...
25-
"eslint": "standard && node src/*/*.js",
26-
"flx": "standard --fix",
14+
"server": "node server/index.js",
15+
"build:ssr": "webpack --config webpack.ssr.js"
2716
},
2817

2918
```
30-
31-
<br />

webpack-21/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"build": "webpack --config webpack.prod.js",
1111
"watch": "webpack --watch",
1212
"dev": "webpack-dev-server --open --config webpack.dev.js",
13+
"server": "node server/index.js",
1314
"build:ssr": "webpack --config webpack.ssr.js"
1415
},
1516
"standard": {

webpack-21/server/index.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ if (typeof window === 'undefined') {
44
}
55

66
const express = require('express');
7+
const fs = require('fs');
8+
const path = require('path');
79
const { renderToString } = require('react-dom/server');
810
const SSR = require('../dist/search-server.js');
11+
const template = fs.readFileSync(path.join(__dirname, '../dist/search.html'), 'utf-8');
912

1013
const server = (port) => {
1114
const app = express();
@@ -27,18 +30,5 @@ server(process.env.PORT || 3000);
2730

2831
// 模板
2932
const renderMarKup = (str) => {
30-
return `
31-
<!DOCTYPE html>
32-
<html lang="en">
33-
<head>
34-
<meta charset="UTF-8">
35-
<meta http-equiv="X-UA-Compatible" content="IE=edge">
36-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
37-
<title>ssr</title>
38-
</head>
39-
<body>
40-
<div id="root"> ${str}</div>
41-
</body>
42-
</html>
43-
`;
33+
return template.replace('<!--HTML_DOEM-->', str);
4434
};

webpack-21/src/search/index-server.js

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,17 @@
11
'use strict';
22

3-
// import React from 'react';
4-
// import largeNumber from 'large-number';
5-
// import logo from './images/logo.png';
6-
// import './search.less';
73
const React = require('react');
84
const logo = require('./images/im1.jpg');
9-
const s = require('./search01.less');
105

11-
class Search extends React.Component {
12-
constructor() {
13-
super(...arguments);
6+
require('./search01.less');
147

15-
this.state = {
16-
Text: null,
17-
};
18-
}
19-
20-
loadComponent() {
21-
import('./text.js').then((Text) => {
22-
this.setState({
23-
Text: Text.default,
24-
});
25-
});
26-
}
27-
28-
render() {
29-
const { Text } = this.state;
30-
// const addResult = largeNumber('999', '1');
31-
return (
32-
<div className='search-text'>
33-
{Text ? <Text /> : null}
34-
{/* {addResult} */}
35-
搜索文字的内容
36-
<img src={logo} onClick={this.loadComponent.bind(this)} />
37-
</div>
38-
);
39-
}
40-
}
8+
const Search = () => {
9+
return (
10+
<div className='box'>
11+
搜索文字的内容
12+
<img src={logo} />
13+
</div>
14+
);
15+
};
4116

4217
module.exports = <Search />;

webpack-21/src/search/index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
</head>
99

1010
<body>
11-
<div id="root"></div>
11+
<div id="root">
12+
<!--HTML_DOEM-->
13+
</div>
1214
<!-- <script src='https://unpkg.com/react@16/umd/react.production.min.js'></script>
1315
<script src='https://unpkg.com/react-dom@16/umd/react-dom.production.min.js'></script> -->
1416
</body>

0 commit comments

Comments
 (0)