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-portal-universal

v0.1.2

Published

Wrapper for React's createPortal allowing for rendering portals on the server

Downloads

5,008

Readme

React Portal Universal

React Portals Universal is a library providing a wrapper for React createPortal. The goal of the library is to render portals also on the server. React's DOM createPortal requires a DOM node which isn't suitable for the NodeJS environment.

Why?

Thanks to React Portal Universal you can now render portals on the server. But why would I like to do that in the first place? That's a great question!

  • Render elements into <head>. You can now manage your title, meta description or Open Graph meta data (Facebook doesn't run JavaScript) in the same way as you'd do that in react-helmet only you don't need a specialized library. Client-side of React Portal Universal is just under 1KB!
  • Aiming to make your page working also without JavaScript enabled.
  • If your JavaScript-powered components (e.g. modals) contain crucial information you would like to be easily indexed by different search engines.

Install

  npm install react-portal-universal

Usage

Render article's title and meta description into the <head>

// CLIENT

import { createUniversalPortal, removeUniversalPortals } from "react-portal-universal";

const Head = (props) => {
  const { children } = props;
  // pass selector for a document.querySelector
  // instead of a DOM node like in createPortal
  return createUniversalPortal(children, "head");
};

class App extends React.Component {
  render() {
    return (
      <article>
        <Head>
          <title>Hello, World!</title>
          <meta name="description" content="Lorem ipsum..." />
        </Head>
        <h1>Hello, World!</h1>
        <p>
          Lorem ipsum sit doloret um.
        </p>
      </article>
    );
  }
}

// remove static markup and allow React
// to render only actual components
removeUniversalPortals();

ReactDOM.render(<App />, document.querySelector("#root"));
// SERVER

const { appendUniversalPortals } = require("react-portal-universal/lib/server");

const body     = ReactDOMServer.renderToString(<App />));
const template = fs.readFileSync(path.resolve("build/index.html"), "utf8");
const html     = template.replace("<div id=\"root\"></div>", `<div id="root">${body}</div>`);
const markup   = appendUniversalPortals(html);

res.status(200).send(markup);

Configure

It is important to make sure that React application code is using the same instance of the library as code responsible for handling rendering on the server. In other words, there must be only one instance of the portals variable in the process. The problem occurs when you import appendUniversalPortals from node_modules on the server but use a bundle with its own instance to render an application.

The cleanest solution is to mark react-portal-universal as an external dependency in your bundler of choice. Here is how to do this in webpack.

const config = {
  externals: ["react-portal-universal"],
};