• Stars
    star
    155
  • Rank 240,864 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 9 years ago
  • Updated over 1 year ago

Reviews

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

Repository Details

A foundational React component useful for rendering full html documents on the server.

HTMLDocument

HTMLDocument

Version build status npm version

HTMLDocument is a foundational React component useful for rendering full html documents on the server.


You'll love HTMLDocument if:

  • You love (or would love) to use React to render full documents on the server without the need for templates, view engines, or static files.
  • You want to render documents in a way that makes it really easy to share state between server and client for hydrating client apps during mounting.

It provides a convenient and simple api for rendering common html tags such as title, meta, stylesheets, and scripts. In addition, it has universal/isomorphic-friendly features such as server state serialization, and support for static and non-static pages.

HTMLDocument is well tested and currently used in production on some of our web projects at Venmo.

Installation

To install the stable version:

npm install --save react-html-document

This assumes that youโ€™re using npm.

Examples

Basic static page

import HTMLDocument from 'react-html-document';
import ReactDOMServer from 'react-dom/server';

const doc = (
  <HTMLDocument title="My Page">
    <h1>Hello World</h1>
  </HTMLDocument>
);
ReactDOMServer.renderToStaticMarkup(doc);

Renders to:

<html>
  <head>
    <title>My Page</title>
  </head>
  <body>
    <div id="app">
      <h1>Hello World</h1>
    </div>
  </body>
</html>

Static page with scripts, stylesheets, and meta tags

import HTMLDocument from 'react-html-document';
import ReactDOMServer from 'react-dom/server';

const doc = (
  <HTMLDocument
    title="My Page"
    scripts={['/scripts/main.js']}
    stylesheets={['/styles/styles.css']}
    metatags={[
      { name: 'description', content: 'My description' }
    ]} >
    <div>My App</div>
  </HTMLDocument>
  );
ReactDOMServer.renderToStaticMarkup(doc);

Renders to:

<html>
  <head>
    <link rel="stylesheet" href="/styles/styles.css">
    <meta name="description" content="My Description">
    <title>My Page</title>
  </head>
  <body>
    <div id="app">
      <div>My App</div>
    </div>
    <script src="/scripts/main.js">
  </body>
</html>

Using Universal state

import HTMLDocument from 'react-html-document';
import ReactDOMServer from 'react-dom/server';

const state = { user: "X" };
const doc = (
  <HTMLDocument
    title="My Page with Universal State"
    universalState={state}>
  </HTMLDocument>
);
ReactDOMServer.renderToStaticMarkup(doc);

Renders to:

<html>
  <head>
    <title>My Page with Universal State</title>
  </head>
  <body>
    <div id="app"></div>
    <script id="__HTMLDOCUMENT__UNIVERSAL_STATE" type="application/json">
      { user: "X" }
    </script>
  </body>
</html>

HTMLDocument also provides a function to make this even easier:

// from the client
import { getUniversalState } from 'react-html-document';

const state = getUniversalState(); // { user: "X"}

API

