forked from react-boilerplate/react-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
server: Add basic server-side renderer
- Loading branch information
Showing
2 changed files
with
35 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const fs = require('fs'); | ||
|
||
const React = require('react'); | ||
const ReactDOMServer = require('react-dom/server'); | ||
|
||
// Memory history | ||
const createMemoryHistory = require('history/createMemoryHistory').default; | ||
|
||
// Import root containers | ||
const Root = require('../app/app').default; | ||
const messages = require('../app/i18n').translationMessages; | ||
const configureStore = require('../app/configureStore').default; | ||
|
||
module.exports = (app) => { | ||
app.use((req, res) => { | ||
const memoryHistory = createMemoryHistory(req.url); | ||
memoryHistory.push(req.originalUrl); | ||
const store = configureStore({}, memoryHistory); | ||
const html = ReactDOMServer.renderToString( | ||
<Root messages={messages} history={memoryHistory} store={store} /> | ||
); | ||
|
||
// This should read the compiled index.html file when running from the | ||
// webpack bundle and the non-compiled index.html file when running | ||
// from babel-node | ||
fs.readFile('./build/index.html', 'utf8', (err, data) => { | ||
// Set a dummy user agent based on the request user agent. | ||
global.navigator = { userAgent: req.headers['user-agent'] }; | ||
res.send(data.replace(/<div id="app">\s*<\/div>/, | ||
`<div id="app">${html}</div>`)); | ||
}); | ||
}); | ||
}; |