UNPKG

3.34 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2013-present, Facebook, Inc.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 */
8'use strict';
9
10var _prodInvariant = require('./reactProdInvariant');
11
12var React = require('react/lib/React');
13var ReactDOMContainerInfo = require('./ReactDOMContainerInfo');
14var ReactDefaultBatchingStrategy = require('./ReactDefaultBatchingStrategy');
15var ReactInstrumentation = require('./ReactInstrumentation');
16var ReactMarkupChecksum = require('./ReactMarkupChecksum');
17var ReactReconciler = require('./ReactReconciler');
18var ReactServerBatchingStrategy = require('./ReactServerBatchingStrategy');
19var ReactServerRenderingTransaction = require('./ReactServerRenderingTransaction');
20var ReactUpdates = require('./ReactUpdates');
21
22var emptyObject = require('fbjs/lib/emptyObject');
23var instantiateReactComponent = require('./instantiateReactComponent');
24var invariant = require('fbjs/lib/invariant');
25
26var pendingTransactions = 0;
27
28/**
29 * @param {ReactElement} element
30 * @return {string} the HTML markup
31 */
32function renderToStringImpl(element, makeStaticMarkup) {
33 var transaction;
34 try {
35 ReactUpdates.injection.injectBatchingStrategy(ReactServerBatchingStrategy);
36
37 transaction = ReactServerRenderingTransaction.getPooled(makeStaticMarkup);
38
39 pendingTransactions++;
40
41 return transaction.perform(function () {
42 var componentInstance = instantiateReactComponent(element, true);
43 var markup = ReactReconciler.mountComponent(componentInstance, transaction, null, ReactDOMContainerInfo(), emptyObject, 0 /* parentDebugID */
44 );
45 if (process.env.NODE_ENV !== 'production') {
46 ReactInstrumentation.debugTool.onUnmountComponent(componentInstance._debugID);
47 }
48 if (!makeStaticMarkup) {
49 markup = ReactMarkupChecksum.addChecksumToMarkup(markup);
50 }
51 return markup;
52 }, null);
53 } finally {
54 pendingTransactions--;
55 ReactServerRenderingTransaction.release(transaction);
56 // Revert to the DOM batching strategy since these two renderers
57 // currently share these stateful modules.
58 if (!pendingTransactions) {
59 ReactUpdates.injection.injectBatchingStrategy(ReactDefaultBatchingStrategy);
60 }
61 }
62}
63
64/**
65 * Render a ReactElement to its initial HTML. This should only be used on the
66 * server.
67 * See https://facebook.github.io/react/docs/top-level-api.html#reactdomserver.rendertostring
68 */
69function renderToString(element) {
70 !React.isValidElement(element) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'renderToString(): You must pass a valid ReactElement.') : _prodInvariant('46') : void 0;
71 return renderToStringImpl(element, false);
72}
73
74/**
75 * Similar to renderToString, except this doesn't create extra DOM attributes
76 * such as data-react-id that React uses internally.
77 * See https://facebook.github.io/react/docs/top-level-api.html#reactdomserver.rendertostaticmarkup
78 */
79function renderToStaticMarkup(element) {
80 !React.isValidElement(element) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'renderToStaticMarkup(): You must pass a valid ReactElement.') : _prodInvariant('47') : void 0;
81 return renderToStringImpl(element, true);
82}
83
84module.exports = {
85 renderToString: renderToString,
86 renderToStaticMarkup: renderToStaticMarkup
87};
\No newline at end of file