Server-side rendering for react-router and react-meteor-data rehydratating Meteor subscriptions
It has a protection against leaking your data. Only subscribed data will be available just the way it would be on the client.
meteor add reactrouter:react-router-ssr
The routes
argument takes the routes you want react-router to use (you don't have to call ReactDOM.render()
yourself)
Read the react-router documentation for more informations.
Your main <Route />
node of your application.
Notice that their is no <Router />
element, ReactRouterSSR takes care of creating it on the client and server with the correct parameters
rootElement
[string]: The root element ID your React application is mounted (default toreact-app
)props
[object]: The additional arguments you would like to give to the<Router />
component on the client.history
: History object to use. You can usenew ReactRouter.history.createHistory()
,new ReactRouter.history.createHashHistory()
ornew ReactRouter.history.createMemoryHistory()
props
[object]: The additional arguments you would like to give to the<Router />
component on the server.preRender
[function(req, res)]: Executed just before the renderToStringpostRender
[function(req, res)]: Executed just after the renderToStringdontMoveScripts
[bool]: Keep the script inside the head tag instead of moving it at the end of the body
const {IndexRoute, Route} = ReactRouter;
AppRoutes = (
<Route path="/" component={App}>
<IndexRoute component={HomePage} />
<Route path="login" component={LoginPage} />
<Route path="*" component={NotFoundPage} />
{/* ... */}
</Route>
);
HomePage = React.createClass({
mixins: [ReactMeteorData],
getMeteorData() {
Meteor.subscribe('profile');
return {
profile: Profile.findOne({ user: Meteor.userId() })
};
},
render() {
return <div>Hi {profile.name}</div>;
}
});
ReactRouterSSR.Run(AppRoutes);
const {IndexRoute, Route} = ReactRouter;
AppRoutes = (
<Route path="/" component={App}>
<IndexRoute component={HomePage} />
<Route path="login" component={LoginPage} />
<Route path="*" component={NotFoundPage} />
{/* ... */}
</Route>
);
ReactRouterSSR.Run(AppRoutes, {
props: {
onUpdate() {
// Notify the page has been changed to Google Analytics
ga('send', 'pageview');
}
}
}, {
preRender: function(req, res) {
ReactCookie.plugToRequest(req, res);
}
});
if (Meteor.isClient) {
// Load Google Analytics
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXXXX-X', 'auto');
ga('send', 'pageview');
}