• Stars
    star
    231
  • Rank 173,434 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 10 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

📦 A hapi view engine for React components

hapi-react-views

A hapi view engine for React components.

Build Status Dependency Status Peer Dependency Status Dev Dependency Status

By default rendering is done using ReactDOMServer.renderToStaticMarkup. You can also choose to use ReactDOMServer.renderToString, preserving the data-react-id attributes so re-mounting client side is possible.

Install

$ npm install hapi-react-views

Note: Your project should have it's own react and react-dom dependencies installed. We depend on these via peerDependencies.

Usage

Configuring the server manually:

const Hapi = require('@hapi/hapi');
const HapiReactViews = require('hapi-react-views');
const Vision = require('@hapi/vision');

require('@babel/register')({
    presets: ['@babel/preset-react', '@babel/preset-env']
});

const main = async function () {
    const server = Hapi.Server();

    await server.register(Vision);

    server.views({
        engines: {
            jsx: HapiReactViews
        },
        compileOptions: {}, // optional
        relativeTo: __dirname,
        path: 'views'
    });

    await server.start();

    console.log(`Server is listening at ${server.info.uri}`);
};

main();

Note: As of hapi-react-views v4.x your project must register a transpiler such as babel. An alternative to this is to transpile ahead of time and save the result to file.

Note: As of hapi v9.x, your project must register the vision plugin in order for the server.views() and server.render() methods to be available.

API

server.views(options)

Please refer to the vision docs on server.views(options) for complete details.

We'll be focusing on the compileOptions property that you can include when passing options to server.views.

The following compileOptions will customize how hapi-react-views works.

  • compileOptions - options object passed to the engine's compile function. Defaults to {}.
    • doctype - a simple string prepended to the response. Defaults to <!DOCTYPE html>
    • renderMethod - the method to invoke on ReactDOMServer to generate our output. Available options are renderToStaticMarkup and renderToString. Defaults to renderToStaticMarkup.
    • removeCache - since transpilers tend to take a while to startup, we can remove templates from the require cache so we don't need to restart the server to see changes. Defaults to 'production' !== process.env.NODE_ENV.
    • removeCacheRegExp - a RegExp pattern string, matching modules in require cache will be removed. Defaults to undefined.
    • layout - the name of the layout file to use.
    • layoutPath - the directory path of where layouts are stored.
    • layoutRenderMethod - same as renderMethod but used for layouts. Defaults to renderToStaticMarkup.

You can override all these compileOptions at runtime.

const context = { name: 'Steve' };
const renderOpts = {
    runtimeOptions: {
        doctype: '<!DOCTYPE html>',
        renderMethod: 'renderToString'
    }
};

const output = await server.render('template', context, renderOpts);

Please refer to vision's docs on server.render(template, context, [options], callback) for complete details.

Examples

Before you can run the examples, you need to clone this repo and install the dependencies.

$ git clone https://github.com/jedireza/hapi-react-views.git
$ cd hapi-react-views
$ npm install

Rendering a simple page

This example renders a component as HTML output. View the code.

$ npm run simple-example

Rendering with layouts

Wrapper style layouts

This example renders components as HTML adding the idea of using wrapper layouts. The wrapping is handled by this module, so it may feel like a bit of magic since there is no direct dependency to the layout in your component views. View the code.

$ npm run layout-example

Component style layouts

This example renders components as HTML but adds the idea of using component layouts. The component layout is a direct dependency of your view components with no magic handling by this module. View the code.

$ npm run layout-component-example

Remounting on the client (universal/isomorphic)

This example demonstrates the idea of rendering the full page on the server and remounting the app view on the client side as a way to to create universal (aka isomorphic) applications.

It uses the wrapper layout feature, making it easy for the layout to be rendered without data-react-id attributes and the app view to be rendered with them. View the code.

$ npm run remount-example

License

MIT

Don't forget

What you create with hapi-react-views is more important than hapi-react-views.

More Repositories

1

drywall

🚧 Project moved, see Aqua and Frame
JavaScript
2,180
star
2

aqua

💡 A website and user system starter
JavaScript
1,377
star
3

frame

💡 A user system API starter
JavaScript
740
star
4

hapi-mongo-models

📦 A hapi plugin for `mongo-models`
JavaScript
100
star
5

generator-hapi-style

📦 Yeoman generator for scaffolding hapi apps and plugins
JavaScript
95
star
6

gimp-hidpi

A theme for HiDPI displays
78
star
7

mongo-models

📦 Map JavaScript classes to MongoDB collections
JavaScript
67
star
8

reserved-subdomains

📦 A list of names that should be reserved in multitenant apps.
JavaScript
46
star
9

warc

⚙️ A Rust library for reading and writing WARC files
Rust
44
star
10

hapi-node-postgres

📦 Wrap hapi requests with a pg connection
JavaScript
31
star
11

flux-store

📦 A simple store for Flux
JavaScript
21
star
12

flux-constant

📦 Unique constants for Flux apps
JavaScript
18
star
13

nodeschool

my nodeschool.io answers
JavaScript
13
star
14

mini-isomorphic-react

The world's smallest isomorphic React demo
JavaScript
6
star
15

projecteuler-js

My projecteuler.net solutions in JavaScript
JavaScript
4
star
16

vim-rizzle

A dark vim color scheme.
Vim Script
3
star
17

conways-yolo

Conway's Game of Life using HTML5 canvas
JavaScript
2
star
18

flatly

🚧 INACTIVE - A small, static website generator written in Node.js
JavaScript
2
star
19

flux-dispatcher

📦 A Flux dispatcher ready for action
JavaScript
2
star
20

hapi-remote-address

Provides `request.remoteAddress` with support for `X-Forwarded-For` headers.
JavaScript
2
star
21

sweep-or-die

A rendition of Minesweeper in JavaScript, HTML and CSS
JavaScript
2
star
22

connected-devices-sf

The SF chapter hub
HTML
1
star
23

j5-experiment

JavaScript
1
star
24

gh-identicon-parser

Parses a GitHub identicon into an array
JavaScript
1
star
25

host-header-trick

🚨 Demo code. Use at your own risk.
JavaScript
1
star
26

hello-watson

🚧 fiddle with IBM watson
JavaScript
1
star
27

dotxmonad

myxmonad settings
Haskell
1
star
28

weblegs-legacy-libs

🚧 INACTIVE - Weblegs libs for VB, C#, Java, Perl, PHP and JS
ASP
1
star
29

weblegs

🚧 INACTIVE - A very naive PHP framework
PHP
1
star
30

dotfiles

My dotfiles
Vim Script
1
star
31

projecteuler-rs

My projecteuler.net solutions in Rust
Rust
1
star
32

visionary

Views loader plugin for hapi.js
JavaScript
1
star
33

fxos-docker

A development environment for FxOS
Shell
1
star
34

joistick

📦 Default values for Joi schemas
JavaScript
1
star
35

bingo

JavaScript
1
star
36

ugly-assets

Watch, lint and build your .js and .less assets
JavaScript
1
star
37

drywall-screen-shots

Using CasperJS to generate Drywall demo screen shots
JavaScript
1
star
38

aqua-screenshots

Using CasperJS to generate Aqua demo screen shots
JavaScript
1
star
39

lab-es6

Demonstrating Lab & ES6
HTML
1
star