Giter Site home page Giter Site logo

rapscallion's Introduction

Rapscallion

CircleCI Join the chat at https://gitter.im/FormidableLabs/rapscallion

Overview

Rapscallion is a React VirtualDOM renderer for the server. Its notable features are as follows:

  • Rendering is asynchronous and non-blocking.
  • Rapscallion is roughly 30% faster than renderToString.
  • It provides a streaming interface so that you can start sending content to the client immediately.
  • It provides a templating feature, so that you can wrap your component's HTML in boilerplate without giving up benefits of streaming.
  • It provides a component caching API to further speed-up your rendering.

Table of Contents

Installation

Using npm:

$ npm install --save rapscallion

In Node.js:

const {
  render,
  template
} = require("rapscallion");

// ...

API

render

render(VirtualDomNode) -> Renderer

This function returns a Renderer, an interface for rendering your VirtualDOM element. Methods are enumerated below.


Renderer#toPromise

renderer.toPromise() -> Promise<String>

This function evaluates the React VirtualDOM Element originally provided to the renderer, and returns a Promise that resolves to the component's evaluated HTML string.

Example:

render(<MyComponent {...props} />)
  .toPromise()
  .then(htmlString => console.log(htmlString));

Renderer#toStream

renderer.toStream() -> NodeStream<StringSegment>

This function evaluates a React VirtualDOM Element, and returns a Node stream. This stream will emit string segments of HTML as the DOM tree is asynchronously traversed and evaluated.

In addition to the normal API for Node streams, the returned stream object has a checksum method. When invoked, this will return the checksum that has been calculated up to this point for the stream. If the stream has ended, the checksum will be the same as would be included by React.renderToString.

Example:

app.get('/example', function(req, res){
  render(<MyComponent prop="stuff" />)
    .toStream()
    .pipe(res);
});

Renderer#includeDataReactAttrs

renderer.includeDataReactAttrs(Boolean) -> undefined

This allows you to set whether you'd like to include properties like data-reactid in your rendered markup.


Renderer#tuneAsynchronicity

renderer.tuneAsynchronicity(PositiveInteger) -> undefined

Rapscallion allows you to tune the asynchronicity of your renders. By default, rapscallion batches events in your stream of HTML segments. These batches are processed in a synchronous-like way. This gives you the benefits of asynchronous rendering without losing too much synchronous rendering performance.

The default value is 100, which means the Rapscallion will process one hundred segments of HTML text before giving control back to the event loop.

You may want to change this number if your server is under heavy load. Possible values are the set of all positive integers. Lower numbers will be "more asynchronous" (shorter periods between I/O processing) and higher numbers will be "more synchronous" (higher performance).


Renderer#checksum

renderer.checksum() -> Integer

In a synchronous rendering environment, the generated markup's checksum would be calculated after all generation has completed. It would then be attached to the start of the HTML string before being sent to the client.

However, in the case of a stream, the checksum is only known once all markup is generated, and the first bits of HTML are already on their way to the client by then.

The renderer's checksum method will give you access to the checksum that has been calculated up to this point. If the rendered has completed generating all markup for the provided component, this value will be identical to that provided by React's renderToString function.

For an example of how to attach this value to the DOM on the client side, see the example in the template section below.


setCacheStrategy

setCacheStrategy({ get: ..., set: ... */ }) -> undefined

The default cache strategy provided by Rapscallion is a naive one. It is synchronous and in-memory, with no cache invalidation or TTL for cache entries.

However, setCacheStrategy is provided to allow you to integrate your own caching solutions. The function expects an options argument with two keys:

  • get should accept a single argument, the key, and return a Promise resolving to a cached value. If no cached value is found, the Promise should resolve to null.
  • set should accept two arguments, a key and its value, and return a Promise that resolves when the set operation has completed.

All values, both those returned from get and passed to set, will be Arrays with both string and integer elements. Keep that in mind if you need to serialize the data for your cache backend.

Example:

const { setCacheStrategy } = require("rapscallion");
const redis = require("redis");

const client = redis.createClient();
const redisGet = Promise.promisify(redisClient.get, { context: redisClient });
const redisSet = Promise.promisify(redisClient.set, { context: redisClient });
setCacheStrategy({
  get: key => redisGet(key).then(val => val && JSON.parse(val) || null),
  set: (key, val) => redisSet(key, JSON.stringify(val))
});

