Remove duplication in ReactServerRendering
Ben Alpert committed
Oct 8, 2015 at 15:06 UTC
d4420eca8a527c22317d06ad166c74e8495c3a3a
2 files changed
+12
-33
src/renderers/dom/server/ReactServerRendering.js
+11
-32
@@ -29,7 +29,7 @@ var invariant = require('invariant');
29
* @param {ReactElement} element
30
* @return {string} the HTML markup
31
*/
32
-function renderToString(element) {
32
+function renderToStringImpl(element, makeStaticMarkup) {
33
invariant(
34
ReactElement.isValidElement(element),
35
'renderToString(): You must pass a valid ReactElement.'
@@ -42,13 +42,16 @@ function renderToString(element) {
42
var id = ReactInstanceHandles.createReactRootID(
43
ServerReactRootIndex.createReactRootIndex()
44
);
45
- transaction = ReactServerRenderingTransaction.getPooled(false);
45
+ transaction = ReactServerRenderingTransaction.getPooled(makeStaticMarkup);
46
47
return transaction.perform(function() {
48
var componentInstance = instantiateReactComponent(element, null);
49
var markup =
50
componentInstance.mountComponent(id, transaction, emptyObject);
51
- return ReactMarkupChecksum.addChecksumToMarkup(markup);
51
+ if (!makeStaticMarkup) {
52
+ markup = ReactMarkupChecksum.addChecksumToMarkup(markup);
53
+ }
54
+ return markup;
55
}, null);
56
} finally {
57
ReactServerRenderingTransaction.release(transaction);
@@ -58,36 +61,12 @@ function renderToString(element) {
61
}
62
}
63
61
-/**
62
- * @param {ReactElement} element
63
- * @return {string} the HTML markup, without the extra React ID and checksum
64
- * (for generating static pages)
65
- */
66
-function renderToStaticMarkup(element) {
67
- invariant(
68
- ReactElement.isValidElement(element),
69
- 'renderToStaticMarkup(): You must pass a valid ReactElement.'
70
- );
71
-
72
- var transaction;
73
- try {
74
- ReactUpdates.injection.injectBatchingStrategy(ReactServerBatchingStrategy);
75
-
76
- var id = ReactInstanceHandles.createReactRootID(
77
- ServerReactRootIndex.createReactRootIndex()
78
- );
79
- transaction = ReactServerRenderingTransaction.getPooled(true);
64
+function renderToString(element) {
65
+ return renderToStringImpl(element, false);
66
+}
67
81
- return transaction.perform(function() {
82
- var componentInstance = instantiateReactComponent(element, null);
83
- return componentInstance.mountComponent(id, transaction, emptyObject);
84
- }, null);
85
- } finally {
86
- ReactServerRenderingTransaction.release(transaction);
87
- // Revert to the DOM batching strategy since these two renderers
88
- // currently share these stateful modules.
89
- ReactUpdates.injection.injectBatchingStrategy(ReactDefaultBatchingStrategy);
90
- }
68
+function renderToStaticMarkup(element) {
69
+ return renderToStringImpl(element, true);
70
}
71
72
module.exports = {
src/renderers/dom/server/__tests__/ReactServerRendering-test.js
+1
-1
@@ -356,7 +356,7 @@ describe('ReactServerRendering', function() {
356
'not a component'
357
)
358
).toThrow(
359
- 'Invariant Violation: renderToStaticMarkup(): You must pass ' +
359
+ 'Invariant Violation: renderToString(): You must pass ' +
360
'a valid ReactElement.'
361
);
362
});