
Security News
Crates.io Implements Trusted Publishing Support
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
@teamthread/react-static-render-plugin
Advanced tools
A webpack plugin to make it easy to statically render your React pages
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.
npm install --save-dev @teamthread/react-static-render-plugin
or
yarn add -D @teamthread/react-static-render-plugin
This plugin assumes that your app uses:
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.
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>
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.
FAQs
A webpack plugin to make it easy to statically render your React pages
The npm package @teamthread/react-static-render-plugin receives a total of 0 weekly downloads. As such, @teamthread/react-static-render-plugin popularity was classified as not popular.
We found that @teamthread/react-static-render-plugin demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.