Skip to content

Commit 8002669

Browse files
committed
wip
1 parent 547b444 commit 8002669

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99

1010
'use strict';
11+
import * as fs from 'fs';
1112

1213
let JSDOM;
1314
let Stream;
@@ -91,17 +92,7 @@ describe('ReactDOMFizzServer', () => {
9192

9293
renderOptions = {};
9394
if (gate(flags => flags.enableFizzNoScriptExecution)) {
94-
window.__init_instruction_observer__ = () => {
95-
global.testDocument = document;
96-
global.MutationObserver = jsdom.window.MutationObserver;
97-
reactInstructionObserver = ReactDOMFizzServer.getReactDOMClientMutationObserver();
98-
reactInstructionObserver.observe(container, {
99-
childList: true,
100-
subtree: true,
101-
});
102-
};
103-
104-
renderOptions.bootstrapScriptContent = '__init_instruction_observer__();';
95+
renderOptions.bootstrapModules = ['../server/ReactDOMClientInstructionObserver.js'];
10596
renderOptions.disableInstructionScriptExecution = true;
10697
}
10798
});
@@ -138,7 +129,13 @@ describe('ReactDOMFizzServer', () => {
138129
(CSPnonce === null || inner.getAttribute('nonce') === CSPnonce)
139130
) {
140131
const script = document.createElement('SCRIPT');
141-
script.textContent = inner.textContent;
132+
const scriptSrc = inner.getAttribute("src");
133+
if (scriptSrc) {
134+
script.textContent = fs.readFileSync(require.resolve(scriptSrc));
135+
console.log("got script src");
136+
} else {
137+
script.textContent = inner.textContent;
138+
}
142139
node.replaceChild(script, inner);
143140
} else {
144141
replaceScripts(inner);
@@ -172,7 +169,12 @@ describe('ReactDOMFizzServer', () => {
172169
(CSPnonce === null || node.getAttribute('nonce') === CSPnonce)
173170
) {
174171
const script = document.createElement('SCRIPT');
175-
script.textContent = node.textContent;
172+
const scriptSrc = node.getAttribute("src");
173+
if (scriptSrc) {
174+
script.textContent = fs.readFileSync(require.resolve(scriptSrc));
175+
} else {
176+
script.textContent = inner.textContent;
177+
}
176178
fakeBody.removeChild(node);
177179
parent.appendChild(script);
178180
} else {
@@ -330,13 +332,14 @@ describe('ReactDOMFizzServer', () => {
330332
const mergedOptions = {...renderOptions};
331333
Object.entries(options).forEach(([key, value]) => {
332334
if (mergedOptions[key]) {
333-
if (key === 'bootstrapScriptContent') {
334-
mergedOptions[key] += value;
335+
if (key === 'bootstrapModules') {
336+
mergedOptions[key].push(value);
335337
}
336338
} else {
337339
mergedOptions[key] = value;
338340
}
339341
});
342+
console.log(mergedOptions);
340343
return ReactDOMFizzServer.renderToPipeableStream(jsx, mergedOptions);
341344
} else {
342345
return ReactDOMFizzServer.renderToPipeableStream(jsx, renderOptions);
@@ -1849,7 +1852,6 @@ describe('ReactDOMFizzServer', () => {
18491852
await act(async () => {
18501853
controls = renderToPipeableStream(
18511854
<App isClient={false} />,
1852-
18531855
{
18541856
onError,
18551857
},

packages/react-dom/src/server/ReactDOMClientInstructionObserver.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,22 @@
77
* @flow
88
*/
99

10+
let __DEV__;
11+
if (window.__DEV__ != null) {
12+
__DEV__ = window.__DEV__;
13+
} else {
14+
__DEV__ = true;
15+
}
16+
17+
console.log("Client mutation observer");
1018
const reactInstructionObserver /*: MutationObserver*/ = new MutationObserver(
1119
mutations => {
1220
mutations.forEach(mutation => {
1321
mutation.addedNodes.forEach(node => {
1422
if (node.nodeType !== 1) {
1523
return;
1624
}
25+
1726
// $FlowFixMe[incompatible-cast]
1827
const attrStr = (node /*: HTMLElement*/)
1928
.getAttribute('data-react-server-instruction');
@@ -24,14 +33,17 @@ const reactInstructionObserver /*: MutationObserver*/ = new MutationObserver(
2433
if (!attr) {
2534
return;
2635
}
36+
2737
if (__DEV__) {
2838
if (!(Array.isArray(attr) && attr.length >= 1)) {
2939
console.error('Bad shape: data attribute');
3040
return;
3141
}
3242
}
43+
3344
const instr = attr[0];
3445
const args = attr.slice(1);
46+
console.log(instr, args);
3547
switch (instr) {
3648
case '$RX':
3749
clientRenderBoundaryImpl(args);
@@ -55,12 +67,6 @@ const reactInstructionObserver /*: MutationObserver*/ = new MutationObserver(
5567
},
5668
);
5769

58-
let document;
59-
if (__DEV__ && global.testDocument) {
60-
document = global.testDocument;
61-
} else {
62-
document = global.document;
63-
}
6470

6571
if (document.body) {
6672
reactInstructionObserver.observe(document.body, {
@@ -209,6 +215,8 @@ function completeBoundary(suspenseBoundaryID, contentID, errorDigest) {
209215
const contentNode = document.getElementById(contentID);
210216
// We'll detach the content node so that regardless of what happens next we don't leave in the tree.
211217
// This might also help by not causing recalcing each time we move a child from here to the target.
218+
// $FlowFixMe[incompatible-use]
219+
// $FlowFixMe[incompatible-call]
212220
contentNode.parentNode.removeChild(contentNode);
213221

214222
// Find the fallback's first element.
@@ -228,11 +236,14 @@ function completeBoundary(suspenseBoundaryID, contentID, errorDigest) {
228236
// This is similar to clearSuspenseBoundary in ReactDOMHostConfig.
229237
// TODO: We could avoid this if we never emitted suspense boundaries in fallback trees.
230238
// They never hydrate anyway. However, currently we support incrementally loading the fallback.
239+
// $FlowFixMe[incompatible-use]
231240
const parentInstance = suspenseNode.parentNode;
241+
// $FlowFixMe[incompatible-use]
232242
let node = suspenseNode.nextSibling;
233243
let depth = 0;
234244
do {
235245
if (node && node.nodeType === COMMENT_NODE) {
246+
// $FlowFixMe[prop-missing]
236247
const data = node.data;
237248
if (data === SUSPENSE_END_DATA) {
238249
if (depth === 0) {
@@ -370,4 +381,4 @@ function completeSegmentImpl(args) {
370381
completeSegment(...args);
371382
}
372383

373-
export default reactInstructionObserver;
384+
// export default reactInstructionObserver;

0 commit comments

Comments
 (0)