forked from shakacode/react_on_rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerRouterApp.jsx
31 lines (25 loc) · 991 Bytes
/
ServerRouterApp.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import React from 'react';
import { match, RouterContext } from 'react-router';
import routes from '../routes/routes';
export default (_props, railsContext) => {
let error;
let redirectLocation;
let routeProps;
const { location } = railsContext;
// See https://github.com/reactjs/react-router/blob/master/docs/guides/ServerRendering.md
match({ routes, location }, (_error, _redirectLocation, _routeProps) => {
error = _error;
redirectLocation = _redirectLocation;
routeProps = _routeProps;
});
// This tell react_on_rails to skip server rendering any HTML. Note, client rendering
// will handle the redirect. What's key is that we don't try to render.
// Critical to return the Object properties to match this { error, redirectLocation }
if (error || redirectLocation) {
return { error, redirectLocation };
}
// Important that you don't do this if you are redirecting or have an error.
return (
<RouterContext {...routeProps} />
);
};