Skip to content
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] Refactor Component Stack Nodes #30298

Merged
merged 6 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export function describeFiber(
case HostComponent:
return describeBuiltInComponentFrame(workInProgress.type);
case LazyComponent:
// TODO: When we support Thenables as component types we should rename this.
return describeBuiltInComponentFrame('Lazy');
case SuspenseComponent:
return describeBuiltInComponentFrame('Suspense');
Expand Down
57 changes: 38 additions & 19 deletions packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -700,17 +700,39 @@ describe('ReactDOMFizzServer', () => {

it('should client render a boundary if a lazy component rejects', async () => {
let rejectComponent;
const promise = new Promise((resolve, reject) => {
rejectComponent = reject;
});
const LazyComponent = React.lazy(() => {
return new Promise((resolve, reject) => {
rejectComponent = reject;
});
return promise;
});

const LazyLazy = React.lazy(async () => {
return {
default: LazyComponent,
};
});

function Wrapper({children}) {
return children;
}
const LazyWrapper = React.lazy(() => {
return {
then(callback) {
callback({
default: Wrapper,
});
},
};
});

function App({isClient}) {
return (
<div>
<Suspense fallback={<Text text="Loading..." />}>
{isClient ? <Text text="Hello" /> : <LazyComponent text="Hello" />}
<LazyWrapper>
{isClient ? <Text text="Hello" /> : <LazyLazy text="Hello" />}
</LazyWrapper>
</Suspense>
</div>
);
Expand Down Expand Up @@ -744,6 +766,7 @@ describe('ReactDOMFizzServer', () => {
});
pipe(writable);
});

expect(loggedErrors).toEqual([]);
expect(bootstrapped).toBe(true);

Expand Down Expand Up @@ -772,7 +795,7 @@ describe('ReactDOMFizzServer', () => {
'Switched to client rendering because the server rendering errored:\n\n' +
theError.message,
expectedDigest,
componentStack(['Lazy', 'Suspense', 'div', 'App']),
componentStack(['Lazy', 'Wrapper', 'Suspense', 'div', 'App']),
],
],
[
Expand Down Expand Up @@ -852,13 +875,9 @@ describe('ReactDOMFizzServer', () => {
}

await act(() => {
const {pipe} = renderToPipeableStream(
<App isClient={false} />,

{
onError,
},
);
const {pipe} = renderToPipeableStream(<App isClient={false} />, {
onError,
});
pipe(writable);
});
expect(loggedErrors).toEqual([]);
Expand Down Expand Up @@ -896,7 +915,7 @@ describe('ReactDOMFizzServer', () => {
'Switched to client rendering because the server rendering errored:\n\n' +
theError.message,
expectedDigest,
componentStack(['Lazy', 'Suspense', 'div', 'App']),
componentStack(['Suspense', 'div', 'App']),
],
],
[
Expand Down Expand Up @@ -1395,13 +1414,13 @@ describe('ReactDOMFizzServer', () => {
'The render was aborted by the server without a reason.',
expectedDigest,
// We get the stack of the task when it was aborted which is why we see `h1`
componentStack(['h1', 'Suspense', 'div', 'App']),
componentStack(['AsyncText', 'h1', 'Suspense', 'div', 'App']),
],
[
'Switched to client rendering because the server rendering aborted due to:\n\n' +
'The render was aborted by the server without a reason.',
expectedDigest,
componentStack(['Suspense', 'main', 'div', 'App']),
componentStack(['AsyncText', 'Suspense', 'main', 'div', 'App']),
],
],
[
Expand Down Expand Up @@ -3523,13 +3542,13 @@ describe('ReactDOMFizzServer', () => {
'Switched to client rendering because the server rendering aborted due to:\n\n' +
'foobar',
'a digest',
componentStack(['Suspense', 'p', 'div', 'App']),
componentStack(['AsyncText', 'Suspense', 'p', 'div', 'App']),
],
[
'Switched to client rendering because the server rendering aborted due to:\n\n' +
'foobar',
'a digest',
componentStack(['Suspense', 'span', 'div', 'App']),
componentStack(['AsyncText', 'Suspense', 'span', 'div', 'App']),
],
],
[
Expand Down Expand Up @@ -3606,13 +3625,13 @@ describe('ReactDOMFizzServer', () => {
'Switched to client rendering because the server rendering aborted due to:\n\n' +
'uh oh',
'a digest',
componentStack(['Suspense', 'p', 'div', 'App']),
componentStack(['AsyncText', 'Suspense', 'p', 'div', 'App']),
],
[
'Switched to client rendering because the server rendering aborted due to:\n\n' +
'uh oh',
'a digest',
componentStack(['Suspense', 'span', 'div', 'App']),
componentStack(['AsyncText', 'Suspense', 'span', 'div', 'App']),
],
],
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ describe('ReactDOMFizzServerNode', () => {
let isComplete = false;
let rendered = false;
const promise = new Promise(r => (resolve = r));
function Wait() {
function Wait({prop}) {
if (!hasLoaded) {
throw promise;
}
Expand Down
4 changes: 1 addition & 3 deletions packages/react-html/src/__tests__/ReactHTMLServer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,7 @@ if (!__EXPERIMENTAL__) {
'\n in Bar (at **)' +
'\n in Foo (at **)' +
'\n in div (at **)'
: '\n in Lazy (at **)' +
'\n in div (at **)' +
'\n in div (at **)',
: '\n in div (at **)' + '\n in div (at **)',
);
expect(normalizeCodeLocInfo(caughtErrors[0].ownerStack)).toBe(
__DEV__ && gate(flags => flags.enableOwnerStacks)
Expand Down
1 change: 1 addition & 0 deletions packages/react-reconciler/src/ReactFiberComponentStack.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function describeFiber(fiber: Fiber): string {
case HostComponent:
return describeBuiltInComponentFrame(fiber.type);
case LazyComponent:
// TODO: When we support Thenables as component types we should rename this.
return describeBuiltInComponentFrame('Lazy');
case SuspenseComponent:
return describeBuiltInComponentFrame('Suspense');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -930,4 +930,74 @@ describe('ReactFlightDOMEdge', () => {
'\n in Bar (at **)' + '\n in Foo (at **)',
);
});

it('supports server components in ssr component stacks', async () => {
let reject;
const promise = new Promise((_, r) => (reject = r));
async function Erroring() {
await promise;
return 'should not render';
}

const model = {
root: ReactServer.createElement(Erroring),
};

const stream = ReactServerDOMServer.renderToReadableStream(
model,
webpackMap,
{
onError() {},
},
);

const rootModel = await ReactServerDOMClient.createFromReadableStream(
stream,
{
ssrManifest: {
moduleMap: null,
moduleLoading: null,
},
},
);

const errors = [];
const result = ReactDOMServer.renderToReadableStream(
<div>{rootModel.root}</div>,
{
onError(error, {componentStack}) {
errors.push({
error,
componentStack: normalizeCodeLocInfo(componentStack),
});
},
},
);

const theError = new Error('my error');
reject(theError);

const expectedMessage = __DEV__
? 'my error'
: 'An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about the nature of the error.';

try {
await result;
} catch (x) {
expect(x).toEqual(
expect.objectContaining({
message: expectedMessage,
}),
);
}

expect(errors).toEqual([
{
error: expect.objectContaining({
message: expectedMessage,
}),
componentStack: (__DEV__ ? '\n in Erroring' : '') + '\n in div',
},
]);
});
});
Loading