Unverified Commit 485955bb authored by Shu Ding's avatar Shu Ding Committed by GitHub

Do not suggest adding "use client" if using next/router in app (#48680)

Feedback form @MaxLeiter, that we shouldn't tell the user to add "use client" in this case:

![image](https://user-images.githubusercontent.com/3676859/233697824-65e8013b-f292-4f77-bdff-d421269ba750.png)
parent a875d5a4
...@@ -40,6 +40,7 @@ function formatRSCErrorMessage( ...@@ -40,6 +40,7 @@ function formatRSCErrorMessage(
formattedVerboseMessage = formattedVerboseMessage =
'\n\nMaybe one of these should be marked as a client entry with "use client":\n' '\n\nMaybe one of these should be marked as a client entry with "use client":\n'
} else if (NEXT_RSC_ERR_SERVER_IMPORT.test(message)) { } else if (NEXT_RSC_ERR_SERVER_IMPORT.test(message)) {
let shouldAddUseClient = true
const matches = message.match(NEXT_RSC_ERR_SERVER_IMPORT) const matches = message.match(NEXT_RSC_ERR_SERVER_IMPORT)
switch (matches && matches[1]) { switch (matches && matches[1]) {
case 'react-dom/server': case 'react-dom/server':
...@@ -49,6 +50,7 @@ function formatRSCErrorMessage( ...@@ -49,6 +50,7 @@ function formatRSCErrorMessage(
case 'next/router': case 'next/router':
// If importing "next/router", we should tell them to use "next/navigation". // If importing "next/router", we should tell them to use "next/navigation".
formattedMessage = `\n\nYou have a Server Component that imports next/router. Use next/navigation instead.` formattedMessage = `\n\nYou have a Server Component that imports next/router. Use next/navigation instead.`
shouldAddUseClient = false
break break
default: default:
formattedMessage = message.replace( formattedMessage = message.replace(
...@@ -56,8 +58,9 @@ function formatRSCErrorMessage( ...@@ -56,8 +58,9 @@ function formatRSCErrorMessage(
`\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` `\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`
) )
} }
formattedVerboseMessage = formattedVerboseMessage = shouldAddUseClient
'\n\nMaybe one of these should be marked as a client entry "use client":\n' ? '\n\nMaybe one of these should be marked as a client entry "use client":\n'
: '\n\nImport trace:\n'
} else if (NEXT_RSC_ERR_CLIENT_IMPORT.test(message)) { } else if (NEXT_RSC_ERR_CLIENT_IMPORT.test(message)) {
if (isPagesDir) { if (isPagesDir) {
formattedMessage = message.replace( formattedMessage = message.replace(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment