travi / hapi-react-router

hapi route to delegate routing for html content to react-router
MIT License
6 stars 2 forks source link

hapi-react-router

hapi route to delegate routing for html content to react-router v3

Build Status Codecov Node CI Workflow Status

Table of Contents

Usage

npm node license Try @travi/hapi-react-router on RunKit

Installation

$ npm install @travi/hapi-react-router -S

:warning: this plugin expects external babel-helpers to be provided by the consumer

Register with your Hapi v18+ server

Include this plugin in the manifest of your hapi application to direct all requests to /html to a server-side renderer for your react-router routes. It is assumed that something (not included) is in place to direct all text/html requests to this /html route.

In addition, redial fetch hooks will be triggered and rendering will wait for all related requests to complete. This enables populating the data store based on the components that are mounted for the current route. See redial arguments for the list of arguments supplied to triggered fetches.

Example

Dependencies:

import React from 'react';
import {IndexRoute, Route} from 'react-router';
import {createStore} from 'redux';
import {Provider} from 'react-redux';

Register with the Hapi server

export default {
  server: {port: process.env.PORT},
  register: {
    plugins: [
      {plugin: '@travi/hapi-html-request-router'},
      {
        plugin: '@travi/hapi-react-router',
        options: {
          respond: (reply, {renderedContent}) => {
            reply.view('layout', {renderedContent});
          },
          routes: (
            <Route path="/" component={Wrap}>
              <IndexRoute component={Index} />
              <Route path="/foo" component={Foo} />
              <Route path="/bar" component={Bar} />
              <Route path="*" component={NotFound} />
            </Route>
          ),
          Root: ({store, children}) => (
            <Provider store={store}>
              {children}
            </Provider>
          ),
          configureStore: ({session}) => createStore(reducer, composeMiddlewares(session)),
Optional custom renderer that passes blankie (optional to provide yourself) nonces as a prop
          render: (defaultRender, request) => ({html: defaultRender({nonces: request.plugins.blankie.nonces})})
        }
      }
    ]
  }
};

Dependencies for you to provide

This plugin provides you the ability to customize a few steps of the process. Default implementations are currently not provided, so these dependencies are required.

Redial fetch trigger arguments

Contribution

Commitizen friendly semantic-release Renovate

Install dependencies

$ nvm install
$ npm install

Verification

$ npm test

Run the example app

$ npm start