For more information on how to cache your component HTML, read through the caching section below.


template

template`TEMPLATE LITERAL` -> Renderer

With React's default renderToString, it is a common pattern to define a function that takes the rendered output and inserts it into some HTML boilerplate; <html> tags and the like.

Rapscallion allows you to stream the rendered content of your components as they are generated. However, this makes it somewhat less simple to wrap that component in your HTML boilerplate.

Fortunately, Rapscallion provides rendering templates. They look very similar to normal template strings, except that you'll prepend it with template as a template-literal tag.

Valid expressions

Like string templates, rendering templates allow you to insert expressions of various types. The following expression types are allowed:

  • string: any expression that evaluates to a string, i.e. template`<html>${ "my string" }</html>`
  • vdom: any React VirtualDOM object, i.e. template`<html>${ <MyComponent /> }</html>
  • Renderer: any Renderer instance, i.e. template `<html>${ render(<div />) }</html>`
  • function: any function that, when invoked, evaluates to one of the other valid expression types, i.e. template`<html>${ () => "my string" }</html>`

One important thing to note is that a rendering template returns a Renderer instance when evaluated. This means that templates can be composed like so:

const myComponent = template`
<div>
  ${ <MyComponent /> }
</div>
`;

const html = template`
<html>
${ <MyHeader /> }
<body>
  ${ myComponent }
