prashaantt / hapi-react-ssr

Render React isomorphically using react-router in hapi
MIT License
4 stars 0 forks source link

hapi-react-ssr

Render React isomorphically using react-router in hapi using a data store of your choice (e.g. MobX).

Dependency Status devDependency Status

Installation

npm install hapi-react-ssr vision --save

Options

The plugin accepts the following options:

Example usage

Define your routes as a PlainRoute:

// routes.js

import App from './components/App';
import Homepage from './components/Homepage';

const routes = [
    {
        path: '/',
        component: App,
        indexRoute: { component: Homepage },
        childRoutes: [...]
    }
];

export default routes;
// server.js

import HapiReactSSR from "hapi-react-ssr";
import Vision from "vision";
import { Provider } from "mobx-react"; // or use any other Provider to pass context down to all children

...

const plugins = [
    Vision,
    {
        register: HapiReactSSR,
        options: {
            routes,
            getInitialContext: () => {
                return {
                    state: new AppState(),
                    store: new AppStore()
                }
            },
            bootstrapAction: 'fetchData',
            rootElement: Provider,
            template: './src/server/index',
            params: {
                env: process.env.NODE_ENV
            }
        }
    },
    // other plugins
];

server.register(plugins, (err) => {});

Notes

{{! handlebars example }}

<html>
<body>
    <div id="react-root">{{{ componentRenderedToString }}}</div>
    <script type="application/javascript">
        window.__INITIAL_STATE__ = {{{ context }}};
    </script>
</body>
</html>

License

MIT