Last active
September 8, 2015 08:21
-
-
Save aulizko/1dbe1bef0cdd79e8fef2 to your computer and use it in GitHub Desktop.
SSR with react-router@1.0.0-beta4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// That variant actually works with 1.0.0-beta4 | |
// Link to the previous (1.0.0-beta3) version: | |
// https://github.com/cdebotton/react-universal/blob/master/src/server.js#L49 | |
import createLocation from 'history/lib/createLocation'; | |
import createHistory from 'history/lib/createMemoryHistory'; | |
import ReactDOM from 'react-dom/server'; | |
import {Router} from 'react-router'; | |
import Layout from './views/Layout'; | |
import Application from './containers/Application'; | |
app.use(function* render() { | |
const stats = require('../build/webpack-stats.json'); | |
const routes = require('../build/routes-compiled'); | |
const history = createHistory(); | |
const location = createLocation(this.req.url); | |
const markup = ReactDOM.renderToString( | |
<Application> | |
<Router history={history} location={location} routes={routes} /> | |
</Application> | |
); | |
const html = ReactDOM.renderToStaticMarkup( | |
<Layout | |
markup={markup} | |
payload={Application.getState()} | |
{...stats} /> | |
); | |
this.body = `<!doctype>\n${html}`; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
After reading the code of
history
I'm pretty sure the location prop on Router doesn't exist anymore. You have to docreateMemoryHistory(this.req.url)
orhistory.pushState(null, this.req.url)