UNPKG

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