Prop Type Details Default
title string Title for the document. ''
metatags array A list of meta tag attributes. [ ]
scripts array A list of scripts in one of three forms: string paths 'mysite.com/script.js', script src objects { src: 'mysite.com/script.js' } or inline scripts { inline: 'var x = 1;' } [ ]
stylesheets array A list of stylesheet in one of three forms: string paths 'mysite.com/styles.css', style href objects { href: 'mysite.com/styles.css' } or inline styles { inline: 'body { color: '#333' }' } [ ]
universalState object Contains current server state that will be rendered into a script tag of type application/json on the page. Helpful for re-mounting with props on the client in universal apps. When not using it, children will be rendered statically. null
childrenContainerId string The id for the dom element that contains the children nodes. 'app'
favicon string URL to your favicon. ''
htmlAttributes object Attributes that you'd like to use on the html tag. { }

Development

Please take a look at package.json for available npm scripts.

For starting a dev server: npm run dev

For running mocha tests: npm test

For compiling src directory into dist directory with babel: npm run build

For linting with eslint: npm run lint

Contributing

We'd love for you to contribute.

Please open PRs from your fork to master. Keep in mind that we're using the eslint linter and the airbnb configuration for it. Rebase and squash when appropriate.

Versioning

This project adheres to Semantic Versioning.

License

MIT

More Repositories

1

synx

A command-line tool that reorganizes your Xcode project folder to match your Xcode groups
Ruby
6,082
star
2

Static

Simple static table views for iOS in Swift.
Swift
1,250
star
3

VENTouchLock

A Touch ID and Passcode framework used in the Venmo app.
Objective-C
965
star
4

business-rules

Python DSL for setting up business intelligence rules that can be configured without code
Python
891
star
5

VENTokenField

Easy-to-use token field that is used in the Venmo app.
Objective-C
793
star
6

VENCalculatorInputView

Calculator keyboard used in the Venmo iOS app
Objective-C
763
star
7

DVR

Network testing for Swift
Swift
651
star
8

tooltip-view

Dead simple Android Tooltip Views
Java
486
star
9

DryDock-iOS

DEPRECATED: An open-source internal installer app
433
star
10

VENSeparatorView

Jagged border separators on UIViews used in the Venmo app.
Objective-C
379
star
11

VENVersionTracker

Objective-C
300
star
12

business-rules-ui

A JavaScript library for building out the logic and UI for business rules.
JavaScript
197
star
13

VENPromotionsManager

iOS Library to perform location & time-based promotions.
Objective-C
197
star
14

cursor-utils

A library that encapsulates the repeatable actions of Android Cursors.
Java
195
star
15

venmo-ios-sdk

Make and accept payments in your iOS app via Venmo
Objective-C
176
star
16

VENExperimentsManager

An Objective-C library enabling easy implementation of feature experiments on iOS allowing users to opt in and out of experiments at will.
Objective-C
76
star
17

slouch

A lightweight Python framework for building cli-inspired Slack bots.
Python
71
star
18

xcode_server

Xcode Bot client
Ruby
58
star
19

android-pin

An easy drop-in PIN controller for Android
Java
42
star
20

app-switch-android

Java
35
star
21

VENCore

Core Objective-C client library for the Venmo API
Objective-C
27
star
22

QuizTrain

Swift Framework for TestRail's API
Swift
19
star
23

feature_ramp

Toggling and ramping features via a lightweight Redis backend.
Python
18
star
24

btnamespace

A Python library to isolate state on the Braintree sandbox during testing.
Python
17
star
25

flaskeleton

Python
13
star
26

swaggergenerator

Create swagger / OpenAPI schemas from example interactions.
Python
11
star
27

tornado-stub-client

A stub library for making requests with tornado's AsyncHTTPClient
Python
9
star
28

venmo.github.io

Old Venmo Engineering Blog
CSS
8
star
29

nose-detecthttp

A nose plugin that can detect tests making external http calls.
Python
7
star
30

puppet-consulr

Dynamic puppet manifests using consul
Ruby
5
star
31

puppet-sentry

Puppet module for managing the Sentry realtime event logging and aggregation platform
Ruby
4
star
32

python3-avro

Copies the python3 client implementation from our fork of apache's avro project.
Python
4
star
33

nose-seed-faker

A nose plugin that seeds the faker package
Python
4
star
34

QuizTrainExample

Example of how to use QuizTrain with Unit Tests and UI Tests on iOS
Swift
3
star
35

single-click-highlightable

HOC for React that allows users to highlight text on elements without triggering the onClick handler
JavaScript
3
star
36

puppet-hound

Puppet hound module
Puppet
2
star
37

nose-timeout

Nose plugin for aborting long running tests
Python
1
star
38

foundations-interview

This is a test repository used in Foundations team interview process
JavaScript
1
star