Skip to content

Commit 22e39ea

Browse files
authored
Include component name in "async/await is not supported" error message if available (#32435)
1 parent 2567726 commit 22e39ea

File tree

4 files changed

+41
-24
lines changed

4 files changed

+41
-24
lines changed

packages/react-reconciler/src/ReactFiberHooks.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,10 +475,13 @@ function warnIfAsyncClientComponent(Component: Function) {
475475
if (!didWarnAboutAsyncClientComponent.has(componentName)) {
476476
didWarnAboutAsyncClientComponent.add(componentName);
477477
console.error(
478-
'async/await is not yet supported in Client Components, only ' +
479-
'Server Components. This error is often caused by accidentally ' +
478+
'%s is an async Client Component. ' +
479+
'Only Server Components can be async at the moment. This error is often caused by accidentally ' +
480480
"adding `'use client'` to a module that was originally written " +
481481
'for the server.',
482+
componentName === null
483+
? 'An unknown Component'
484+
: `<${componentName}>`,
482485
);
483486
}
484487
}

packages/react-reconciler/src/ReactFiberThenable.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,9 @@ export function trackUsedThenable<T>(
194194
// this case include forcing a concurrent render, or putting the whole
195195
// root into offscreen mode.
196196
throw new Error(
197-
'async/await is not yet supported in Client Components, only ' +
198-
'Server Components. This error is often caused by accidentally ' +
197+
'An unknown Component is an async Client Component. ' +
198+
'Only Server Components can be async at the moment. ' +
199+
'This error is often caused by accidentally ' +
199200
"adding `'use client'` to a module that was originally written " +
200201
'for the server.',
201202
);

packages/react-reconciler/src/__tests__/ReactUse-test.js

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,7 +1343,8 @@ describe('ReactUse', () => {
13431343
});
13441344
assertLog(['Async text requested [Hi]']);
13451345
assertConsoleErrorDev([
1346-
'async/await is not yet supported in Client Components, only Server Components. ' +
1346+
'<App> is an async Client Component. ' +
1347+
'Only Server Components can be async at the moment. ' +
13471348
"This error is often caused by accidentally adding `'use client'` " +
13481349
'to a module that was originally written for the server.\n' +
13491350
' in App (at **)',
@@ -1443,7 +1444,8 @@ describe('ReactUse', () => {
14431444
});
14441445
assertLog(['A', 'Mount: A']);
14451446
assertConsoleErrorDev([
1446-
'async/await is not yet supported in Client Components, only Server Components. ' +
1447+
'<App> is an async Client Component. ' +
1448+
'Only Server Components can be async at the moment. ' +
14471449
"This error is often caused by accidentally adding `'use client'` " +
14481450
'to a module that was originally written for the server.\n' +
14491451
' in App (at **)',
@@ -1915,25 +1917,29 @@ describe('ReactUse', () => {
19151917
);
19161918
});
19171919
assertConsoleErrorDev([
1918-
'async/await is not yet supported in Client Components, only Server Components. ' +
1920+
'<AsyncClientComponent> is an async Client Component. ' +
1921+
'Only Server Components can be async at the moment. ' +
19191922
"This error is often caused by accidentally adding `'use client'` " +
19201923
'to a module that was originally written for the server.\n' +
19211924
' in AsyncClientComponent (at **)' +
19221925
(gate('enableOwnerStacks') ? '' : '\n in ErrorBoundary (at **)'),
19231926
]);
19241927
assertLog([
1925-
'async/await is not yet supported in Client Components, only Server ' +
1926-
'Components. This error is often caused by accidentally adding ' +
1928+
'An unknown Component is an async Client Component. ' +
1929+
'Only Server Components can be async at the moment. ' +
1930+
'This error is often caused by accidentally adding ' +
19271931
"`'use client'` to a module that was originally written for " +
19281932
'the server.',
1929-
'async/await is not yet supported in Client Components, only Server ' +
1930-
'Components. This error is often caused by accidentally adding ' +
1933+
'An unknown Component is an async Client Component. ' +
1934+
'Only Server Components can be async at the moment. ' +
1935+
'This error is often caused by accidentally adding ' +
19311936
"`'use client'` to a module that was originally written for " +
19321937
'the server.',
19331938
]);
19341939
expect(root).toMatchRenderedOutput(
1935-
'async/await is not yet supported in Client Components, only Server ' +
1936-
'Components. This error is often caused by accidentally adding ' +
1940+
'An unknown Component is an async Client Component. ' +
1941+
'Only Server Components can be async at the moment. ' +
1942+
'This error is often caused by accidentally adding ' +
19371943
"`'use client'` to a module that was originally written for " +
19381944
'the server.',
19391945
);
@@ -1967,25 +1973,29 @@ describe('ReactUse', () => {
19671973
);
19681974
});
19691975
assertConsoleErrorDev([
1970-
'async/await is not yet supported in Client Components, only Server Components. ' +
1976+
'<AsyncClientComponent> is an async Client Component. ' +
1977+
'Only Server Components can be async at the moment. ' +
19711978
"This error is often caused by accidentally adding `'use client'` " +
19721979
'to a module that was originally written for the server.\n' +
19731980
' in AsyncClientComponent (at **)' +
19741981
(gate('enableOwnerStacks') ? '' : '\n in ErrorBoundary (at **)'),
19751982
]);
19761983
assertLog([
1977-
'async/await is not yet supported in Client Components, only Server ' +
1978-
'Components. This error is often caused by accidentally adding ' +
1984+
'An unknown Component is an async Client Component. ' +
1985+
'Only Server Components can be async at the moment. ' +
1986+
'This error is often caused by accidentally adding ' +
19791987
"`'use client'` to a module that was originally written for " +
19801988
'the server.',
1981-
'async/await is not yet supported in Client Components, only Server ' +
1982-
'Components. This error is often caused by accidentally adding ' +
1989+
'An unknown Component is an async Client Component. ' +
1990+
'Only Server Components can be async at the moment. ' +
1991+
'This error is often caused by accidentally adding ' +
19831992
"`'use client'` to a module that was originally written for " +
19841993
'the server.',
19851994
]);
19861995
expect(root).toMatchRenderedOutput(
1987-
'async/await is not yet supported in Client Components, only Server ' +
1988-
'Components. This error is often caused by accidentally adding ' +
1996+
'An unknown Component is an async Client Component. ' +
1997+
'Only Server Components can be async at the moment. ' +
1998+
'This error is often caused by accidentally adding ' +
19891999
"`'use client'` to a module that was originally written for " +
19902000
'the server.',
19912001
);
@@ -2012,7 +2022,8 @@ describe('ReactUse', () => {
20122022
// decided to warn for _any_ async client component regardless of
20132023
// whether the update is sync. But if we ever add back support for async
20142024
// client components, we should add back the hook warning.
2015-
'async/await is not yet supported in Client Components, only Server Components. ' +
2025+
'<AsyncClientComponent> is an async Client Component. ' +
2026+
'Only Server Components can be async at the moment. ' +
20162027
"This error is often caused by accidentally adding `'use client'` " +
20172028
'to a module that was originally written for the server.\n' +
20182029
' in AsyncClientComponent (at **)',
@@ -2044,7 +2055,8 @@ describe('ReactUse', () => {
20442055
// decided to warn for _any_ async client component regardless of
20452056
// whether the update is sync. But if we ever add back support for async
20462057
// client components, we should add back the hook warning.
2047-
'async/await is not yet supported in Client Components, only Server Components. ' +
2058+
'<AsyncClientComponent> is an async Client Component. ' +
2059+
'Only Server Components can be async at the moment. ' +
20482060
"This error is often caused by accidentally adding `'use client'` " +
20492061
'to a module that was originally written for the server.\n' +
20502062
' in AsyncClientComponent (at **)',
@@ -2079,7 +2091,8 @@ describe('ReactUse', () => {
20792091
});
20802092
});
20812093
assertConsoleErrorDev([
2082-
'async/await is not yet supported in Client Components, only Server Components. ' +
2094+
'<App> is an async Client Component. ' +
2095+
'Only Server Components can be async at the moment. ' +
20832096
"This error is often caused by accidentally adding `'use client'` " +
20842097
'to a module that was originally written for the server.\n' +
20852098
' in App (at **)',

scripts/error-codes/codes.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@
467467
"479": "Cannot update optimistic state while rendering.",
468468
"480": "File/Blob fields are not yet supported in progressive forms. Will fallback to client hydration.",
469469
"481": "Tried to encode a Server Action from a different instance than the encoder is from. This is a bug in React.",
470-
"482": "async/await is not yet supported in Client Components, only Server Components. This error is often caused by accidentally adding `'use client'` to a module that was originally written for the server.",
470+
"482": "An unknown Component is an async Client Component. Only Server Components can be async at the moment. This error is often caused by accidentally adding `'use client'` to a module that was originally written for the server.",
471471
"483": "Hooks are not supported inside an async component. This error is often caused by accidentally adding `'use client'` to a module that was originally written for the server.",
472472
"484": "A Server Component was postponed. The reason is omitted in production builds to avoid leaking sensitive details.",
473473
"485": "Cannot update form state while rendering.",

0 commit comments

Comments
 (0)