-
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
[Fax] Support nesting in existing RSC renderers #30736
Open
eps1lon
wants to merge
10
commits into
facebook:main
Choose a base branch
from
eps1lon:fax/nested-rsc-renderer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+390
−48
Open
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
45e1f38
[Fax] Test `react-markup` in Flight fixture
eps1lon 568dc35
Use unit test instead
eps1lon 68d5c57
Share async dispatchers
eps1lon 5231a95
Expand tests for stacks in nested Flight renderers
eps1lon 7b885f1
Expand cache tests
eps1lon c334f20
Set async dispatchers during module init
eps1lon 40bad2d
Simplify `getCacheForType` impl
eps1lon d9c489b
Ensure the active cache is used or shared
eps1lon f140f36
Remove `getCacheForType` from dispatcher
eps1lon 35507c5
Reuse active cache
eps1lon 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
319 changes: 319 additions & 0 deletions
319
packages/react-markup/src/__tests__/ReactMarkupAndFlight-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,319 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and 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 | ||
* @jest-environment node | ||
*/ | ||
|
||
'use strict'; | ||
|
||
if (typeof Blob === 'undefined') { | ||
global.Blob = require('buffer').Blob; | ||
} | ||
if (typeof File === 'undefined' || typeof FormData === 'undefined') { | ||
global.File = require('undici').File; | ||
global.FormData = require('undici').FormData; | ||
} | ||
|
||
let act; | ||
let React; | ||
let ReactServer; | ||
let ReactMarkup; | ||
let ReactNoop; | ||
let ReactNoopFlightServer; | ||
let ReactNoopFlightClient; | ||
|
||
function normalizeCodeLocInfo(str) { | ||
return ( | ||
str && | ||
String(str).replace(/\n +(?:at|in) ([\S]+)[^\n]*/g, function (m, name) { | ||
return '\n in ' + name + ' (at **)'; | ||
}) | ||
); | ||
} | ||
|
||
if (!__EXPERIMENTAL__) { | ||
it('should not be built in stable', () => { | ||
try { | ||
require('react-markup'); | ||
} catch (x) { | ||
return; | ||
} | ||
throw new Error('Expected react-markup not to exist in stable.'); | ||
}); | ||
} else { | ||
describe('ReactMarkupAndFlight', () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
jest.mock('react', () => require('react/react.react-server')); | ||
ReactServer = require('react'); | ||
ReactNoopFlightServer = require('react-noop-renderer/flight-server'); | ||
// This stores the state so we need to preserve it | ||
const flightModules = require('react-noop-renderer/flight-modules'); | ||
if (__EXPERIMENTAL__) { | ||
jest.resetModules(); | ||
jest.mock('react', () => ReactServer); | ||
jest.mock('react-markup', () => | ||
require('react-markup/react-markup.react-server'), | ||
); | ||
ReactMarkup = require('react-markup'); | ||
} | ||
jest.resetModules(); | ||
__unmockReact(); | ||
jest.mock('react-noop-renderer/flight-modules', () => flightModules); | ||
React = require('react'); | ||
ReactNoop = require('react-noop-renderer'); | ||
ReactNoopFlightClient = require('react-noop-renderer/flight-client'); | ||
act = require('internal-test-utils').act; | ||
}); | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it('supports using react-markup', async () => { | ||
async function Preview() { | ||
const html = | ||
await ReactMarkup.experimental_renderToHTML('Hello, Dave!'); | ||
|
||
return <pre>{html}</pre>; | ||
} | ||
|
||
const model = <Preview />; | ||
const transport = ReactNoopFlightServer.render(model); | ||
|
||
await act(async () => { | ||
// So it throws here with "Cannot read properties of null (reading 'length')" | ||
ReactNoop.render(await ReactNoopFlightClient.read(transport)); | ||
}); | ||
|
||
expect(ReactNoop).toMatchRenderedOutput(<pre>Hello, Dave!</pre>); | ||
}); | ||
|
||
it('has a cache if the first renderer is used standalone', async () => { | ||
let n = 0; | ||
const uncachedFunction = jest.fn(() => { | ||
return n++; | ||
}); | ||
const random = ReactServer.cache(uncachedFunction); | ||
|
||
function Random() { | ||
return random(); | ||
} | ||
|
||
function App() { | ||
return ( | ||
<> | ||
<p> | ||
RSC A_1: <Random /> | ||
</p> | ||
<p> | ||
RSC A_2: <Random /> | ||
</p> | ||
</> | ||
); | ||
} | ||
|
||
const model = <App />; | ||
const transport = ReactNoopFlightServer.render(model); | ||
|
||
await act(async () => { | ||
ReactNoop.render(await ReactNoopFlightClient.read(transport)); | ||
}); | ||
|
||
expect(ReactNoop).toMatchRenderedOutput( | ||
<> | ||
<p>RSC A_1: 0</p> | ||
<p>RSC A_2: 0</p> | ||
</>, | ||
); | ||
expect(uncachedFunction).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('has a cache if the second renderer is used standalone', async () => { | ||
let n = 0; | ||
const uncachedFunction = jest.fn(() => { | ||
return n++; | ||
}); | ||
const random = ReactServer.cache(uncachedFunction); | ||
|
||
function Random() { | ||
return random(); | ||
} | ||
|
||
function App() { | ||
return ( | ||
<> | ||
<p> | ||
RSC B_1: <Random /> | ||
</p> | ||
<p> | ||
RSC B_2: <Random /> | ||
</p> | ||
</> | ||
); | ||
} | ||
|
||
const html = await ReactMarkup.experimental_renderToHTML( | ||
ReactServer.createElement(App), | ||
); | ||
|
||
expect(html).toEqual('<p>RSC B_1: 0</p><p>RSC B_2: 0</p>'); | ||
expect(uncachedFunction).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('shares cache between RSC renderers', async () => { | ||
let n = 0; | ||
const uncachedFunction = jest.fn(() => { | ||
return n++; | ||
}); | ||
const random = ReactServer.cache(uncachedFunction); | ||
|
||
function Random() { | ||
return random(); | ||
} | ||
|
||
async function Preview() { | ||
const html = await ReactMarkup.experimental_renderToHTML(<Random />); | ||
|
||
return ( | ||
<> | ||
<p> | ||
RSC A: <Random /> | ||
</p> | ||
<p>RSC B: {html}</p> | ||
</> | ||
); | ||
} | ||
|
||
function App() { | ||
return ( | ||
<> | ||
<Preview /> | ||
<Preview /> | ||
</> | ||
); | ||
} | ||
|
||
const model = <App />; | ||
const transport = ReactNoopFlightServer.render(model); | ||
|
||
await act(async () => { | ||
ReactNoop.render(await ReactNoopFlightClient.read(transport)); | ||
}); | ||
|
||
expect(ReactNoop).toMatchRenderedOutput( | ||
<> | ||
<p>RSC A: 0</p> | ||
<p>RSC B: 0</p> | ||
<p>RSC A: 0</p> | ||
<p>RSC B: 0</p> | ||
</>, | ||
); | ||
expect(uncachedFunction).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('shows correct stacks in nested RSC renderers', async () => { | ||
const thrownError = new Error('hi'); | ||
|
||
const caughtNestedRendererErrors = []; | ||
const ownerStacksDuringParentRendererThrow = []; | ||
const ownerStacksDuringNestedRendererThrow = []; | ||
function Throw() { | ||
if (gate(flags => flags.enableOwnerStacks)) { | ||
const stack = ReactServer.captureOwnerStack(); | ||
ownerStacksDuringNestedRendererThrow.push( | ||
normalizeCodeLocInfo(stack), | ||
); | ||
} | ||
throw thrownError; | ||
} | ||
|
||
function Indirection() { | ||
return ReactServer.createElement(Throw); | ||
} | ||
|
||
function App() { | ||
return ReactServer.createElement(Indirection); | ||
} | ||
|
||
async function Preview() { | ||
try { | ||
await ReactMarkup.experimental_renderToHTML( | ||
ReactServer.createElement(App), | ||
{ | ||
onError: (error, errorInfo) => { | ||
caughtNestedRendererErrors.push({ | ||
error: error, | ||
parentStack: errorInfo.componentStack, | ||
ownerStack: gate(flags => flags.enableOwnerStacks) | ||
? ReactServer.captureOwnerStack() | ||
: null, | ||
}); | ||
}, | ||
}, | ||
); | ||
} catch (error) { | ||
let stack = ''; | ||
if (gate(flags => flags.enableOwnerStacks)) { | ||
stack = ReactServer.captureOwnerStack(); | ||
ownerStacksDuringParentRendererThrow.push( | ||
normalizeCodeLocInfo(stack), | ||
); | ||
} | ||
|
||
return 'did error'; | ||
} | ||
} | ||
|
||
function PreviewApp() { | ||
return ReactServer.createElement(Preview); | ||
} | ||
|
||
const model = ReactServer.createElement(PreviewApp); | ||
const transport = ReactNoopFlightServer.render(model); | ||
|
||
await act(async () => { | ||
ReactNoop.render(await ReactNoopFlightClient.read(transport)); | ||
}); | ||
|
||
expect(caughtNestedRendererErrors).toEqual([ | ||
{ | ||
error: thrownError, | ||
ownerStack: | ||
__DEV__ && gate(flags => flags.enableOwnerStacks) | ||
? // TODO: Shouldn't this read the same as the one we got during render? | ||
'' | ||
: null, | ||
// TODO: Shouldn't a parent stack exist? | ||
parentStack: undefined, | ||
}, | ||
]); | ||
expect(ownerStacksDuringParentRendererThrow).toEqual( | ||
gate(flags => flags.enableOwnerStacks) | ||
? [ | ||
__DEV__ | ||
? // TODO: Should have an owner stack | ||
'' | ||
: null, | ||
] | ||
: [], | ||
); | ||
expect(ownerStacksDuringNestedRendererThrow).toEqual( | ||
gate(flags => flags.enableOwnerStacks) | ||
? [ | ||
__DEV__ | ||
? '\n' + | ||
// | ||
' in Indirection (at **)\n' + | ||
' in App (at **)' | ||
: null, | ||
] | ||
: [], | ||
); | ||
}); | ||
}); | ||
} |
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
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
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
Oops, something went wrong.
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.
There's no need for both of these. If we're saying that the implementation detail is a
Map<Function>
and that is exposed throughgetActiveCache()
, there's not that muchgetCacheForType
can configure and it's unnecessary indirection.getCacheForType
can just be a utility function that callsgetActiveCache
. (Or we can get rid of getCacheForType completely and just have the callers callgetActiveCache
.) No need to be on the dispatcher anymore.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.
To preempt "I'll do it in a follow up". Since we know that these dispatchers are messed with by people and we break them we don't want to change this too many times. Even the async cache ones (cc Janka).
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.
Like this
f140f36
(#30736)?Whatever resolves the cache conflict needs to know the previous dispatcher so it made sense to me to just do the resolution inside
getActiveCache
. I don't know how else to do it if we wantgetActiveCache
to return the actual Map (or null) and resolve conflicts in a utility since that utility needs to know the current and previous dispatcher.