Unverified Commit 0a97d078 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Improve error messages (#41669)

Same as #41636 but with a new condition.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `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`
- [ ] Integration 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`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
Co-authored-by: 's avatarJiachi Liu <inbox@huozhi.im>
parent a1a26ca7
...@@ -395,14 +395,9 @@ pub fn server_components<C: Comments>( ...@@ -395,14 +395,9 @@ pub fn server_components<C: Comments>(
JsWord::from("client-only"), JsWord::from("client-only"),
JsWord::from("react-dom/client"), JsWord::from("react-dom/client"),
JsWord::from("react-dom/server"), JsWord::from("react-dom/server"),
// TODO-APP: JsWord::from("next/router"), JsWord::from("next/router"),
// TODO-APP: Rule out client hooks.
],
invalid_client_imports: vec![
JsWord::from("server-only"),
// TODO-APP: Rule out server hooks such as `useCookies`, `useHeaders`,
// `usePreviewData`.
], ],
invalid_client_imports: vec![JsWord::from("server-only"), JsWord::from("next/headers")],
invalid_server_react_dom_apis: vec![ invalid_server_react_dom_apis: vec![
JsWord::from("findDOMNode"), JsWord::from("findDOMNode"),
JsWord::from("flushSync"), JsWord::from("flushSync"),
......
...@@ -25,14 +25,20 @@ export function formatRSCErrorMessage( ...@@ -25,14 +25,20 @@ export function formatRSCErrorMessage(
'\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)) {
const matches = message.match(NEXT_RSC_ERR_SERVER_IMPORT) const matches = message.match(NEXT_RSC_ERR_SERVER_IMPORT)
if (matches && matches[1] === 'react-dom/server') { switch (matches && matches[1]) {
// If importing "react-dom/server", we should show a different error. case 'react-dom/server':
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.` // If importing "react-dom/server", we should show a different error.
} else { 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.`
formattedMessage = message.replace( break
NEXT_RSC_ERR_SERVER_IMPORT, case 'next/router':
`\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` // 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.`
break
default:
formattedMessage = message.replace(
NEXT_RSC_ERR_SERVER_IMPORT,
`\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 =
'\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'
......
'use client'
import { cookies } from 'next/headers'
export default function Page() {
// This should throw an error.
cookies()
return null
}
'use client'
import { headers } from 'next/headers'
export default function Page() {
// This should throw an error.
headers()
return null
}
'use client'
import { previewData } from 'next/headers'
export default function Page() {
// This should throw an error.
previewData()
return null
}
'use client'
import { useRouter, withRouter } from 'next/router'
import IsNull from './is-null'
function ClientRouter({ router: withRouter }) {
const router = useRouter()
return (
<>
<IsNull value={withRouter} />
<IsNull value={router} />
</>
)
}
export default withRouter(ClientRouter)
export default function IsNull({ value }) {
if (value === null) {
return <div className="was-null">Value Was Null</div>
}
return <div className="was-not-null">Value Was Not Null</div>
}
import { cookies } from 'next/headers'
import Router from './router'
export default function Page() {
cookies()
return (
<div id="old-router">
<Router />
</div>
)
}
import { useRouter, withRouter } from 'next/router'
import IsNull from './is-null'
import ServerRouter from './server-router'
import ClientRouter from './client-router'
function SharedRouter({ router: withRouter }) {
const router = useRouter()
return (
<>
<IsNull value={withRouter} />
<IsNull value={router} />
<ServerRouter />
<ClientRouter />
</>
)
}
export default withRouter(SharedRouter)
import { useRouter, withRouter } from 'next/router'
import IsNull from './is-null'
function ServerRouter({ router: withRouter }) {
const router = useRouter()
return (
<>
<IsNull value={withRouter} />
<IsNull value={router} />
</>
)
}
export default withRouter(ServerRouter)
...@@ -893,23 +893,6 @@ describe('app dir', () => { ...@@ -893,23 +893,6 @@ describe('app dir', () => {
}) })
describe('next/router', () => { describe('next/router', () => {
// `useRouter` should not be accessible in server components.
it('should always return null when accessed from /app', async () => {
const browser = await webdriver(next.url, '/old-router')
try {
await browser.waitForElementByCss('#old-router')
const notNull = await browser.elementsByCss('.was-not-null')
expect(notNull.length).toBe(0)
const wasNull = await browser.elementsByCss('.was-null')
expect(wasNull.length).toBe(6)
} finally {
await browser.close()
}
})
it('should support router.back and router.forward', async () => { it('should support router.back and router.forward', async () => {
const browser = await webdriver(next.url, '/back-forward/1') const browser = await webdriver(next.url, '/back-forward/1')
...@@ -1134,42 +1117,6 @@ describe('app dir', () => { ...@@ -1134,42 +1117,6 @@ describe('app dir', () => {
describe('client components', () => { describe('client components', () => {
describe('hooks', () => { describe('hooks', () => {
describe('cookies function', () => {
// TODO-APP: should enable when implemented
it.skip('should throw an error when imported', async () => {
const res = await fetchViaHTTP(
next.url,
'/hooks/use-cookies/client'
)
expect(res.status).toBe(500)
expect(await res.text()).toContain('Internal Server Error')
})
})
describe('previewData function', () => {
// TODO-APP: should enable when implemented
it.skip('should throw an error when imported', async () => {
const res = await fetchViaHTTP(
next.url,
'/hooks/use-preview-data/client'
)
expect(res.status).toBe(500)
expect(await res.text()).toContain('Internal Server Error')
})
})
describe('headers function', () => {
// TODO-APP: should enable when implemented
it.skip('should throw an error when imported', async () => {
const res = await fetchViaHTTP(
next.url,
'/hooks/use-headers/client'
)
expect(res.status).toBe(500)
expect(await res.text()).toContain('Internal Server Error')
})
})
describe('usePathname', () => { describe('usePathname', () => {
it('should have the correct pathname', async () => { it('should have the correct pathname', async () => {
const html = await renderViaHTTP(next.url, '/hooks/use-pathname') const html = await renderViaHTTP(next.url, '/hooks/use-pathname')
......
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