Skip to content

Commit 636b11c

Browse files
committed
Fix preload for production NODE_ENV.
Fixes jaydenseric/graphql-react#11 and fixes jaydenseric/graphql-react#12.
1 parent 36040be commit 636b11c

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

changelog.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
### Patch
66

7-
- Remove redundant uses of `this` in the internal `GraphQLQuery` component constructor.
8-
- Test the library with undefined and production `NODE_ENV`.
7+
- Fixed preload for `production` `NODE_ENV`, fixing [#11](https://github.com/jaydenseric/graphql-react/issues/11) and [#12](https://github.com/jaydenseric/graphql-react/issues/12).
8+
- Removed redundant uses of `this` in the internal `GraphQLQuery` component constructor.
9+
- Test the library with undefined and `production` `NODE_ENV`.
910

1011
## 4.0.0
1112

src/preload.mjs

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,16 @@ export function preload(element) {
5757
* @kind function
5858
* @name preload~recursePreload
5959
* @param {ReactElement} rootElement A React virtual DOM element.
60-
* @param {Object} [rootLegacyContext={}] Legacy React context for the root element and children.
60+
* @param {Object} [rootLegacyContext={}] React legacy context for the root element and children.
61+
* @param {Object} [rootNewContext={}] React new context map for the root element and children.
6162
* @param {boolean} [loadRoot=true] Should the root element be loaded.
6263
* @returns {Promise} Resolves once loading is done.
6364
* @ignore
6465
*/
6566
const recursePreload = (
6667
rootElement,
6768
rootLegacyContext = {},
69+
rootNewContext = new Map(),
6870
loadRoot = true
6971
) => {
7072
const loading = []
@@ -73,14 +75,15 @@ export function preload(element) {
7375
* @kind function
7476
* @name preload~recursePreload~recurse
7577
* @param {ReactElement} element A React virtual DOM element.
76-
* @param {Object} [legacyContext] Legacy React context for the element and children.
78+
* @param {Object} [legacyContext] React legacy context for the element and children.
79+
* @param {Map} [newContext] React new context map for the element and children.
7780
* @ignore
7881
*/
79-
const recurse = (element, legacyContext) => {
82+
const recurse = (element, legacyContext, newContext) => {
8083
if (!element) return
8184

8285
if (Array.isArray(element)) {
83-
element.forEach(item => recurse(item, legacyContext))
86+
element.forEach(item => recurse(item, legacyContext, newContext))
8487
return
8588
}
8689

@@ -94,13 +97,19 @@ export function preload(element) {
9497
// Determine the component props.
9598
const props = { ...element.type.defaultProps, ...element.props }
9699

97-
if (element.type.$$typeof === REACT_CONTEXT_TYPE)
100+
if (element.type.$$typeof === REACT_CONTEXT_TYPE) {
98101
// Context consumer element.
99-
recurse(
100-
element.props.children(element.type._context.currentValue),
101-
legacyContext
102-
)
103-
else if (
102+
103+
let value = element.type._currentValue
104+
const Provider = element.type._context
105+
? element.type._context.Provider
106+
: element.type.Provider
107+
108+
if (newContext && newContext.has(Provider))
109+
value = newContext.get(Provider)
110+
111+
recurse(element.props.children(value), legacyContext, newContext)
112+
} else if (
104113
// The element is a class component…
105114
element.type.prototype &&
106115
(element.type.prototype.isReactComponent ||
@@ -144,28 +153,34 @@ export function preload(element) {
144153
// Load this query.
145154
instance.load().then(() =>
146155
// Preload children, without reloading this query as the root.
147-
recursePreload(element, legacyContext, false)
156+
recursePreload(element, legacyContext, newContext, false)
148157
)
149158
)
150-
else recurse(instance.render(), legacyContext)
159+
else recurse(instance.render(), legacyContext, newContext)
151160
}
152161
// The element is a functional component…
153-
else recurse(element.type(props), legacyContext)
162+
else recurse(element.type(props), legacyContext, newContext)
154163
} else if (
155164
// The element is a context provider or DOM element and…
156165
element.props &&
157166
// …It has children…
158167
element.props.children
159168
) {
160169
// If the element is a context provider first set the value.
161-
if (element.type._context)
162-
element.type._context.currentValue = element.props.value
170+
if (element.type._context) {
171+
// Clone the context map to scope mutations to this provider’s
172+
// descendants.
173+
newContext = new Map(newContext)
174+
175+
// Set the context, keyed by the provider’s component type.
176+
newContext.set(element.type, element.props.value)
177+
}
163178

164-
recurse(element.props.children, legacyContext)
179+
recurse(element.props.children, legacyContext, newContext)
165180
}
166181
}
167182

168-
recurse(rootElement, rootLegacyContext)
183+
recurse(rootElement, rootLegacyContext, rootNewContext)
169184

170185
return Promise.all(loading)
171186
}

0 commit comments

Comments
 (0)