Skip to content
A webpack plugin to make it easy to statically render your React pages
JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
LICENSE
README.md
index.js
package-lock.json
package.json
render.js

README.md

React-Static-Render-Plugin

This webpack plugin makes it easy to statically render your React pages.

It generates HTML fragments, that can then be included in your server templates.

Install

npm install --save-dev @teamthread/react-static-render-plugin

or

yarn add -D @teamthread/react-static-render-plugin

Requirements

This plugin assumes that your app uses:

Usage

Change your React app entry from:

import React from "react";
import ReactDOM from "react-dom";
import { Router } from "react-router-dom";

ReactDOM.render(
  <Router>
    <MyApp />
  </Router>,
  document.getElementById("app")
);

to:

import React from "react";
import { render } from "@teamthread/react-static-render-plugin/render";

export default render(
  Router => (
    <Router>
      <MyApp />
    </Router>
  ),
  "app"
);

and you're done!

Now you can configure the plugin to statically render pages in your app. In your webpack config, add:

const StaticRenderPlugin = require("@teamthread/react-static-render-plugin");

module.exports = {
  plugins: [
    // ...
    new StaticRenderPlugin({
      pages: {
        index: {
          path: "/"
        },
        signin: {
          path: "/signin"
        }
      }
    })
    // ...
  ]
};

You should now see a index.html and signin.html in your output! These HTML fragments can now be included in your server-side templates.

App API

render

import { render } from "@teamthread/react-static-render-plugin";

The render method takes 2 arguments:

  • routerToApp - this should be a function that takes the Router component (dynamically passed in, since a different router is used at run-time vs. static-render time)
  • id - unlike ReactDOM.render() which has a second argument of a DOM element, the StaticRenderPlugin needs to know just the element's ID. This is because the plugin uses it to find the element in the page at run-time, but also uses it to generate the element, at static-render time.

process.env.STATIC_RENDER

The StaticRenderPlugin sets an environment variable process.env.STATIC_RENDER for the static-render build. This means that you can hide certain parts of the app, specifically for the static render.

For example:

<section className="user-data">
  <img
    src={
      process.env.STATIC_RENDER
        ? // we don't know what user this is at static-render time, so we
          // use a template at static-render time
          templatePhoto
        : photoOfUser
    }
  />
</section>

Plugin API

Pass these into the constructor, as an object:

new StaticRenderPlugin(options);

options.paths

Type: Array<Object>
Example: [{ a: { path: '/foo' }]

This mandatory field takes an array of path objects. Each path object must be the following shape:

{
    fileOutputName: {
        path: '/route-to-go-to',
        locals: {
            // an optional object that will be passed to the router context
            meaningOfLife: 42,
        }
    }
}

The fileOutputName will cause fileOutputName.html to be generated in the output. The path is what route to statically render.

The locals object is optional, but lets you pass through variables to your app, which can be accessed through the router context. For example:

<Route
  render={({ staticContext }) => (
    <div>The meaning of life is {staticContext.meaningOfLife}</div>
  )}
/>

options.output

Type: Object
Example: { path: './static-render' }

This lets you specify the output path, where all of the statically rendered HTML fragments will be output.

options.targetEntry

Type: String
Example: 'foo'

If you have multiple entries, then the StaticRenderPlugin needs to be told which entry to use for its static rendering.

For example, if your webpack config is:

module.exports = {
  entry: {
    foo: "./foo.js",
    bar: "./bar.js"
  }
};

then you can specify targetEntry: 'foo' or targetEntry: 'bar'

options.entry

Type: String
Example: './foo.js'

If you want to specify a custom entry just for the StaticRenderPlugin, then you can do so with this option.

options.subWebpackConfig

Type: Object
Example: { module: { rules: [staticRenderSpecificRule] } }

By default, StaticRenderPlugin uses your normal webpack config. If you want to use custom rules for the static render, then you can specify overrides here.

License

MIT

You can’t perform that action at this time.