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

react-stasis

v0.1.0

Published

Allow to render a static subtree on the server that will not cause reconiliation problems on the client without appropriate data.

Downloads

9

Readme

react-stasis

Universal (isomorphic) rendering is one of the benefits of using React. Often times, however, a large portion of the page is static, so ideally you would want to to render it on the server, but not worry about it on the client side.

Unfortunately when React checks if the markup can be reused on the client side, it's currently impossible to tell it to ignore a certain subtree. That means that if you are unable to replicate DOM exactly, reconciliation will fail.

This is where react-stasis comes in. This package allows to you to only render part of the React tree on the server thus possibly reducing data sent to the client and the app initialization time.

In case you want some dynamic elements somewhere deep inside your static tree, react-stasis has you covered with a portal functionality

Basics

react-stasis provides a wrapper around react-dom APIs, so usually on the server side you just need to install react-stasis NPM package and then change react-dom/server import to react-stasis/server.

On the client side you also need change the import of react-dom to react-stasis.

In the React application itself, the only thing you need to do is to wrap a static subtree in a Static component provided by react-stasis and only include it's children on the server-side:

const {Component} = require('react');
const {Static, render} = require('react-stasis');

class MyApp extends Component {
    render() {
        return <div>
            <div>Some dynamic part of your app</div>
            <Static>
                {this.renderStatic()}
            </Static>
        </div>
    }
    renderStatic() {
        // this can be used for example by webpack
        // to exclude the statis part from the bundle
        // if you use https://webpack.github.io/docs/list-of-plugins.html#defineplugin
        if (process.env.TARGET === 'browser') {
            return null;
        } else {
            const MyStaticComponent = require('./my-static-component');
            return <MyStaticComponent someFoo="bar"/>;
        }
    }
}

Portals

Portals allow you to have some dynamic React components inside your sub tree. A good example would be a slider with images in a long page of text. Here's how it will roughly look like:

const {Component} = require('react');
const {Static, Portal, render} = require('react-stasis');

class MyApp extends Component {
    render() {
        return <div>
            <div>Some dynamic part of your app</div>
            <Static>
                {this.renderStatic(
                    <MySlider />
                )}
            </Static>
        </div>
    }
    renderStatic(slider) {
        // this can be used for example by webpack
        // to exclude the statis part from the bundle
        // if you use https://webpack.github.io/docs/list-of-plugins.html#defineplugin
        const portal =
            <Portal name="slider">
                <MySlider />
            </Portal>
        if (process.env.TARGET === 'browser') {
            return portal;
        } else {
            const MyStaticComponent = require('./my-static-component');
            return <MyStaticComponent someFoo="bar">
                {portal}
            </MyStaticComponent>;
        }
    }
}

The example above only assumes that MyStaticComponent can accept Slider as a child and knows where to place it in the content.

Fully Working Example

To see a fully-working example, but which is really bare bones (without a server-side framework or a bundler) you can take a look at the example folder.

You can also clone this repo and then run:

npm install
npm run example

and see how it works out in the browser.

You shouldn't use this example as a starting point for your React app — look at react-create-app instead.

Known issues

  • Using react-stasis just in the browser will result in an error.

  • Nested Static components are not supported at the moment and will result in an error in the browser.

  • Due to a difference in reported markup between browser-generated innerHTML and what ReactDOM.renderToStaticMarkup generates, react-stasis has to go a global replace /> to >, so if you have some html markup as text (static data) in your app, it might be affected. If you don't need to render, it's recommended to escape this markup beforehand.

Static component props

  • component: React.Component | string
  • props: object