Skip to content

Commit f54e38d

Browse files
committed
Fixes bug where a browser-only implementation was executing behaviour meant for server.
1 parent d2b2cb8 commit f54e38d

File tree

3 files changed

+24
-54
lines changed

3 files changed

+24
-54
lines changed

src/__tests__/__snapshots__/integration.test.js.snap

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,6 @@
1-
exports[`integration works 1`] = `
2-
<AsyncComponentProvider
3-
execContext={
4-
Object {
5-
"getComponent": [Function],
6-
"getNextId": [Function],
7-
"getResolved": [Function],
8-
"registerComponent": [Function],
9-
}
10-
}>
11-
<AsyncBob>
12-
<Bob>
13-
<div>
14-
<div>
15-
<AsyncBobTwo>
16-
<Bob>
17-
<div>
18-
<span>
19-
In Render.
20-
</span>
21-
</div>
22-
</Bob>
23-
</AsyncBobTwo>
24-
<DeferredAsyncBob />
25-
<BoundaryAsyncBob>
26-
<Bob>
27-
<div>
28-
<span>
29-
In Boundary but outside an AsyncComponent, server render me!
30-
</span>
31-
<AsyncBobThree />
32-
</div>
33-
</Bob>
34-
</BoundaryAsyncBob>
35-
</div>
36-
</div>
37-
</Bob>
38-
</AsyncBob>
39-
</AsyncComponentProvider>
40-
`;
41-
42-
exports[`integration works 2`] = `"<div data-reactroot=\"\" data-reactid=\"1\" data-react-checksum=\"-636787408\"><div data-reactid=\"2\"><div data-reactid=\"3\"><span data-reactid=\"4\">In Render.</span></div><!-- react-empty: 5 --><div data-reactid=\"6\"><span data-reactid=\"7\">In Boundary but outside an AsyncComponent, server render me!</span><!-- react-empty: 8 --></div></div></div>"`;
1+
exports[`integration works 1`] = `"<div><div><div><span>In Render.</span></div><div><span>In Boundary but outside an AsyncComponent, server render me!</span></div></div></div>"`;
432

44-
exports[`integration works 3`] = `
3+
exports[`integration works 2`] = `
454
<AsyncComponentProvider
465
execContext={
476
Object {
@@ -82,7 +41,7 @@ exports[`integration works 3`] = `
8241
</AsyncComponentProvider>
8342
`;
8443

85-
exports[`integration works 4`] = `
44+
exports[`integration works 3`] = `
8645
<AsyncComponentProvider
8746
execContext={
8847
Object {

src/__tests__/integration.test.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* @flow */
22

33
import React from 'react';
4-
import { renderToString } from 'react-dom/server';
4+
import { renderToStaticMarkup } from 'react-dom/server';
55
import { mount } from 'enzyme';
66
import { createAsyncComponent, withAsyncComponents } from '../';
77
import { STATE_IDENTIFIER } from '../constants';
@@ -63,14 +63,18 @@ describe('integration', () => {
6363
delete global.window[STATE_IDENTIFIER];
6464
});
6565

66-
it('works', () =>
66+
it('works', () => {
67+
const windowTemp = global.window;
68+
// we have to delete the window to emulate a server only environment
69+
delete global.window;
70+
6771
// "Server" side render...
68-
withAsyncComponents(app)
72+
return withAsyncComponents(app)
6973
.then(({ appWithAsyncComponents, state, STATE_IDENTIFIER: STATE_ID }) => {
70-
expect(mount(appWithAsyncComponents)).toMatchSnapshot();
71-
const serverString = renderToString(appWithAsyncComponents);
74+
const serverString = renderToStaticMarkup(appWithAsyncComponents);
7275
expect(serverString).toMatchSnapshot();
73-
// Attach the state to the "window" for the client
76+
// Restore the window and attach the state to the "window" for the client
77+
global.window = windowTemp;
7478
global.window[STATE_ID] = state;
7579
return serverString;
7680
})
@@ -80,7 +84,7 @@ describe('integration', () => {
8084
.then(({ appWithAsyncComponents }) => {
8185
const clientRenderWrapper = mount(appWithAsyncComponents);
8286
expect(clientRenderWrapper).toMatchSnapshot();
83-
expect(renderToString(appWithAsyncComponents)).toEqual(serverHTML);
87+
expect(renderToStaticMarkup(appWithAsyncComponents)).toEqual(serverHTML);
8488
return clientRenderWrapper;
8589
})
8690
// Now give the client side components time to resolve
@@ -91,6 +95,6 @@ describe('integration', () => {
9195
.then(clientRenderWrapper =>
9296
expect(clientRenderWrapper).toMatchSnapshot(),
9397
),
94-
),
95-
);
98+
);
99+
});
96100
});

src/withAsyncComponents.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ function createExecContext() {
3232
export default function withAsyncComponents(app : React$Element) {
3333
const execContext = createExecContext();
3434

35-
const rehydrateState = typeof window !== 'undefined'
35+
const isBrowser = typeof window !== 'undefined';
36+
const rehydrateState = isBrowser
3637
&& typeof window[STATE_IDENTIFIER] !== 'undefined'
3738
? window[STATE_IDENTIFIER]
3839
: null;
@@ -43,6 +44,12 @@ export default function withAsyncComponents(app : React$Element) {
4344
</AsyncComponentProvider>
4445
);
4546

47+
if (isBrowser && !rehydrateState) {
48+
return Promise.resolve({
49+
appWithAsyncComponents,
50+
});
51+
}
52+
4653
const doWalk = (el, ctx = {}, fetchRoot = false) => {
4754
const resolvers = [];
4855

0 commit comments

Comments
 (0)