Unverified Commit 84531c53 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Add special error message for class component in server component (#44265)

This PR adjusts the error message so it's more accurate and makes more sense. Current error:

> You're importing a component that needs Component.

New:

> You’re importing a class component.

NEXT-317

## Bug

- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent 4ac737ee
......@@ -19,10 +19,15 @@ function formatRSCErrorMessage(message: string): null | [string, string] {
const NEXT_RSC_ERR_INVALID_API = /.+NEXT_RSC_ERR_INVALID_API: (.*?)\n/s
if (NEXT_RSC_ERR_REACT_API.test(message)) {
formattedMessage = message.replace(
NEXT_RSC_ERR_REACT_API,
`\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`
)
const matches = message.match(NEXT_RSC_ERR_REACT_API)
if (matches && matches[1] === 'Component') {
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`
} else {
formattedMessage = message.replace(
NEXT_RSC_ERR_REACT_API,
`\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`
)
}
formattedVerboseMessage =
'\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)) {
......
// import { Component } from 'react'
export default function Page() {
return null
}
......@@ -92,6 +92,23 @@ if (!(globalThis as any).isNextDev) {
'directive must be placed before other expressions'
)
})
it('should throw an error when "Component" is imported in server components', async () => {
const pageFile = 'app/server-with-errors/class-component/page.js'
const content = await next.readFile(pageFile)
const uncomment = content.replace(
"// import { Component } from 'react'",
"import { Component } from 'react'"
)
await next.patchFile(pageFile, uncomment)
const res = await next.fetch('/server-with-errors/class-component')
await next.patchFile(pageFile, content)
expect(res.status).toBe(500)
expect(await res.text()).toContain(
`You’re importing a class component. It only works in a Client Component`
)
})
}
)
}
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