Skip to content
This repository was archived by the owner on Apr 13, 2023. It is now read-only.

Commit 58c9f6e

Browse files
committed
Remove top-level react-dom/server import to fix #2592.
Supersedes #2593. Should fix #2592. By pushing the react-dom/server import down into the relevant functions that need it, we can avoid unconditionally importing that dependency tree, which helps in environments like React Native where react-dom/server either doesn't work or seems undesirable (see discussion in #2592). Since the React Native bundler will still try to traverse the require("react-dom/server") dependencies, it's important to prune that dependency with a "react-native": { "react-dom/server": false } section in react-apollo/package.json. Note that this does not prevent React Native apps from using getMarkupFromTree with an appropriate renderFunction; it simply prevents React Native's bundler from bundling the react-dom/server dependency just because react-apollo is imported. Tested with both [email protected] and @0.57.7 (Expo SDKs 30 and 31).
1 parent a91e290 commit 58c9f6e

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
],
1313
"license": "MIT",
1414
"main": "lib/react-apollo.umd.js",
15+
"react-native": {
16+
"react-dom/server": false
17+
},
1518
"module": "src/index.ts",
1619
"typings": "lib/index.d.ts",
1720
"repository": {

src/getDataFromTree.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as React from 'react';
22
import * as PropTypes from 'prop-types';
3-
import { renderToStaticMarkup } from 'react-dom/server';
43
import Query from './Query';
54

65
// Like a Set, but for tuples. In practice, this class is used to store
@@ -93,14 +92,14 @@ export default function getDataFromTree(
9392
context,
9493
// If you need to configure this renderFunction, call getMarkupFromTree
9594
// directly instead of getDataFromTree.
96-
renderFunction: renderToStaticMarkup,
95+
renderFunction: require("react-dom/server").renderToStaticMarkup,
9796
});
9897
}
9998

10099
export type GetMarkupFromTreeOptions = {
101100
tree: React.ReactNode;
102101
context?: { [key: string]: any };
103-
renderFunction?: typeof renderToStaticMarkup;
102+
renderFunction?: (tree: React.ReactElement<any>) => string;
104103
};
105104

106105
export function getMarkupFromTree({
@@ -109,7 +108,7 @@ export function getMarkupFromTree({
109108
// The rendering function is configurable! We use renderToStaticMarkup as
110109
// the default, because it's a little less expensive than renderToString,
111110
// and legacy usage of getDataFromTree ignores the return value anyway.
112-
renderFunction = renderToStaticMarkup,
111+
renderFunction = require("react-dom/server").renderToStaticMarkup,
113112
}: GetMarkupFromTreeOptions): Promise<string> {
114113
const renderPromises = new RenderPromises();
115114

src/renderToStringWithData.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { ReactElement } from 'react';
2-
import * as ReactDOM from 'react-dom/server';
3-
4-
import { default as getDataFromTree } from './getDataFromTree';
2+
import { getMarkupFromTree } from './getDataFromTree';
53

64
export function renderToStringWithData(component: ReactElement<any>): Promise<string> {
7-
return getDataFromTree(component).then(() => ReactDOM.renderToString(component));
5+
return getMarkupFromTree({
6+
tree: component,
7+
renderFunction: require("react-dom/server").renderToString,
8+
});
89
}

0 commit comments

Comments
 (0)