Unverified Commit c0b7f72b authored by Janka Uryga's avatar Janka Uryga Committed by GitHub

compile-time error for useLinkStatus imports in server components (#77962)

Adds `useLinkStatus` to the list of APIs that can't be imported in
server components.
parent 1633c4ac
......@@ -626,6 +626,7 @@ impl ReactServerComponentValidator {
"ServerInsertedHTMLContext",
],
),
("next/link", vec!["useLinkStatus"]),
]),
deprecated_apis_mapping: FxHashMap::from_iter([("next/server", vec!["ImageResponse"])]),
......
......@@ -532,4 +532,37 @@ describe('Error Overlay for server components', () => {
)
})
})
describe('Next.js link client hooks called in Server Component', () => {
it.each([['useLinkStatus']])(
'should show error when %s is called',
async (hook: string) => {
await using sandbox = await createSandbox(
next,
new Map([
[
'app/page.js',
outdent`
import { ${hook} } from 'next/link'
export default function Page() {
return "Hello world"
}
`,
],
])
)
const { session } = sandbox
await session.assertHasRedbox()
// In webpack when the message too long it gets truncated with ` | ` with new lines.
// 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.`
)
expect(normalizedSource).toContain(
`import { ${hook} } from 'next/link'`
)
}
)
})
})
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