Skip to content

Commit 3847592

Browse files
authored
Add docs links to RSC errors (#51557)
When working with React Server Components, it's essential to have a clear understanding of how they function and how to troubleshoot any errors that may arise. This PR adds links to our documentation whenever an error related to React Server Components occurs.
1 parent a101ffe commit 3847592

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

packages/next/src/build/webpack/plugins/wellknown-errors-plugin/parseRSC.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ function formatRSCErrorMessage(
3030
if (NEXT_RSC_ERR_REACT_API.test(message)) {
3131
const matches = message.match(NEXT_RSC_ERR_REACT_API)
3232
if (matches && matches[1] === 'Component') {
33-
formattedMessage = `\n\nYou’re importing a class component. It only works in a Client Component but none of its parents are marked with "use client", so they're Server Components by default.\n\n`
33+
formattedMessage = `\n\nYou’re importing a class component. It only works in a Client Component but none of its parents are marked with "use client", so they're Server Components by default.\nLearn more: https://nextjs.org/docs/getting-started/react-essentials#client-components\n\n`
3434
} else {
3535
formattedMessage = message.replace(
3636
NEXT_RSC_ERR_REACT_API,
37-
`\n\nYou're importing a component that needs $1. It only works in a Client Component but none of its parents are marked with "use client", so they're Server Components by default.\n\n`
37+
`\n\nYou're importing a component that needs $1. It only works in a Client Component but none of its parents are marked with "use client", so they're Server Components by default.\nLearn more: https://nextjs.org/docs/getting-started/react-essentials\n\n`
3838
)
3939
}
4040
formattedVerboseMessage =
@@ -45,17 +45,17 @@ function formatRSCErrorMessage(
4545
switch (matches && matches[1]) {
4646
case 'react-dom/server':
4747
// If importing "react-dom/server", we should show a different error.
48-
formattedMessage = `\n\nYou're importing a component that imports react-dom/server. To fix it, render or return the content directly as a Server Component instead for perf and security.`
48+
formattedMessage = `\n\nYou're importing a component that imports react-dom/server. To fix it, render or return the content directly as a Server Component instead for perf and security.\nLearn more: https://nextjs.org/docs/getting-started/react-essentials`
4949
break
5050
case 'next/router':
5151
// If importing "next/router", we should tell them to use "next/navigation".
52-
formattedMessage = `\n\nYou have a Server Component that imports next/router. Use next/navigation instead.`
52+
formattedMessage = `\n\nYou have a Server Component that imports next/router. Use next/navigation instead.\nLearn more: https://nextjs.org/docs/app/api-reference/functions/use-router`
5353
shouldAddUseClient = false
5454
break
5555
default:
5656
formattedMessage = message.replace(
5757
NEXT_RSC_ERR_SERVER_IMPORT,
58-
`\n\nYou're importing a component that imports $1. It only works in a Client Component but none of its parents are marked with "use client", so they're Server Components by default.\n\n`
58+
`\n\nYou're importing a component that imports $1. It only works in a Client Component but none of its parents are marked with "use client", so they're Server Components by default.\nLearn more: https://nextjs.org/docs/getting-started/react-essentials\n\n`
5959
)
6060
}
6161
formattedVerboseMessage = shouldAddUseClient
@@ -71,7 +71,7 @@ function formatRSCErrorMessage(
7171
} else {
7272
formattedMessage = message.replace(
7373
NEXT_RSC_ERR_CLIENT_IMPORT,
74-
`\n\nYou're importing a component that needs $1. That only works in a Server Component but one of its parents is marked with "use client", so it's a Client Component.\n\n`
74+
`\n\nYou're importing a component that needs $1. That only works in a Server Component but one of its parents is marked with "use client", so it's a Client Component.\nLearn more: https://nextjs.org/docs/getting-started/react-essentials\n\n`
7575
)
7676
formattedVerboseMessage =
7777
'\n\nOne of these is marked as a client entry with "use client":\n'
@@ -85,7 +85,7 @@ function formatRSCErrorMessage(
8585
} else if (NEXT_RSC_ERR_CLIENT_DIRECTIVE_PAREN.test(message)) {
8686
formattedMessage = message.replace(
8787
NEXT_RSC_ERR_CLIENT_DIRECTIVE_PAREN,
88-
`\n\n"use client" must be a directive, and placed before other expressions. Remove the parentheses and move it to the top of the file to resolve this issue.\n\n`
88+
`\n\n"use client" must be a directive, and placed before other expressions. Remove the parentheses and move it to the top of the file to resolve this issue.\nLearn more: https://nextjs.org/docs/getting-started/react-essentials#the-use-client-directive\n\n`
8989
)
9090
formattedVerboseMessage = '\n\nImport path:\n'
9191
} else if (NEXT_RSC_ERR_INVALID_API.test(message)) {
@@ -97,7 +97,7 @@ function formatRSCErrorMessage(
9797
} else if (NEXT_RSC_ERR_ERROR_FILE_SERVER_COMPONENT.test(message)) {
9898
formattedMessage = message.replace(
9999
NEXT_RSC_ERR_ERROR_FILE_SERVER_COMPONENT,
100-
`\n\n${fileName} must be a Client Component. Add the "use client" directive the top of the file to resolve this issue.\n\n`
100+
`\n\n${fileName} must be a Client Component. Add the "use client" directive the top of the file to resolve this issue.\nLearn more: https://nextjs.org/docs/getting-started/react-essentials#client-components\n\n`
101101
)
102102
formattedVerboseMessage = '\n\nImport path:\n'
103103
} else if (NEXT_RSC_ERR_CLIENT_METADATA_EXPORT.test(message)) {

test/development/acceptance-app/rsc-build-errors.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ createNextDescribe(
281281
ReactServerComponentsError:
282282
283283
./app/server-with-errors/error-file/error.js must be a Client Component. Add the \\"use client\\" directive the top of the file to resolve this issue.
284+
Learn more: https://nextjs.org/docs/getting-started/react-essentials#client-components
284285
285286
,-[TEST_DIR/app/server-with-errors/error-file/error.js:1:1]
286287
1 | export default function Error() {}
@@ -402,6 +403,7 @@ createNextDescribe(
402403
ReactServerComponentsError:
403404
404405
You're importing a component that needs useState. It only works in a Client Component but none of its parents are marked with \\"use client\\", so they're Server Components by default.
406+
Learn more: https://nextjs.org/docs/getting-started/react-essentials
405407
406408
,-[TEST_DIR/node_modules/client-package/module2.js:1:1]
407409
1 | import { useState } from 'react'

0 commit comments

Comments
 (0)