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 theRouter
component (dynamically passed in, since a different router is used at run-time vs. static-render time) -
id
- unlikeReactDOM.render()
which has a second argument of a DOM element, theStaticRenderPlugin
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.