-
Notifications
You must be signed in to change notification settings - Fork 47.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Fizz] Add react-dom/static Entry Point for Prerendering #24752
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict'; | ||
|
||
if (process.env.NODE_ENV === 'production') { | ||
module.exports = require('./cjs/react-dom-static.browser.production.min.js'); | ||
} else { | ||
module.exports = require('./cjs/react-dom-static.browser.development.js'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
'use strict'; | ||
|
||
module.exports = require('./static.node'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict'; | ||
|
||
if (process.env.NODE_ENV === 'production') { | ||
module.exports = require('./cjs/react-dom-static.node.production.min.js'); | ||
} else { | ||
module.exports = require('./cjs/react-dom-static.node.development.js'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
237 changes: 237 additions & 0 deletions
237
packages/react-dom/src/__tests__/ReactDOMFizzStatic-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,237 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @emails react-core | ||
*/ | ||
|
||
'use strict'; | ||
|
||
let JSDOM; | ||
let Stream; | ||
let React; | ||
let ReactDOMClient; | ||
let ReactDOMFizzStatic; | ||
let Suspense; | ||
let textCache; | ||
let document; | ||
let writable; | ||
let container; | ||
let buffer = ''; | ||
let hasErrored = false; | ||
let fatalError = undefined; | ||
|
||
describe('ReactDOMFizzStatic', () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
JSDOM = require('jsdom').JSDOM; | ||
React = require('react'); | ||
ReactDOMClient = require('react-dom/client'); | ||
if (__EXPERIMENTAL__) { | ||
ReactDOMFizzStatic = require('react-dom/static'); | ||
} | ||
Stream = require('stream'); | ||
Suspense = React.Suspense; | ||
|
||
textCache = new Map(); | ||
|
||
// Test Environment | ||
const jsdom = new JSDOM( | ||
'<!DOCTYPE html><html><head></head><body><div id="container">', | ||
{ | ||
runScripts: 'dangerously', | ||
}, | ||
); | ||
document = jsdom.window.document; | ||
container = document.getElementById('container'); | ||
|
||
buffer = ''; | ||
hasErrored = false; | ||
|
||
writable = new Stream.PassThrough(); | ||
writable.setEncoding('utf8'); | ||
writable.on('data', chunk => { | ||
buffer += chunk; | ||
}); | ||
writable.on('error', error => { | ||
hasErrored = true; | ||
fatalError = error; | ||
}); | ||
}); | ||
|
||
async function act(callback) { | ||
await callback(); | ||
// Await one turn around the event loop. | ||
// This assumes that we'll flush everything we have so far. | ||
await new Promise(resolve => { | ||
setImmediate(resolve); | ||
}); | ||
if (hasErrored) { | ||
throw fatalError; | ||
} | ||
// JSDOM doesn't support stream HTML parser so we need to give it a proper fragment. | ||
// We also want to execute any scripts that are embedded. | ||
// We assume that we have now received a proper fragment of HTML. | ||
const bufferedContent = buffer; | ||
buffer = ''; | ||
const fakeBody = document.createElement('body'); | ||
fakeBody.innerHTML = bufferedContent; | ||
while (fakeBody.firstChild) { | ||
const node = fakeBody.firstChild; | ||
if (node.nodeName === 'SCRIPT') { | ||
const script = document.createElement('script'); | ||
script.textContent = node.textContent; | ||
fakeBody.removeChild(node); | ||
container.appendChild(script); | ||
} else { | ||
container.appendChild(node); | ||
} | ||
} | ||
} | ||
|
||
function getVisibleChildren(element) { | ||
const children = []; | ||
let node = element.firstChild; | ||
while (node) { | ||
if (node.nodeType === 1) { | ||
if ( | ||
node.tagName !== 'SCRIPT' && | ||
node.tagName !== 'TEMPLATE' && | ||
node.tagName !== 'template' && | ||
!node.hasAttribute('hidden') && | ||
!node.hasAttribute('aria-hidden') | ||
) { | ||
const props = {}; | ||
const attributes = node.attributes; | ||
for (let i = 0; i < attributes.length; i++) { | ||
if ( | ||
attributes[i].name === 'id' && | ||
attributes[i].value.includes(':') | ||
) { | ||
// We assume this is a React added ID that's a non-visual implementation detail. | ||
continue; | ||
} | ||
props[attributes[i].name] = attributes[i].value; | ||
} | ||
props.children = getVisibleChildren(node); | ||
children.push(React.createElement(node.tagName.toLowerCase(), props)); | ||
} | ||
} else if (node.nodeType === 3) { | ||
children.push(node.data); | ||
} | ||
node = node.nextSibling; | ||
} | ||
return children.length === 0 | ||
? undefined | ||
: children.length === 1 | ||
? children[0] | ||
: children; | ||
} | ||
|
||
function resolveText(text) { | ||
const record = textCache.get(text); | ||
if (record === undefined) { | ||
const newRecord = { | ||
status: 'resolved', | ||
value: text, | ||
}; | ||
textCache.set(text, newRecord); | ||
} else if (record.status === 'pending') { | ||
const thenable = record.value; | ||
record.status = 'resolved'; | ||
record.value = text; | ||
thenable.pings.forEach(t => t()); | ||
} | ||
} | ||
|
||
/* | ||
function rejectText(text, error) { | ||
const record = textCache.get(text); | ||
if (record === undefined) { | ||
const newRecord = { | ||
status: 'rejected', | ||
value: error, | ||
}; | ||
textCache.set(text, newRecord); | ||
} else if (record.status === 'pending') { | ||
const thenable = record.value; | ||
record.status = 'rejected'; | ||
record.value = error; | ||
thenable.pings.forEach(t => t()); | ||
} | ||
} | ||
*/ | ||
|
||
function readText(text) { | ||
const record = textCache.get(text); | ||
if (record !== undefined) { | ||
switch (record.status) { | ||
case 'pending': | ||
throw record.value; | ||
case 'rejected': | ||
throw record.value; | ||
case 'resolved': | ||
return record.value; | ||
} | ||
} else { | ||
const thenable = { | ||
pings: [], | ||
then(resolve) { | ||
if (newRecord.status === 'pending') { | ||
thenable.pings.push(resolve); | ||
} else { | ||
Promise.resolve().then(() => resolve(newRecord.value)); | ||
} | ||
}, | ||
}; | ||
|
||
const newRecord = { | ||
status: 'pending', | ||
value: thenable, | ||
}; | ||
textCache.set(text, newRecord); | ||
|
||
throw thenable; | ||
} | ||
} | ||
|
||
function Text({text}) { | ||
return text; | ||
} | ||
|
||
function AsyncText({text}) { | ||
return readText(text); | ||
} | ||
|
||
// @gate experimental | ||
it('should render a fully static document, send it and then hydrate it', async () => { | ||
function App() { | ||
return ( | ||
<div> | ||
<Suspense fallback={<Text text="Loading..." />}> | ||
<AsyncText text="Hello" /> | ||
</Suspense> | ||
</div> | ||
); | ||
} | ||
|
||
const promise = ReactDOMFizzStatic.prerenderToNodeStreams(<App />); | ||
|
||
resolveText('Hello'); | ||
|
||
const result = await promise; | ||
|
||
await act(async () => { | ||
result.prelude.pipe(writable); | ||
}); | ||
expect(getVisibleChildren(container)).toEqual(<div>Hello</div>); | ||
|
||
await act(async () => { | ||
ReactDOMClient.hydrateRoot(container, <App />); | ||
}); | ||
|
||
expect(getVisibleChildren(container)).toEqual(<div>Hello</div>); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do these not use the DCE check code because they are not client and won’t always be bundled?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It matters less on the server so not worth the code. If you're setting up a server environment you are down a deeper rabbit hole anyway.