Skip to content

Commit 194b157

Browse files
committed
Update server rendering guide
1 parent e72812d commit 194b157

File tree

1 file changed

+21
-28
lines changed

1 file changed

+21
-28
lines changed
Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,41 @@
1-
Rendering on the server is not much different than rendering in the
2-
browser. The primary difference is that while on the client we can do
3-
asynchronous work *after* rendering, on the server we have to do that
4-
work *before* rendering, like path matching and data fetching.
1+
## Server Rendering
52

6-
We'll start with the client since it's pretty simple. The only
7-
interesting thing is that we are getting some initial data from the
8-
server render to ensure that the first client render markup matches the
9-
the markup from the server render. Without the initial data, the markup
10-
the client creates would differ from what the server sent, and React
11-
would replace the DOM.
3+
Rendering on the server is not much different than rendering in the browser. The primary difference is that while on the client we can do asynchronous work *after* rendering, on the server we have to do that work *before* rendering, like path matching and data fetching.
4+
5+
We'll start with the client since it's pretty simple. The only interesting thing is that we are getting some initial data from the server render to ensure that the first client render markup matches the the markup from the server render. Without the initial data, the markup the client creates would differ from what the server sent, and React would replace the DOM.
126

137
```js
14-
// client.js
15-
import { Router } from 'react-router';
16-
import BrowserHistory from 'react-router/lib/BrowserHistory';
8+
// browser.js
9+
import createHistory from 'history/lib/createBrowserHistory';
10+
import Router from 'react-router';
1711
import routes from './routes';
1812

19-
React.render(<Router children={routes}/>, document.getElementById('app'));
13+
React.render(
14+
<Router history={createHistory()} children={routes}/>,
15+
document.getElementById('app')
16+
);
2017
```
2118

22-
On the server, we need to asynchronously match the routes and fetch data
23-
first, and then provide the initial state to the router so it renders
24-
synchronously.
19+
On the server, we need to asynchronously match the routes and fetch data first, and then provide the initial state to the router so it renders synchronously.
2520

2621
```js
2722
// server.js
23+
import createLocation from 'history/lib/createLocation';
2824
import Router from 'react-router';
29-
import Location from 'react-router/lib/Location';
3025
import routes from './routes';
3126

32-
// you'll want to configure your server to serve up static assets, and
33-
// and then handle the rest with React Router
27+
// you'll want to configure your server to serve up static assets
28+
// and then handle the rest with React Router. this example assumes
29+
// an express-like API
3430
serveNonStaticPaths((req, res) => {
35-
var location = new Location(req.path, req.query);
31+
// create a location object that tells the router what to render
32+
var location = createLocation(req.originalUrl);
3633

37-
Router.run(routes, location, (error, initialState, transition) => {
34+
Router.run(routes, location, (error, initialState, redirectInfo) => {
3835
// do your own data fetching, perhaps using the
3936
// branch of components in the initialState
4037
fetchSomeData(initialState.components, (error, initialData) => {
41-
var html = React.renderToString(
42-
<Router {...initialState}/>
43-
);
38+
var html = React.renderToString(<Router initialState={initialState} />);
4439
res.send(renderFullPage(html, initialData));
4540
});
4641
});
@@ -63,8 +58,7 @@ function renderFullPage(html, initialData) {
6358
}
6459
```
6560

66-
API Routes
67-
----------
61+
### API Routes
6862

6963
Sometimes servers function as both the UI server and the api server. Do
7064
your best to think of them separately.
@@ -80,4 +74,3 @@ the methods on your route instead of rendering the components. I was
8074
about to tell you not to do this, but please do and let us know how it
8175
goes. Most of us should probably just use the server side router from
8276
our favorite libraries for now, though.
83-

0 commit comments

Comments
 (0)