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: README.md
+92-37Lines changed: 92 additions & 37 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,6 +8,7 @@ Yet another simple React SSR solution inspired by vue-server-render with:
8
8
2. prefetch/preload client injection with ClientManifest, generated by [ssr-webpack-plugin](https://github.com/JounQin/ssr-webpack-plugin)
9
9
3. server css support with [react-style-loader](https://github.com/JounQin/react-style-loader)
10
10
4. Async component support with [react-async-component](https://github.com/ctrlplusb/react-async-component) and [react-async-bootstrapper](https://github.com/ctrlplusb/react-async-bootstrapper)
11
+
5. custom dynamic head management for better SEO
11
12
12
13
## Real World Demo
13
14
@@ -35,7 +36,7 @@ import { resolve } from './config'
35
36
36
37
importbasefrom'./base'
37
38
38
-
exportdefaultmerge(base, {
39
+
exportdefaultmerge.smart(base, {
39
40
// Point entry to your app's server entry file
40
41
entry:resolve('src/entry-server.js'),
41
42
@@ -64,6 +65,7 @@ export default merge(base, {
64
65
65
66
plugins: [
66
67
newwebpack.DefinePlugin({
68
+
'process.env.REACT_ENV':'"server"',
67
69
__SERVER__:true,
68
70
}),
69
71
// This is the plugin that turns the entire output of the server build
@@ -79,7 +81,8 @@ export default merge(base, {
79
81
```js
80
82
importwebpackfrom'webpack'
81
83
importmergefrom'webpack-merge'
82
-
importHtmlWebpackPluginfrom'html-webpack-plugin'
84
+
// do not need 'html-webpack-plugin' any more because we will render html from server
85
+
// import HtmlWebpackPlugin from 'html-webpack-plugin'
@@ -184,17 +185,17 @@ Since you generate server bundle renderer as above, you can easily call `rendere
184
185
185
186
`renderToString` is very simple, just `try/catch` error to handle it.
186
187
187
-
`renderToStream` is a tiny complicated to handle, you can rediect or reject request by listening `error` event and handle error param. If you want to render application but change response status, you can listen `afterRender` event and handle with your own `userContext`, for example maybe you want to render 404 Not Found page via React Component but respond with 404 status.
188
+
`renderToStream` is a tiny complicated to handle, you can rediect or reject request by listening `error` event and handle error param. If you want to render application but change response status, you can listen `afterRender` event and handle with your own `context`, for example maybe you want to render 404 Not Found page via React Component but respond with 404 status.
188
189
189
190
### State management
190
191
191
192
If you set `context.state` on server, it will auto inject a script contains `window.__INITIAL_STATE__` in output, so that you can resue server state on client.
192
193
193
-
### Style injection on server
194
+
### Style injection and Head Management
194
195
195
196
Without SSR, we can easily use `style-loader`, however we need to collect rendered components with there styles together on runtime, so we choose to use [react-style-loader](https://github.com/JounQin/react-style-loader) which forked [vue-style-loader](https://github.com/vuejs/vue-style-loader) indeed.
196
197
197
-
Let's create a simple HOC for server style and http injection.
198
+
Let's create a simple HOC for server style, title management and http injection.
198
199
199
200
```js
200
201
importaxiosfrom'axios'
@@ -203,38 +204,72 @@ import PropTypes from 'prop-types'
0 commit comments