-
Notifications
You must be signed in to change notification settings - Fork 200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add server rendering example #95
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,3 @@ | ||
{ | ||
"stage": 0, | ||
"plugins": [ | ||
"react-transform" | ||
], | ||
"extra": { | ||
"react-transform": [{ | ||
"target": "react-transform-webpack-hmr", | ||
"imports": ["react"], | ||
"locals": ["module"] | ||
}] | ||
} | ||
"stage": 0 | ||
} |
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
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,42 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { createStore, compose } from 'redux'; | ||
|
||
import { | ||
ReduxRouter, | ||
reduxReactRouter, | ||
} from 'redux-router'; | ||
|
||
import { Provider } from 'react-redux'; | ||
import { devTools } from 'redux-devtools'; | ||
import { DevTools, DebugPanel, LogMonitor } from 'redux-devtools/lib/react'; | ||
import createHistory from 'history/lib/createBrowserHistory'; | ||
|
||
import routes from './routes'; | ||
import reducer from './reducer'; | ||
import {MOUNT_ID} from './constants'; | ||
|
||
const store = compose( | ||
reduxReactRouter({ createHistory }), | ||
devTools() | ||
)(createStore)(reducer, window.__initialState); | ||
|
||
const rootComponent = ( | ||
<Provider store={store}> | ||
<ReduxRouter routes={routes} /> | ||
</Provider> | ||
); | ||
|
||
const mountNode = document.getElementById(MOUNT_ID); | ||
|
||
// First render to match markup from server | ||
ReactDOM.render(rootComponent, mountNode); | ||
// Optional second render with dev-tools | ||
ReactDOM.render(( | ||
<div> | ||
{rootComponent} | ||
<DebugPanel top right bottom> | ||
<DevTools store={store} monitor={LogMonitor} /> | ||
</DebugPanel> | ||
</div> | ||
), mountNode); |
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,56 @@ | ||
import React, { Component, PropTypes } from 'react'; | ||
import {connect} from 'react-redux'; | ||
import {Link} from 'react-router'; | ||
|
||
@connect(state => ({ routerState: state.router })) | ||
export const App = class App extends Component { | ||
static propTypes = { | ||
children: PropTypes.node | ||
} | ||
|
||
render() { | ||
const links = [ | ||
'/', | ||
'/parent?foo=bar', | ||
'/parent/child?bar=baz', | ||
'/parent/child/123?baz=foo' | ||
].map(l => | ||
<p> | ||
<Link to={l}>{l}</Link> | ||
</p> | ||
); | ||
|
||
return ( | ||
<div> | ||
<h1>App Container</h1> | ||
{links} | ||
{this.props.children} | ||
</div> | ||
); | ||
} | ||
}; | ||
|
||
export const Parent = class Parent extends Component { | ||
static propTypes = { | ||
children: PropTypes.node | ||
} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<h2>Parent</h2> | ||
{this.props.children} | ||
</div> | ||
); | ||
} | ||
}; | ||
|
||
export const Child = class Child extends Component { | ||
render() { | ||
return ( | ||
<div> | ||
<h2>Child</h2> | ||
</div> | ||
); | ||
} | ||
}; |
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 @@ | ||
export const MOUNT_ID = 'root'; |
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,13 @@ | ||
{ | ||
"name": "redux-router-server-rendering-example", | ||
"version": "0.1.0", | ||
"description": "", | ||
"main": "server.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"start": "node -r babel/register server.js" | ||
}, | ||
"author": "Andrew Clark <[email protected]>", | ||
"license": "MIT", | ||
"dependencies": {} | ||
} |
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,6 @@ | ||
import {combineReducers} from 'redux'; | ||
import {routerStateReducer} from '../../lib'; // 'redux-router'; | ||
|
||
export default combineReducers({ | ||
router: routerStateReducer | ||
}); |
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,12 @@ | ||
import React from 'react'; | ||
import {Route} from 'react-router'; | ||
import {App, Parent, Child} from './components'; | ||
|
||
export default ( | ||
<Route path="/" component={App}> | ||
<Route path="parent" component={Parent}> | ||
<Route path="child" component={Child} /> | ||
<Route path="child/:id" component={Child} /> | ||
</Route> | ||
</Route> | ||
); |
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,80 @@ | ||
import express from 'express'; | ||
import webpack from 'webpack'; | ||
import React from 'react'; | ||
import {renderToString} from 'react-dom/server'; | ||
import {Provider} from 'react-redux'; | ||
import {createStore} from 'redux'; | ||
import {ReduxRouter} from '../../src'; // 'redux-router' | ||
import {reduxReactRouter, match} from '../../src/server'; // 'redux-router/server'; | ||
import qs from 'query-string'; | ||
import serialize from 'serialize-javascript'; | ||
|
||
import webpackDevMiddleware from 'webpack-dev-middleware'; | ||
import webpackHotMiddleware from 'webpack-hot-middleware'; | ||
|
||
import config from './webpack.config.clientDev'; | ||
import {MOUNT_ID} from './constants'; | ||
import reducer from './reducer'; | ||
import routes from './routes'; | ||
|
||
const app = express(); | ||
const compiler = webpack(config); | ||
|
||
const getMarkup = (store) => { | ||
const initialState = serialize(store.getState()); | ||
|
||
const markup = renderToString( | ||
<Provider store={store} key="provider"> | ||
<ReduxRouter/> | ||
</Provider> | ||
); | ||
|
||
return `<!doctype html> | ||
<html> | ||
<head> | ||
<title>Redux React Router – Server rendering Example</title> | ||
</head> | ||
<body> | ||
<div id="${MOUNT_ID}">${markup}</div> | ||
<script>window.__initialState = ${initialState};</script> | ||
<script src="/static/bundle.js"></script> | ||
</body> | ||
</html> | ||
`; | ||
}; | ||
|
||
app.use(webpackDevMiddleware(compiler, { | ||
noInfo: true, | ||
publicPath: config.output.publicPath | ||
})); | ||
|
||
app.use(webpackHotMiddleware(compiler)); | ||
|
||
app.use((req, res) => { | ||
const store = reduxReactRouter({ routes })(createStore)(reducer); | ||
const query = qs.stringify(req.query); | ||
const url = req.path + (query.length ? '?' + query : ''); | ||
|
||
store.dispatch(match(url, (error, redirectLocation, routerState) => { | ||
if (redirectLocation) { | ||
res.redirect(redirectLocation.pathname + redirectLocation.search); | ||
} else if (error) { | ||
console.error('Router error:', error); | ||
res.status(500); | ||
res.send(error); | ||
} else if (!routerState) { | ||
res.status(500); | ||
} else { | ||
res.send(getMarkup(store)); | ||
} | ||
})); | ||
}); | ||
|
||
app.listen(3000, 'localhost', error => { | ||
if (error) { | ||
console.log(error); | ||
return; | ||
} | ||
|
||
console.log('Listening at http://localhost:3000'); | ||
}); |
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,27 @@ | ||
import { | ||
HotModuleReplacementPlugin, | ||
NoErrorsPlugin | ||
} from 'webpack'; | ||
|
||
import baseConfig from '../webpack.config.base'; | ||
import mergeConfig from '../mergeConfig'; | ||
import path from 'path'; | ||
|
||
const clientDevConfig = mergeConfig(baseConfig, { | ||
entry: [ | ||
'webpack-hot-middleware/client', | ||
'./client' | ||
], | ||
output: { | ||
path: path.resolve(__dirname, 'build'), | ||
filename: 'bundle.js', | ||
publicPath: '/static/' | ||
}, | ||
plugins: [ | ||
new HotModuleReplacementPlugin(), | ||
new NoErrorsPlugin() | ||
], | ||
devtool: 'eval' | ||
}); | ||
|
||
export default clientDevConfig; |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#162 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe use
{ routes, createHistory: createMemoryHistory }
instead of{ routes }
?