Coder Social home page Coder Social logo

milesforks / react-portal-universal Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jesstelford/react-portal-universal

0.0 1.0 0.0 232 KB

Generic wrapper for React's createPortal allowing for rendering portals on the server

License: ISC License

TypeScript 94.70% JavaScript 5.30%

react-portal-universal's Introduction

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.

NOTE: This is a fork of react-portal-universal which fixes a critical bug encountered during concurrent renders.

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 @jesstelford/react-portal-universal

Usage

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

// CLIENT
import { UniversalPortal, prepareClientPortals } from  "@jesstelford/react-portal-universal";

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

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
prepareClientPortals();

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

const { ServerPortal } = require("@jesstelford/react-portal-universal/server");

const portals  = new ServerPortal();
const element  = portals.collectPortals(<App />);
const body     = ReactDOMServer.renderToString(element));
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   = portals.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. The problem occurs when you import ServerPortal from node_modules on the server but use a bundle with its own instance to render an application.

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

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

react-portal-universal's People

Contributors

michalzalecki avatar jesstelford avatar sanniassin avatar dutzi avatar jshbrntt avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.