</body>
</html>
`;

Behavior

To utilize rendering templates effectively, it will be important to understand their following three properties:

  1. template segments are evaluated asynchronously;
  2. template segments are evaluated in order; and
  3. template segments are evaluated lazily, as they are consumed.

These properties are actually true of all Renderers. However, they present potential pitfalls in the more complex situations that templates often represent. The asynchronicity is the easiest of the three properties to understand, so not much time will be spent on that. It is the lazy orderedness that can introduce interesting ramifications.

Here are a handful of consequences of these properties that might not be readily apparent:

  • You cannot instantiate a component, pass it a store, and immediately pull out an updated state from the store. You have to wait until after the component is fully rendered before any side-effects of that rendering occur.
  • The same is true of checksums. You can't get a checksum of a component that hasn't been rendered yet.
  • If an error occurs half-way through a render, and you are streaming content to the user, it is too late to send a 404 - because you've already sent a 200. You'll have to find other ways to present error conditions to the client.

However, these properties also allow the computation cost to be spread across the lifetime of the render, and ultimately make things like asynchronous rendering possible.

Example

All of this may be somewhat unclear in the abstract, so here's a fuller example:

import { render, template } from "rapscallion";

// ...

app.get('/example', function(req, res){
  // ...

  const store = createStore(/* ... */);
  const componentRenderer = render(<MyComponent store={store} />);

  const responseRenderer = template`
    <html>
    <body>
      ${componentRenderer}
      ${
        <MyOtherComponent />
      }
      <script>
        // Expose initial state to client store bootstrap code.
        window._initialState = ${() => JSON.stringify(store.getState()).replace(/</g, '\\u003c')};
        // Attach checksum to the component's root element.
        document.querySelector("#id-for-component-root").setAttribute("data-react-checksum", "${() => componentRenderer.checksum()}")
        // Bootstrap your application here...
      </script>
    </body>
    </html>
  `;

  responseRenderer.toStream().pipe(res);
});

Note that the template comprises a stream of HTML text (componentRenderer), the HTML from a second component (MyOtherComponent), and a function that evaluates to the store's state - something you'll often want to do with SSR.

Additionally, we attach the checksum to the rendered component's DOM element on the client side.


Caching

Caching is performed on a per-component level, is completely opt-in, and should be used judiciously. The gist is this: you define a cacheKey prop on your component, and that component will only be rendered once for that particular key. cacheKey can be set on both React components and html React elements.

If you cache components that change often, this will result in slower performance. But if you're careful to cache only those components for which 1) a cacheKey is easy to compute, and 2) will have a small set of keys (i.e. the props don't change often), you can see considerable performance improvements.

Example:

const Child = ({ val }) => (
  <div>
    ComponentA
  </div>
);

const Parent = ({ toVal }) => (
  <div cacheKey={ `Parent:${toVal}` }>
    {
      _.range(toVal).map(val => (
        <Child cacheKey={ `Child:${val}` } key={val} />
      ))
    }
  </div>
);

Promise.resolve()
  // The first render will take the expected duration.
  .then(() => render(<Parent toVal={5} />).toPromise())
  // The second render will be much faster, due to multiple cache hits.
  .then(() => render(<Parent toVal={6} />).toPromise())
  // The third render will be near-instantaneous, due to a top-level cache hit.
  .then(() => render(<Parent toVal={6} />).toPromise());

Babel Plugins

Rapscallion ships with two Babel plugins, one intended for your server build and one for your client build. Each serves a different purpose.

babel-plugin-client

When running in development mode, ReactDOM.render checks the DOM elements you define for any invalid HTML attributes. When found, a warning is issued in the console.

If you're utilizing Rapscallion's caching mechanisms, you will see warnings for the cacheKey props that you define on your elements. Additionally, these properties are completely useless on the client, since they're only utilized during SSR.

Rapscallion's client plugin will strip cacheKey props from your build, avoiding the errors and removing unnecessary bits from your client build.

To use, add the following to your .babelrc:

{
  "plugins": [
    "rapscallion/babel-plugin-client",
    // ...
  ]
}

babel-plugin-server

In typical scenarios, developers will use the babel-plugin-transform-react-jsx plugin to transform their JSX into React.createElement calls. However, these createElement function calls involve run-time overhead that is ultimately unnecessary for SSR.

Rapscallion's server plugin is provided as a more efficient alternative. It provides two primary benefits:

Efficient VDOM data-structure: Instead of transforming JSX into React.createElement calls, Rapscallion's server plugin transforms JSX into a simple object/array data-structure. This data-structure is more efficient to traverse and avoids extraneous function invocations.

Pre-rendering: Rapscallion's server plugin also attempts to pre-render as much content as possible. For example, if your component always starts with a <div>, that fact can be determined at build-time. Transforming JSX into these pre-computed string segments avoids computation cost at run-time, and in some cases can make for a more shallow VDOM tree.

To be clear, rapscallion/babel-plugin-server should be used in place of babel-plugin-transform-react-jsx.

To use, add the following to your .babelrc:

{
  "plugins": [
    "rapscallion/babel-plugin-server",
    // ...
  ]
}

The plugin also supports Rapscallion-aware JSX hoisting. This may improve performance, but may also hurt. We recommend you profile your application's rendering behavior to determine whether to enable hoisting. To use:

{
  "plugins": [
    ["rapscallion/babel-plugin-server", {
      "hoist": true
    }]
  ]
}

Benchmarks

The below benchmarks do not represent a typical use-case. Instead, they represent the absolute best case scenario for component caching.

However, you'll note that even without caching, a concurrent workload will be processed almost 50% faster, without any of the blocking!

Starting benchmark for 10 concurrent render operations...
renderToString took 9.639041541 seconds
rapscallion, no caching took 9.168861890 seconds; ~1.05x faster
rapscallion, caching DIVs took 3.830723252 seconds; ~2.51x faster
rapscallion, caching DIVs (second time) took 3.004709954 seconds; ~3.2x faster
rapscallion, caching Components took 3.088687965 seconds; ~3.12x faster
rapscallion, caching Components (second time) took 2.484650701 seconds; ~3.87x faster
rapscallion (pre-rendered), no caching took 7.423578183 seconds; ~1.29x faster
rapscallion (pre-rendered), caching DIVs took 3.202458180 seconds; ~3x faster
rapscallion (pre-rendered), caching DIVs (second time) took 2.671346947 seconds; ~3.6x faster
rapscallion (pre-rendered), caching Components took 2.578935599 seconds; ~3.73x faster
rapscallion (pre-rendered), caching Components (second time) took 2.470472298 seconds; ~3.9x faster

License

MIT License

Maintenance Status

Archived: This project is no longer maintained by Formidable. We are no longer responding to issues or pull requests unless they relate to security concerns. We encourage interested developers to fork this project and make it their own!

rapscallion's People

Contributors

aweary avatar bmathews avatar cullenjett avatar dittos avatar divmain avatar ethersage avatar exogen avatar fleg avatar getkey avatar jaredpalmer avatar jkaminetsky avatar jnoody avatar jpdriver avatar mini-eggs avatar moklick avatar nyobe avatar pwmckenna avatar wmertens avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rapscallion's Issues

Error thrown when using react-redux Provider

Error thrown:
"Uncaught Error: React.Children.only expected to receive a single React element child."
From debugging I could see the provider didn't get any children. Not sure why.

minimal reproduction code:

import React, { Component } from 'react';
import { render } from 'rapscallion/lib'
import { Provider } from 'react-redux';
class App extends Component {
    render() {
        return (
            <Provider>
                <div className="App" >
                hello joe
                </div>
            </Provider>
        );
    }
}
render(<App />).toPromise().then((html) => {
    console.log(html);
});

.babelrc

 {
    "presets": [
      "env"
    ],
    "plugins": [
      "rapscallion/babel-plugin-server"
    ]
  }

Possibility to return undefined from template expression

I'm using rapscallion and am using helmet to render a title and meta description (see here):

        <div id="app">${appRenderer}</div>
          // ...
          // Set title and meta description
          ${() => {
            helmet = Helmet.renderStatic()
            return ''
          }}
          appendToHead('${() => helmet.title.toString()}')
          appendToHead('${() => helmet.meta.toString()}')

Now helmet needs to be instantiated after rendering the app, but since rapscallion throws an error when returning undefined from an expression* I'm just returning an empty string. This isn't really a big deal and the current solution works fine.

But, I thought it might be nice to allow expressions that evaluate to undefined, which would then just insert nothing. No idea if that's possible or even desirable, but it would allow me to omit the return ''.

*: throw new Error("Unknown value in template of type " + (typeof segment === "undefined" ? "undefined" : _typeof(segment)) + ": " + segment);

Getting Error: not implemented

Great library! I'm trying to use this with Express, but can't seem to get it to work.
I'm getting this error;

Error: not implemented
    at Readable._read (_stream_readable.js:470:22)
    at Readable.read (_stream_readable.js:348:10)
    at resume_ (_stream_readable.js:737:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)

This is my render function;

const renderApp = (req, res) => {
    const context = {};

    const application = renderToStream(
        <StaticRouter location={req.url} context={context}>
            <App />
        </StaticRouter>
    );

    const html = streamTemplate`<!doctype html><html>
      <head>
        <title>Test</title>
      </head>
      <body>
        <div id="root">${application}</div>
        ${process.env.NODE_ENV === 'production' ? '<script type="text/javascript" src="/assets/vendor.js"></script>' : ''}
        <script src="/assets/app.js"></script>
      </body>
    </html>`

    if (context.url) {
        res.writeHead(302, { Location: context.url });
        res.end();
    } else {
        toNodeStream(html).pipe(res)
    }

};

Any idea as to what's going wrong here?

babel-plugin-server: prop without value

babel-plugin-server throws an error on a prop without value.

A minimum test case:

$ echo '<Foo isFoo />;' | ./node_modules/.bin/babel --plugins ./src/transform/server
TypeError: unknown: Property value of ObjectProperty expected node to be of a type ["Expression"] but instead got null
    at Object.validate (/Users/shuhei/work/js/rapscallion/node_modules/babel-types/lib/definitions/index.js:109:13)
    at validate (/Users/shuhei/work/js/rapscallion/node_modules/babel-types/lib/index.js:505:9)
    at Object.builder (/Users/shuhei/work/js/rapscallion/node_modules/babel-types/lib/index.js:466:7)
    at attributes.map.attr (/Users/shuhei/work/js/rapscallion/src/transform/server.js:97:14)
    at Array.map (native)
    at getComponentProps (/Users/shuhei/work/js/rapscallion/src/transform/server.js:91:14)
    at prerenderComponent (/Users/shuhei/work/js/rapscallion/src/transform/server.js:67:31)
    at PluginPass.enter (/Users/shuhei/work/js/rapscallion/src/transform/server.js:21:13)
    at newFn (/Users/shuhei/work/js/rapscallion/node_modules/babel-traverse/lib/visitors.js:276:21)
    at NodePath._call (/Users/shuhei/work/js/rapscallion/node_modules/babel-traverse/lib/path/context.js:76:18)

API Discussion: Why isn't template just a render function?

First off, congratulations on your launch! Nice job. 🚀🌈🔥
Also: thanks for having good docs!

It's great that you included static templates, but I'm a little confused about your choice to use template strings rather than just making another render function with plain old React elements. It seems to me that using template strings requires the developer to learn a second API (in addition to render), but that's not really required. If instead you made it a plain old function (called, say, `renderTemplate``), I think Rapscallion might have a more compact, learnable API.

To get a sense for what this would look like, the example in the template section would become:

import { render, renderTemplate } from "rapscallion";

// ...

app.get('/example', function(req, res){
  // ...

  const store = createStore(/* ... */);
  const componentRenderer = render(<MyComponent store={store} />);

  const responseRenderer = renderTemplate (
    <html>
    <body>
      {componentRenderer}
      <MyOtherComponent />
      <script>
        // Expose initial state to client store bootstrap code.
        window._initialState = {JSON.stringify(store.getState())};
        // Attach checksum to the component's root element.
        document.querySelector("#id-for-component-root").setAttribute("data-react-checksum", "{componentRenderer.checksum()}")
        // Bootstrap your application here...
      </script>
    </body>
    </html>
  );

  responseRenderer.toStream().pipe(res);
});

This also gets rid of the need to support callbacks in the templated strings, I think. Thoughts?

Differences in escaped/encoded text between React and Rapscallion

This test fails due to differences in encoding charcode 39 between React and Rapscallion.

describe("text encoding", () => {
  const Component = () => <div>{"<script type='' src=\"\"></script>"}</div>;
  checkParity(Component, {});
});

We can either pull in react-dom/lib/escapeTextContentForBrowser as a dep and use it directly, or duplicate/copy the changes necessary for parity.

https://github.com/facebook/react/blob/master/src/renderers/dom/shared/escapeTextContentForBrowser.js

Add parity test suite for ReactDOMServer

It's important that rapscallion produce output consistent with ReactDOMServer.renderToString. We should add a suite of tests that renders a number of different types of components and layouts and verify that the output matches.

"TypeError: Cannot read property 'createHref' of undefined" if used with React Router

If used with React Router I'm getting

TypeError: Cannot read property 'createHref' of undefined

App.js:

import React, { Component } from 'react'
import { Route } from 'react-router-dom'
import Home from './Home'

export default class App extends Component {
	render() {
		return (
			<div>
				<Route path="/" component={Home} />
			</div>
		)
	}
}

Home.js:

import React, {Component} from 'react'

export default class Home extends Component {
	constructor(props) {
		super(props)
	}
	render() {
		return (
			<div className="home">Test</div>
		)
	}
}

Server:

require('babel-register');
require('babel-polyfill');
import express from "express";
import path from "path";
import App from './App';
import React from 'react';
import { renderToStream, toNodeStream, streamTemplate } from "rapscallion";
import { StaticRouter } from 'react-router-dom';

const app = express();

app.use("/", (req, res) => {

    const context = {};

    const application = renderToStream(
        <StaticRouter location={req.url} context={context}>
            <App />
        </StaticRouter>
    );

    const html = streamTemplate`<!doctype html><html>
      <head>
        <title>Test</title>
      </head>
      <body>
        <div id="root">${application}</div>
      </body>
    </html>`

    if (context.url) {
        res.writeHead(302, { Location: context.url });
        res.end();
    } else {
        toNodeStream(html).pipe(res)
    }

});

app.listen(3000, () => {
    console.log("App listening on port 3000");
});

babel-plugin-server: non-identifier expression as JSX element name

babel-plugin-server prerenders <undefined><undefined> given a non-identifier expression as JSX element name.

A minimum test case:

$ echo '<Foo.Bar />;' | ./node_modules/.bin/babel --plugins ./src/transform/server
({
  __prerendered__: "dom",
  segments: ["<undefined", 1, "></undefined>"]
});

babel-plugin-server: spread attributes

babel-plugin-server throws an error on spread attributes.

A minimum test case:

