npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@teamthread/react-static-render-plugin

v0.0.1

Published

A webpack plugin to make it easy to statically render your React pages

Downloads

6

Readme

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