gaearon / gaearon/react-side-effect

API suggestion: deprecate rewind() and use a closure to prevent leaks and race conditions

Open
#23 5 comments 2 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
1.2k
Forks
74
PR merge metrics
No merged PRs in 30d

Description

This is a proposal to deprecate the current API for server-side rendering:

``` js
let markup = ReactDOM.renderToString();
let title = DocumentTitle.rewind();
```

and replace it with this, inspired by Aphrodite's SSR API:

``` js
let { title, markup } = DocumentTitle.renderStatic(
() => ReactDOM.renderToString()
);
```

This API has two advantages. It is impossible to forget to call `rewind`. It is also easy to throw an error if `DocumentTitle.renderStatic` is called in a reentrant way.

You could use this with Aphrodite easily too. The `markup` field in the result is whatever you return from the closure (can figure out a better name later -- I just didn't want to call it "html" since it might include CSS):

``` js
let { title, markup: { html, css } } = DocumentTitle.renderStatic(
// Aphrodite.renderStatic() returns { html, css }
() => Aphrodite.renderStatic(() => ReactDOM.renderToString())
);
```

This is easy to build in user-space but I think it might be a strictly better API than `rewind()` so wanted to start a discussion here.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reviewing the current server-side rendering entry point using DocumentTitle.rewind(), then compare it with the proposed DocumentTitle.renderStatic() closure contract. Done means the deprecation and replacement API are resolved, including the returned markup value and the Aphrodite-style {html, css} result.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.