$ echo '<Foo {...props} />;' | ./node_modules/.bin/babel --plugins ./src/transform/server
TypeError: unknown: Cannot read property 'name' of undefined
    at objExpr.properties.forEach.property (/Users/shuhei/work/js/rapscallion/src/transform/server.js:198:21)
    at Array.forEach (native)
    at objectExpressionToObject (/Users/shuhei/work/js/rapscallion/src/transform/server.js:197:22)
    at PluginPass.exit (/Users/shuhei/work/js/rapscallion/src/transform/server.js:27:21)
    at newFn (/Users/shuhei/work/js/rapscallion/node_modules/babel-traverse/lib/visitors.js:276:21)
    at NodePath._call (/Users/shuhei/work/js/rapscallion/node_modules/babel-traverse/lib/path/context.js:76:18)
    at NodePath.call (/Users/shuhei/work/js/rapscallion/node_modules/babel-traverse/lib/path/context.js:48:17)
    at NodePath.visit (/Users/shuhei/work/js/rapscallion/node_modules/babel-traverse/lib/path/context.js:117:8)
    at TraversalContext.visitQueue (/Users/shuhei/work/js/rapscallion/node_modules/babel-traverse/lib/context.js:150:16)
    at TraversalContext.visitSingle (/Users/shuhei/work/js/rapscallion/node_modules/babel-traverse/lib/context.js:108:19)

Downgrade npm engines

  "engines": {
    "node": ">=6.0.0"
  },

should be downgraded now that we transpile on npm publishing for additional node support.

throw new TypeError(`Unknown node of type: ${node.type}`);

Probably me doing something wrong, but I'm getting this error now;

/Users/user/project/node_modules/rapscallion/src/render/traverse.js:212

throw new TypeError(`Unknown node of type: ${node.type}`);TypeError: Unknown node of type: undefined

Cache TTL and/or invalidation

In many cases, we will want to control the growth of the component render cache. One option for cache-invalidation is to introduce a sibling prop to cacheKey: cacheTTL. When provided, this would invoke a function that clears the particular cache entry with setTimeout.

There may be other ways to go about this also... a FIFO array that tracks all cache entries might work for some use cases.

TypeError: frameIterator.patch is not a function

When using setCacheStrategy on a memcached-backed store that returns promises I'm getting the following error:

node_modules/rapscallion/lib/sequence/sequence.js:49
      delegate();
      ^
TypeError: frameIterator.patch is not a function

config:

setCacheStrategy({
  get: (key) => cacheStore.get(key),
  set: (key, val) => cacheStore.set(key, val)
});

Adapter source is here but it is relatively simple (get/set returns promises and gets are batched).

Reliability

Howdy, is this library ready for prime time? Is it somehow battle tested?

I've project with about 500k unique visitors daily and 50-200 req/s all the time, so SSR performance and component level cache is crucial. Redis caching approach I came up with works but it's far from perfect. I know I won't have time to improve it soon enough, so I'm hoping for solution from open source scene.

Issue with <select> tags - <select value=""> is not moved to <option selected>

With ReactDOM, this:

let locales = Object.keys(locales).map((key) =>
      <option key={"locale-" + key} value={ key }>{ locales[key].name }</option>
);

<select className="unstyled" value={ this.props.locale} onChange={this._changeLocale}>
     { locales }
</select>

gets rendered into

<select class="unstyled" data-reactid="106">
<option value="de" data-reactid="107">Deutsch</option>
<option value="ru" data-reactid="108">Русский</option>
<option value="sv" data-reactid="109">Svenska</option>
<option selected="" value="en" data-reactid="110">English</option>
<option value="it" data-reactid="111">Italiano</option>
<option value="fr" data-reactid="112">Français</option>
<option value="es" data-reactid="113">Español</option>
<option value="uk" data-reactid="114">Українськa</option>
<option value="ro" data-reactid="115">Român</option>
<option value="nl" data-reactid="116">Nederlandse</option>
</select>

while with rapscallion the value is not moved to options:

<select class="unstyled" value="en" data-reactid="106">
<option value="de" data-reactid="107">Deutsch</option>
<option value="ru" data-reactid="108">Русский</option>
<option value="sv" data-reactid="109">Svenska</option>
<option value="en" data-reactid="110">English</option>
<option value="it" data-reactid="111">Italiano</option>
<option value="fr" data-reactid="112">Français</option>
<option value="es" data-reactid="113">Español</option>
<option value="uk" data-reactid="114">Українськa</option>
<option value="ro" data-reactid="115">Român</option>
<option value="nl" data-reactid="116">Nederlandse</option>
</select>

The React docs say we should use value on select.

This triggers the dreaded warning:

warning.js:36 Warning: React attempted to reuse markup in a container but the checksum was invalid. This generally means that you are using server rendering and the markup generated on the server was not what the client was expecting. React injected new markup to compensate which works but you have lost many of the benefits of server rendering. Instead, figure out why the markup being generated is different on the client or server:
 (client) ct class="unstyled" data-reactid="82"><o
 (server) ct class="unstyled" value="en" data-reac

Yarn ./src directory

I have installed this library with yarn add rapscallion

Then when I try to use it I get

[0]     ERROR in ./~/rapscallion/index.js
[0]     Module not found: Error: Can't resolve './src' in '/srv/eat/node_modules/rapscallion'
[0]      @ ./~/rapscallion/index.js 5:19-35
[0]      @ ./src/middleware.tsx
[0]      @ multi ./src/middleware
[0] webpack: bundle is now VALID.

Because of thess lines
My webpack config get mad about the fact that yarn does not bring the src directory and fails because cannot find it. How can I deal with these? removing the try/catch block solves the issue module.exports = require("./lib");

Component caching

If a component specifies a cacheKey prop:

  1. consume all string segments from its virtual DOM subtree,
  2. stash the concatenated segments in a hash-map, keyed by the cacheKey,
  3. emit the cached value on subsequent requests for the same cacheKey,
  4. don't render cacheKey.

It might also be worth writing a Babel plugin that strips out cacheKey props, so that they won't be rendered to the DOM on the client.

stream chunk error

            <script dangerouslySetInnerHTML={{__html:[] }} type="text/javascript" />

