Skip to content

Commit

Permalink
PoC hack for facebook#3009.
Browse files Browse the repository at this point in the history
Allows feeding a write callback for renderToStaticMarkup().
  • Loading branch information
jussi-kalliokoski committed Feb 4, 2015
1 parent c8833da commit 5dcf432
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
26 changes: 20 additions & 6 deletions src/browser/server/ReactServerRendering.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,27 @@ var invariant = require('invariant');
* @param {ReactElement} element
* @return {string} the HTML markup
*/
function renderToString(element) {
function renderToString(element, writeFn) {
invariant(
ReactElement.isValidElement(element),
'renderToString(): You must pass a valid ReactElement.'
);

var markup = '';

writeFn = writeFn || function (string) {
markup += string;
};

var transaction;
try {
var id = ReactInstanceHandles.createReactRootID();
transaction = ReactServerRenderingTransaction.getPooled(false);
transaction.write = writeFn;

return transaction.perform(function() {
var componentInstance = instantiateReactComponent(element, null);
var markup =
componentInstance.mountComponent(id, transaction, emptyObject);
componentInstance.mountComponent(id, transaction, emptyObject);
return ReactMarkupChecksum.addChecksumToMarkup(markup);
}, null);
} finally {
Expand All @@ -52,20 +58,28 @@ function renderToString(element) {
* @return {string} the HTML markup, without the extra React ID and checksum
* (for generating static pages)
*/
function renderToStaticMarkup(element) {
function renderToStaticMarkup(element, writeFn) {
invariant(
ReactElement.isValidElement(element),
'renderToStaticMarkup(): You must pass a valid ReactElement.'
);

var markup = '';

writeFn = writeFn || function (string) {
markup += string;
};

var transaction;
try {
var id = ReactInstanceHandles.createReactRootID();
transaction = ReactServerRenderingTransaction.getPooled(true);
transaction = ReactServerRenderingTransaction.getPooled(true, writeFn);
transaction.write = writeFn;

return transaction.perform(function() {
var componentInstance = instantiateReactComponent(element, null);
return componentInstance.mountComponent(id, transaction, emptyObject);
componentInstance.mountComponent(id, transaction, emptyObject);
return markup;
}, null);
} finally {
ReactServerRenderingTransaction.release(transaction);
Expand Down
2 changes: 1 addition & 1 deletion src/browser/server/ReactServerRenderingTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var TRANSACTION_WRAPPERS = [
* @class ReactServerRenderingTransaction
* @param {boolean} renderToStaticMarkup
*/
function ReactServerRenderingTransaction(renderToStaticMarkup) {
function ReactServerRenderingTransaction(renderToStaticMarkup, writeFn) {
this.reinitializeTransaction();
this.renderToStaticMarkup = renderToStaticMarkup;
this.reactMountReady = CallbackQueue.getPooled(null);
Expand Down
12 changes: 6 additions & 6 deletions src/browser/ui/ReactDOMComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,12 @@ ReactDOMComponent.Mixin = {
mountComponent: function(rootID, transaction, context) {
this._rootNodeID = rootID;
assertValidProps(this._currentElement.props);
var closeTag = omittedCloseTags[this._tag] ? '' : '</' + this._tag + '>';
return (
this._createOpenTagMarkupAndPutListeners(transaction) +
this._createContentMarkup(transaction, context) +
closeTag
);
transaction.write(this._createOpenTagMarkupAndPutListeners(transaction));
transaction.write(this._createContentMarkup(transaction, context));
if (!omittedCloseTags[this._tag]) {
var closeTag = '</' + this._tag + '>';
transaction.write(closeTag);
}
},

/**
Expand Down
4 changes: 2 additions & 2 deletions src/browser/ui/ReactDOMTextComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ assign(ReactDOMTextComponent.prototype, {
// Normally we'd wrap this in a `span` for the reasons stated above, but
// since this is a situation where React won't take over (static pages),
// we can simply return the text as it is.
return escapedText;
transaction.write(escapedText);
}

return (
transaction.write(
'<span ' + DOMPropertyOperations.createMarkupForID(rootID) + '>' +
escapedText +
'</span>'
Expand Down

0 comments on commit 5dcf432

Please sign in to comment.