2.5.2 • Published 6 years ago

hapi-render-react v2.5.2

Weekly downloads
1
License
BSD-3-Clause
Repository
github
Last release
6 years ago

hapi-render-react

npm License: BSD 3-clause "New" or "Revised" License standard-readme compliant

David David David

Table of Contents

Install

yarn add hapi-render-react
npm install hapi-render-react

Usage

const { Server } = require('hapi');
const Main = require('apr-main');
const RenderReact = require('hapi-render-react');
const Path = require('path');

const { PORT = 3001 } = process.env;

const server = new Server({
  port: PORT
});

Main(async () => {
  await server.register({
    plugin: RenderReact,
    options: {
      relativeTo: Path.join(__dirname, 'views')
    }
  });

  await server.initialize();

  server.route({
    method: 'GET',
    path: '/',
    handler: {
      view: {
        name: 'home',
        dynamic: false, // true by default. if !dynamic, it generates an .html and always serves said file
        props: {
          hello: 'world'
        }
      }
    }
  });

  server.route({
    method: 'GET',
    path: '/hello',
    handler: (request, h) => {
      return h.render('hello', { name: 'world' });
    }
  });

  await server.start();

  // eslint-disable-next-line no-console
  console.log(`Server started at ${server.info.uri}`);
});

pages/home.js:

import React from 'react';

export default ({ hello }) => <p>Hello {hello}</p>;

pages/hello.js:

import React from 'react';

export default ({ name }) => <p>Hello {name}</p>;

_document

inspired by Next.js.

You can customize how a view is rendered. Example of a _document to support React-Helmet:

const React = require('react');
const { HelmetProvider } = require('react-helmet-async');
const { renderToString, renderToNodeStream } = require('react-dom/server');

module.exports = View => {
  const helmetContext = {};

  const appHtml = renderToString(
    React.createElement(
      HelmetProvider,
      { context: helmetContext },
      React.createElement(View)
    )
  );

  const { helmet } = helmetContext;

  const {
    bodyAttributes,
    htmlAttributes,
    link,
    meta,
    noscript,
    script,
    style,
    title
  } = helmet;

  const htmlAttrs = htmlAttributes.toString();
  const bodyAttrs = bodyAttributes.toString();

  const bodyHtml = renderToString(
    React.createElement(React.Fragment, null, [
      link.toComponent(),
      noscript.toComponent(),
      style.toComponent(),
      script.toComponent()
    ])
  );

  return renderToNodeStream(
    React.createElement('html', htmlAttrs, [
      React.createElement('head', null, [
        meta.toComponent(),
        title.toComponent()
      ]),
      React.createElement(
        'body',
        Object.assign(bodyAttrs, {
          dangerouslySetInnerHTML: {
            __html: appHtml + bodyHtml
          }
        })
      )
    ])
  );
};

License

BSD-3-Clause

2.5.2

6 years ago

2.5.1

6 years ago

2.5.0

6 years ago

2.4.1

6 years ago

2.4.0

6 years ago

2.3.0

6 years ago

2.2.0

6 years ago

2.1.0

6 years ago

2.0.0

6 years ago

1.1.0

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago