Unverified Commit 66478522 authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

Improve RSC error messages (#90702)

Improve the RSC error messages to be more comprehensive.

x-ref:
https://github.com/vercel/next.js/pull/89688#discussion_r2865847895

---------
Co-authored-by: 's avatarClaude Haiku 4.5 <noreply@anthropic.com>
parent a060fbf9
......@@ -328,9 +328,9 @@ fn report_error(app_dir: &Option<PathBuf>, filepath: &str, error_kind: RSCErrorK
.unwrap_or_default();
let msg = if !is_app_dir {
format!("You're importing a component that needs \"{source}\". That only works in a Server Component which is not supported in the pages/ directory. Read more: https://nextjs.org/docs/app/building-your-application/rendering/server-components\n\n")
format!("You're importing a module that depends on \"{source}\". This API is only available in Server Components in the App Router, but you are using it in the Pages Router.\nLearn more: https://nextjs.org/docs/app/building-your-application/rendering/server-components\n\n")
} else {
format!("You're importing a component that needs \"{source}\". 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/app/building-your-application/rendering\n\n")
format!("You're importing a module that depends on \"{source}\" into a React Client Component module. This API is only available in Server Components but one of its parents is marked with \"use client\", so this module is also a Client Component.\nLearn more: https://nextjs.org/docs/app/building-your-application/rendering\n\n")
};
(msg, vec![span])
}
......@@ -338,7 +338,7 @@ fn report_error(app_dir: &Option<PathBuf>, filepath: &str, error_kind: RSCErrorK
let msg = if source == "Component" {
"You’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/app/building-your-application/rendering/client-components\n\n".to_string()
} else {
format!("You're importing a component that needs `{source}`. This React Hook only works in a Client Component. To fix, mark the file (or its parent) with the `\"use client\"` directive.\n\n Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client\n\n")
format!("You're importing a module that depends on `{source}` into a React Server Component module. This API is only available in Client Components. To fix, mark the file (or its parent) with the `\"use client\"` directive.\nLearn more: https://nextjs.org/docs/app/api-reference/directives/use-client\n\n")
};
(msg, vec![span])
......
x You're importing a component that needs "cacheLife". That only works in a Server Component but one of its parents is marked with "use client", so it's a Client Component.
x You're importing a module that depends on "cacheLife" into a React Client Component module. This API is only available in Server Components but one of its parents is marked with "use client", so
| this module is also a Client Component.
| Learn more: https://nextjs.org/docs/app/building-your-application/rendering
|
|
......
x You're importing a component that needs "cacheTag". That only works in a Server Component but one of its parents is marked with "use client", so it's a Client Component.
x You're importing a module that depends on "cacheTag" into a React Client Component module. This API is only available in Server Components but one of its parents is marked with "use client", so
| this module is also a Client Component.
| Learn more: https://nextjs.org/docs/app/building-your-application/rendering
|
|
......
x You're importing a component that needs "next/root-params". That only works in a Server Component but one of its parents is marked with "use client", so it's a Client Component.
x You're importing a module that depends on "next/root-params" into a React Client Component module. This API is only available in Server Components but one of its parents is marked with "use
| client", so this module is also a Client Component.
| Learn more: https://nextjs.org/docs/app/building-your-application/rendering
|
|
......
x You're importing a component that needs "server-only". That only works in a Server Component but one of its parents is marked with "use client", so it's a Client Component.
x You're importing a module that depends on "server-only" into a React Client Component module. This API is only available in Server Components but one of its parents is marked with "use client",
| so this module is also a Client Component.
| Learn more: https://nextjs.org/docs/app/building-your-application/rendering
|
|
......
x You're importing a component that needs "cacheLife". That only works in a Server Component which is not supported in the pages/ directory. Read more: https://nextjs.org/docs/app/building-your-
| application/rendering/server-components
x You're importing a module that depends on "cacheLife". This API is only available in Server Components in the App Router, but you are using it in the Pages Router.
| Learn more: https://nextjs.org/docs/app/building-your-application/rendering/server-components
|
|
,-[input.js:1:1]
......
x You're importing a component that needs "cacheTag". That only works in a Server Component which is not supported in the pages/ directory. Read more: https://nextjs.org/docs/app/building-your-
| application/rendering/server-components
x You're importing a module that depends on "cacheTag". This API is only available in Server Components in the App Router, but you are using it in the Pages Router.
| Learn more: https://nextjs.org/docs/app/building-your-application/rendering/server-components
|
|
,-[input.js:1:1]
......
x You're importing a component that needs "next/root-params". That only works in a Server Component which is not supported in the pages/ directory. Read more: https://nextjs.org/docs/app/building-
| your-application/rendering/server-components
x You're importing a module that depends on "next/root-params". This API is only available in Server Components in the App Router, but you are using it in the Pages Router.
| Learn more: https://nextjs.org/docs/app/building-your-application/rendering/server-components
|
|
,-[input.js:9:1]
......
x You're importing a component that needs "server-only". That only works in a Server Component which is not supported in the pages/ directory. Read more: https://nextjs.org/docs/app/building-your-
| application/rendering/server-components
x You're importing a module that depends on "server-only". This API is only available in Server Components in the App Router, but you are using it in the Pages Router.
| Learn more: https://nextjs.org/docs/app/building-your-application/rendering/server-components
|
|
,-[input.js:9:1]
......
x You're importing a component that needs `useState`. This React Hook only works in a Client Component. To fix, mark the file (or its parent) with the `"use client"` directive.
|
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
x You're importing a module that depends on `useState` into a React Server Component module. This API is only available in Client Components. To fix, mark the file (or its parent) with the `"use
| client"` directive.
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
|
|
,-[input.js:1:1]
1 | import { useState } from 'react'
: ^^^^^^^^
`----
x You're importing a component that needs `createContext`. This React Hook only works in a Client Component. To fix, mark the file (or its parent) with the `"use client"` directive.
|
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
x You're importing a module that depends on `createContext` into a React Server Component module. This API is only available in Client Components. To fix, mark the file (or its parent) with the
| `"use client"` directive.
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
|
|
,-[input.js:3:1]
......@@ -17,9 +17,9 @@
3 | import { createContext } from 'react'
: ^^^^^^^^^^^^^
`----
x You're importing a component that needs `useEffect`. This React Hook only works in a Client Component. To fix, mark the file (or its parent) with the `"use client"` directive.
|
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
x You're importing a module that depends on `useEffect` into a React Server Component module. This API is only available in Client Components. To fix, mark the file (or its parent) with the `"use
| client"` directive.
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
|
|
,-[input.js:5:1]
......@@ -27,9 +27,9 @@
5 | import { useEffect, useImperativeHandle } from 'react'
: ^^^^^^^^^
`----
x You're importing a component that needs `useImperativeHandle`. This React Hook only works in a Client Component. To fix, mark the file (or its parent) with the `"use client"` directive.
|
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
x You're importing a module that depends on `useImperativeHandle` into a React Server Component module. This API is only available in Client Components. To fix, mark the file (or its parent) with
| the `"use client"` directive.
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
|
|
,-[input.js:5:1]
......@@ -47,9 +47,9 @@
: ^^^^^^^^^
9 | createFactory,
`----
x You're importing a component that needs `createFactory`. This React Hook only works in a Client Component. To fix, mark the file (or its parent) with the `"use client"` directive.
|
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
x You're importing a module that depends on `createFactory` into a React Server Component module. This API is only available in Client Components. To fix, mark the file (or its parent) with the
| `"use client"` directive.
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
|
|
,-[input.js:9:1]
......@@ -58,9 +58,9 @@
: ^^^^^^^^^^^^^
10 | PureComponent,
`----
x You're importing a component that needs `PureComponent`. This React Hook only works in a Client Component. To fix, mark the file (or its parent) with the `"use client"` directive.
|
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
x You're importing a module that depends on `PureComponent` into a React Server Component module. This API is only available in Client Components. To fix, mark the file (or its parent) with the
| `"use client"` directive.
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
|
|
,-[input.js:10:1]
......@@ -69,9 +69,9 @@
: ^^^^^^^^^^^^^
11 | useDeferredValue,
`----
x You're importing a component that needs `useDeferredValue`. This React Hook only works in a Client Component. To fix, mark the file (or its parent) with the `"use client"` directive.
|
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
x You're importing a module that depends on `useDeferredValue` into a React Server Component module. This API is only available in Client Components. To fix, mark the file (or its parent) with the
| `"use client"` directive.
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
|
|
,-[input.js:11:1]
......@@ -80,9 +80,9 @@
: ^^^^^^^^^^^^^^^^
12 | useInsertionEffect,
`----
x You're importing a component that needs `useInsertionEffect`. This React Hook only works in a Client Component. To fix, mark the file (or its parent) with the `"use client"` directive.
|
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
x You're importing a module that depends on `useInsertionEffect` into a React Server Component module. This API is only available in Client Components. To fix, mark the file (or its parent) with
| the `"use client"` directive.
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
|
|
,-[input.js:12:1]
......@@ -91,9 +91,9 @@
: ^^^^^^^^^^^^^^^^^^
13 | useLayoutEffect,
`----
x You're importing a component that needs `useLayoutEffect`. This React Hook only works in a Client Component. To fix, mark the file (or its parent) with the `"use client"` directive.
|
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
x You're importing a module that depends on `useLayoutEffect` into a React Server Component module. This API is only available in Client Components. To fix, mark the file (or its parent) with the
| `"use client"` directive.
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
|
|
,-[input.js:13:1]
......@@ -102,9 +102,9 @@
: ^^^^^^^^^^^^^^^
14 | useReducer,
`----
x You're importing a component that needs `useReducer`. This React Hook only works in a Client Component. To fix, mark the file (or its parent) with the `"use client"` directive.
|
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
x You're importing a module that depends on `useReducer` into a React Server Component module. This API is only available in Client Components. To fix, mark the file (or its parent) with the `"use
| client"` directive.
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
|
|
,-[input.js:14:1]
......@@ -113,9 +113,9 @@
: ^^^^^^^^^^
15 | useRef,
`----
x You're importing a component that needs `useRef`. This React Hook only works in a Client Component. To fix, mark the file (or its parent) with the `"use client"` directive.
|
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
x You're importing a module that depends on `useRef` into a React Server Component module. This API is only available in Client Components. To fix, mark the file (or its parent) with the `"use
| client"` directive.
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
|
|
,-[input.js:15:1]
......@@ -124,9 +124,9 @@
: ^^^^^^
16 | useSyncExternalStore,
`----
x You're importing a component that needs `useSyncExternalStore`. This React Hook only works in a Client Component. To fix, mark the file (or its parent) with the `"use client"` directive.
|
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
x You're importing a module that depends on `useSyncExternalStore` into a React Server Component module. This API is only available in Client Components. To fix, mark the file (or its parent) with
| the `"use client"` directive.
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
|
|
,-[input.js:16:1]
......@@ -135,9 +135,9 @@
: ^^^^^^^^^^^^^^^^^^^^
17 | } from 'react'
`----
x You're importing a component that needs `experimental_useOptimistic`. This React Hook only works in a Client Component. To fix, mark the file (or its parent) with the `"use client"` directive.
|
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
x You're importing a module that depends on `experimental_useOptimistic` into a React Server Component module. This API is only available in Client Components. To fix, mark the file (or its
| parent) with the `"use client"` directive.
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
|
|
,-[input.js:19:1]
......
x You're importing a component that needs `flushSync`. This React Hook only works in a Client Component. To fix, mark the file (or its parent) with the `"use client"` directive.
|
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
x You're importing a module that depends on `flushSync` into a React Server Component module. This API is only available in Client Components. To fix, mark the file (or its parent) with the `"use
| client"` directive.
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
|
|
,-[input.js:1:1]
1 | import { flushSync, unstable_batchedUpdates } from 'react-dom'
: ^^^^^^^^^
`----
x You're importing a component that needs `unstable_batchedUpdates`. This React Hook only works in a Client Component. To fix, mark the file (or its parent) with the `"use client"` directive.
|
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
x You're importing a module that depends on `unstable_batchedUpdates` into a React Server Component module. This API is only available in Client Components. To fix, mark the file (or its parent)
| with the `"use client"` directive.
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
|
|
,-[input.js:1:1]
1 | import { flushSync, unstable_batchedUpdates } from 'react-dom'
: ^^^^^^^^^^^^^^^^^^^^^^^
`----
x You're importing a component that needs `useActionState`. This React Hook only works in a Client Component. To fix, mark the file (or its parent) with the `"use client"` directive.
|
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
x You're importing a module that depends on `useActionState` into a React Server Component module. This API is only available in Client Components. To fix, mark the file (or its parent) with the
| `"use client"` directive.
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
|
|
,-[input.js:3:1]
......@@ -26,9 +26,9 @@
3 | import { useActionState } from 'react'
: ^^^^^^^^^^^^^^
`----
x You're importing a component that needs `useFormStatus`. This React Hook only works in a Client Component. To fix, mark the file (or its parent) with the `"use client"` directive.
|
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
x You're importing a module that depends on `useFormStatus` into a React Server Component module. This API is only available in Client Components. To fix, mark the file (or its parent) with the
| `"use client"` directive.
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
|
|
,-[input.js:5:1]
......@@ -36,9 +36,9 @@
5 | import { useFormStatus, useFormState } from 'react-dom'
: ^^^^^^^^^^^^^
`----
x You're importing a component that needs `useFormState`. This React Hook only works in a Client Component. To fix, mark the file (or its parent) with the `"use client"` directive.
|
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
x You're importing a module that depends on `useFormState` into a React Server Component module. This API is only available in Client Components. To fix, mark the file (or its parent) with the
| `"use client"` directive.
| Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
|
|
,-[input.js:5:1]
......
......@@ -230,7 +230,7 @@ describe('Error overlay - RSC build errors', () => {
// `Component` has a custom error message
api === 'Component'
? `You’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.`
: `You're importing a component that needs \`${api}\`. This React Hook only works in a Client Component. To fix, mark the file (or its parent) with the \`"use client"\` directive.`
: `You're importing a module that depends on \`${api}\` into a React Server Component module. This API is only available in Client Components. To fix, mark the file (or its parent) with the \`"use client"\` directive.`
)
})
}
......@@ -251,7 +251,7 @@ describe('Error overlay - RSC build errors', () => {
const { session } = sandbox
await session.waitForRedbox()
expect(await session.getRedboxSource()).toInclude(
`You're importing a component that needs \`${api}\`. This React Hook only works in a Client Component. To fix, mark the file (or its parent) with the \`"use client"\` directive.`
`You're importing a module that depends on \`${api}\` into a React Server Component module. This API is only available in Client Components. To fix, mark the file (or its parent) with the \`"use client"\` directive.`
)
})
}
......@@ -276,7 +276,7 @@ describe('Error overlay - RSC build errors', () => {
const { session } = sandbox
await session.waitForRedbox()
expect(await session.getRedboxSource()).toInclude(
'You\'re importing a component that needs `unstable_catchError`. This React Hook only works in a Client Component. To fix, mark the file (or its parent) with the `"use client"` directive.'
'You\'re importing a module that depends on `unstable_catchError` into a React Server Component module. This API is only available in Client Components. To fix, mark the file (or its parent) with the `"use client"` directive.'
)
})
......@@ -311,7 +311,7 @@ describe('Error overlay - RSC build errors', () => {
const { session } = sandbox
await session.waitForRedbox()
expect(await session.getRedboxSource()).toInclude(
'You\'re importing a component that needs `unstable_catchError`. This React Hook only works in a Client Component. To fix, mark the file (or its parent) with the `"use client"` directive.'
'You\'re importing a module that depends on `unstable_catchError` into a React Server Component module. This API is only available in Client Components. To fix, mark the file (or its parent) with the `"use client"` directive.'
)
}
)
......@@ -335,7 +335,7 @@ describe('Error overlay - RSC build errors', () => {
await session.waitForRedbox()
expect(await session.getRedboxSource()).toInclude(
`You're importing a component that needs "server-only". That only works in a Server Component but one of its parents is marked with "use client", so it's a Client Component.`
`You're importing a module that depends on "server-only" into a React Client Component module. This API is only available in Server Components but one of its parents is marked with "use client", so this module is also a Client Component.`
)
})
......@@ -351,7 +351,7 @@ describe('Error overlay - RSC build errors', () => {
const { session } = sandbox
await session.waitForRedbox()
expect(await session.getRedboxSource()).toInclude(
`You're importing a component that needs "${api}". That only works in a Server Component but one of its parents is marked with "use client", so it's a Client Component.`
`You're importing a module that depends on "${api}" into a React Client Component module. This API is only available in Server Components but one of its parents is marked with "use client", so this module is also a Client Component.`
)
}
)
......@@ -414,7 +414,7 @@ describe('Error overlay - RSC build errors', () => {
const { session } = sandbox
await session.waitForRedbox()
expect(await session.getRedboxSource()).toInclude(
`You're importing a component that needs "next/root-params". That only works in a Server Component but one of its parents is marked with "use client", so it's a Client Component.`
`You're importing a module that depends on "next/root-params" into a React Client Component module. This API is only available in Server Components but one of its parents is marked with "use client", so this module is also a Client Component.`
)
})
......
......@@ -525,7 +525,7 @@ describe('Error Overlay for server components', () => {
// So we need to check for the first part of the message.
const normalizedSource = await session.getRedboxSource()
expect(normalizedSource).toContain(
`You're importing a component that needs \`${hook}\`. This React Hook only works in a Client Component. To fix, mark the file (or its parent) with the \`"use client"\` directive.`
`You're importing a module that depends on \`${hook}\` into a React Server Component module. This API is only available in Client Components. To fix, mark the file (or its parent) with the \`"use client"\` directive.`
)
expect(normalizedSource).toContain(
`import { ${hook} } from 'next/navigation'`
......@@ -557,7 +557,7 @@ describe('Error Overlay for server components', () => {
// So we need to check for the first part of the message.
const normalizedSource = await session.getRedboxSource()
expect(normalizedSource).toContain(
`You're importing a component that needs \`${hook}\`. This React Hook only works in a Client Component. To fix, mark the file (or its parent) with the \`"use client"\` directive.`
`You're importing a module that depends on \`${hook}\` into a React Server Component module. This API is only available in Client Components. To fix, mark the file (or its parent) with the \`"use client"\` directive.`
)
expect(normalizedSource).toContain(
`import { ${hook} } from 'next/link'`
......
......@@ -27,7 +27,7 @@ describe('after() - invalid usages', () => {
await waitForRedbox(session)
expect(await getRedboxSource(session)).toMatch(
/You're importing a component that needs "?after"?\. That only works in a Server Component but one of its parents is marked with "use client", so it's a Client Component\./
/You're importing a module that depends on "?after"? into a React Client Component module\. This API is only available in Server Components but one of its parents is marked with "use client", so this module is also a Client Component\./
)
expect(getAfterLogs()).toHaveLength(0)
})
......
......@@ -62,7 +62,7 @@ _describe('after() - pages', () => {
await waitForRedbox(browser)
expect(await getRedboxSource(browser)).toMatch(
/You're importing a component that needs "?after"?\. That only works in a Server Component which is not supported in the pages\/ directory\./
/You're importing a module that depends on "?after"?\. This API is only available in Server Components in the App Router, but you are using it in the Pages Router\./
)
expect(getLogs()).toHaveLength(0)
})
......
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