This code (which is borderline invalid, but doesn't cause problems with renderToString), triggers the following:

     Uncaught TypeError: Invalid non-string/buffer chunk
      at chunkInvalid (_stream_readable.js:393:10)
      at readableAddChunk (_stream_readable.js:148:12)
      at Readable.push (_stream_readable.js:134:10)
      at pullBatch (node_modules/rapscallion/lib/consumers/common.js:30:14)
      at Readable.read [as _read] (node_modules/rapscallion/lib/consumers/node-stream.js:32:18)
      at Readable.read (_stream_readable.js:348:10)
      at resume_ (_stream_readable.js:737:12)
      at wrapped (node_modules/newrelic/lib/transaction/tracer/index.js:184:28)
      at _combinedTickCallback (internal/process/next_tick.js:74:11)
      at process._tickDomainCallback [as _tickCallback] (internal/process/next_tick.js:122:9)

renderToString just throws the array away and renders <script type="text/javascript"></script>

Boolean attributes are not rendered

Consider this React component:

<video autoPlay />

The expected output when rendering is:

<video autoplay="" />

But Rapscallion actually returns

<video />

Note that the autoplay attribute is gone.

After quickly looking at the code, it seems like https://github.com/FormidableLabs/rapscallion/blob/master/src/render/attrs/index.js#L31 is responsible. If the value is true Object.keys(attrVal).length is indeed 0 but it is still a perfectly valid attribute. Checking if (attrVal === true) afterwards if then useless as it would have called continue previously, and the value is never inserted.

This is also illustrated by the failing test for attributes in the pull request #58 . If you look at the CI result here you will see many tests prop with no value failing for that reason.
I am not quite sure of the purpose of the Object.keys(attrVal).length check so I did not submit a pull request because I am unsure of the best way to fix it, but it should be an easy fix in any case.

Problem reading checksum, not a stream?

In the example, It is implied that with the following code, one should be able to set the react checksum.

const componentRenderer = render(<MyComponent store={store} />);

template`
  document.querySelector("#id-for-component-root").setAttribute("data-react-checksum", "${componentRenderer.checksum()}")
`.

For me, this leads to an error: Renderer#checksum can only be invoked for a renderer converted to node stream..

This seems to be an inconsistency with the readme. If I append toStream() to componentRenderer, I get another error:

Unknown value in template of type object: [object Object] at getSequenceEvent (/Users/…node_modules/rapscallion/lib/template.js:33:11).

If I get the checksum by doing componentRenderer.toStream().checksum() it ends up being empty "".

Is there something I'm doing wrong? or is this an issue with rapscallion or its documentation/

Tagged template literals --> Stream

When server-side rendering, most users will not want to send the exact output of a virtual-DOM render. For example, you might want to wrap it in <html> ... </html>, and you may want to inject your initial state into the DOM.

Often, developers will accomplish this by rendering to string, and then inserting that string into a template literal. Unfortunately, this pattern can become somewhat unwieldy since you're wanting to stream HTML data as it becomes available.

Fortunately, you can have the best of both worlds, via tagged template literals.

This library should export a helper function (nodeStreamTemplate) that transforms a template with embedded streams into one large stream.

Example:

import ssrAsync from "react-ssr-async";

// ...

app.get('/example', function(req, res){
  // ...

  const store = createStore(/* ... */);

  const componentHtml = ssrAsync.asNodeStream(<MyComponent store={store} />);
  const responseStream = ssrAsync.nodeStreamTemplate`
    <html>
    <body>
      ${componentHtml}
      <script>
        window._initialState = ${() => store.getState()};
      </script>
    </body>
    </html>
  `;

  responseStream.pipe(res);
});

Implement data-fetching for server rendering

This is more of a question, but would it be possible to implement something like this into Rapscallion? https://github.com/rexhome7326/sync-will-mount

Tested it and it works, lets you await a promise in ComponentWillMount() before continuing render.
With React Router 4's new API data fetching is a little more cumbersome, so a lot of people are looking for simpler solutions to server side data fetching.

Adding a cacheKey attribute to a DOM element causes a React dev warning.

ReactDOM.render checks properties on DOM elements to make sure they are valid HTML attributes, and it throws a console warning in development mode if not. Since cacheKey is not a valid HTML attribute, rendering <div cacheKey="foo"/> results in this console warning:

Warning: Unknown prop `cacheKey` on <div> tag. Remove this prop from the element. For details, see https://fb.me/react-unknown-prop

I would suggest changing the name of the prop to data-cacheKey or data-cache-key. That will avoid the unknown prop warning.

Consider automatically pushing out a script tag that sets the checksum

I sent this in a tweet to @divmain but thought it was worth expanding on here.

The first version of react-dom-stream took the same checksum strategy you are using here in Rapscallion: have the checksum be an extra attribute on the stream which the API client can then read out into a script tag, and that script tag in turn sets the checksum on the react root. This strategy works, but it caused a lot of confusion with users; if my experience is any indication, you may end up spending a lot of time explaining and supporting this quirk in github issues/gitter.

I wanted to just render out the script tag that was needed at the end of the stream, but React complains when it renders into an element where there are siblings that weren't made by React. I realized later, though, that you could read out such a script tag at the end of the stream and have it set the checksum and then delete itself from the DOM before React client rendering happens. I tested this in all my supported browsers, and it worked. Its implementation is here, and you should feel free to use any part of it that is helpful in Rapscallion.

Cheers!

Null components should be rendered

Consider the empty component:

class Empty extends React.Component {
  render() {
    return null;
  }
}

Then rendering the component should result in placeholder in the form of HTML comment like so

> ReactDOMServer.renderToString(<Empty />)
'<!-- react-empty: 1 -->'

Instead Rapscallion just outputs an empty string. Client-side React will then complain about invalid checksum.

This should also work with undefined and false.

Consider non-global cache strategies

Capturing an out-of-band conversation with @ryan-roemer:

Using a global singleton for the cache strategy is potentially undesirable. Or, inversely, it might be desirable to use different caching solutions for different components / pages in the context of a single Node.js process.

If the traversal functions in src/render/traverse.js were made to be class methods of a new Traverser class, a cacher object with methods get and set could be attached to the instance to simplify access to the custom cacher.

This could be provided as a replacement to setCacheStrategy or as a complement. If a complement, setCacheStrategy would set the global cache strategy. This value would be overrideable by passing a Cacher to the Renderer constructor.

This is probably a better design, but also a nice-to-have, since a global caching strategy is probably adequate for the vast majority of use cases.

See also: #36 (comment)

Allow component cache to use alternative backend

Everything is in-memory for now. However, caching is done with simple keys. And once a cached component has been compressed, the values stored are simple strings. This being the case, there's no reason why Redis couldn't be used as a backend.

Several benefits come to mind:

  • TTLs for cache entries become simple to manage
  • you don't risk out-of-memory errors in your Node process for folks caching anything and everything
  • this scales across nodes
  • you could also prime your cache using known keys

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.