Unverified Commit 5b4d844a authored by Janka Uryga's avatar Janka Uryga Committed by GitHub

test: allow-runtime breaks Link's Server component child detection (#95596)

See NAR-876. We discovered this problem in #95384, when removing the
Early/Late stage split, but it turns out that this was a preexisting
race that occurs for runtime-prefetchable segments the problem existed
before for runtime (which currently render in EarlyStatic, a task
earlier than non-runtime-prefetchable segments)

adds failing tests for `passHref` with allow-runtime (i.e. when the
segment runs early), and passing tests for it in the dynamic stage (when
the passHref is gated behind dynamic, which seems to delay it long
enough for the debug channel to initialize)
parent 3ebd45b1
import Link from 'next/link' import Link from 'next/link'
import CustomComponent from './_client' import CustomComponent from '../_client'
export default function Page() { export default function Page() {
return ( return (
......
import Link from 'next/link'
import CustomComponent from '../_client'
import { Suspense } from 'react'
import { connection } from 'next/server'
export default function Page() {
return (
<Suspense>
<Dynamic />
</Suspense>
)
}
async function Dynamic() {
await connection()
return (
<Link href="/about" legacyBehavior passHref>
<CustomComponent>About</CustomComponent>
</Link>
)
}
import Link from 'next/link'
import CustomComponent from '../_client'
export const prefetch = 'allow-runtime'
export default function Page() {
return (
<>
<Link href="/about" legacyBehavior passHref>
<CustomComponent>About</CustomComponent>
</Link>
</>
)
}
import { ClientLink } from '../client-link' import { ClientLink } from '../../client-link'
import ClientA from './_client' import ClientA from '../_client'
export default function Page() { export default function Page() {
return ( return (
......
import { connection } from 'next/server'
import { ClientLink } from '../../client-link'
import ClientA from '../_client'
import { Suspense } from 'react'
export default function Page() {
return (
<Suspense>
<Dynamic />
</Suspense>
)
}
async function Dynamic() {
await connection()
return (
<ClientLink href="/about">
<ClientA>
<RSC />
</ClientA>
</ClientLink>
)
}
async function RSC() {
return <span>About</span>
}
import { ClientLink } from '../../client-link'
import ClientA from '../_client'
export const prefetch = 'allow-runtime'
export default function Page() {
return (
<>
<ClientLink href="/about">
<ClientA>
<RSC />
</ClientA>
</ClientLink>
</>
)
}
async function RSC() {
return <span>About</span>
}
import { ClientLink } from '../client-link' import { ClientLink } from '../../client-link'
import ClientA from './_client' import ClientA from '../_client'
export default function Page() { export default function Page() {
return ( return (
......
import { connection } from 'next/server'
import { ClientLink } from '../../client-link'
import ClientA from '../_client'
import { Suspense } from 'react'
export default function Page() {
return (
<Suspense>
<Dynamic />
</Suspense>
)
}
async function Dynamic() {
await connection()
return (
<ClientLink href="/about">
<ClientA>About</ClientA>
</ClientLink>
)
}
import { ClientLink } from '../../client-link'
import ClientA from '../_client'
export const prefetch = 'allow-runtime'
export default function Page() {
return (
<>
<ClientLink href="/about">
<ClientA>About</ClientA>
</ClientLink>
</>
)
}
import Link from 'next/link' import Link from 'next/link'
import { ClientComponent } from './_client' import { ClientComponent } from '../_client'
export default function Page() { export default function Page() {
return ( return (
......
import Link from 'next/link'
import { ClientComponent } from '../_client'
import { connection } from 'next/server'
import { Suspense } from 'react'
export default function Page() {
return (
<Suspense>
<Dynamic />
</Suspense>
)
}
async function Dynamic() {
await connection()
return (
<Link href="/about" legacyBehavior passHref>
<ClientComponent>
<RSC />
</ClientComponent>
</Link>
)
}
function RSC() {
return <span>About</span>
}
import Link from 'next/link'
import { ClientComponent } from '../_client'
export const prefetch = 'allow-runtime'
export default function Page() {
return (
<>
<Link href="/about" legacyBehavior passHref>
<ClientComponent>
<RSC />
</ClientComponent>
</Link>
</>
)
}
function RSC() {
return <span>About</span>
}
import Link from 'next/link' import Link from 'next/link'
import { ClientComponent } from './_client' import { ClientComponent } from '../_client'
export default function Page() { export default function Page() {
return ( return (
......
import Link from 'next/link'
import { ClientComponent } from '../_client'
import { Suspense } from 'react'
import { connection } from 'next/server'
export default function Page() {
return (
<Suspense>
<Dynamic />
</Suspense>
)
}
async function Dynamic() {
await connection()
return (
<Link href="/about" legacyBehavior passHref>
<ClientComponent>About</ClientComponent>
</Link>
)
}
import Link from 'next/link'
import { ClientComponent } from '../_client'
export const prefetch = 'allow-runtime'
export default function Page() {
return (
<>
<Link href="/about" legacyBehavior passHref>
<ClientComponent>About</ClientComponent>
</Link>
</>
)
}
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils' import { openRedbox, retry } from 'next-test-utils'
import {
createRedboxSnapshot,
type ErrorSnapshot,
} from '../../lib/add-redbox-matchers'
describe('Link with legacyBehavior', () => { describe('Link with legacyBehavior', () => {
const { next, isNextDev, skipped } = nextTestSetup({ const { next, isNextDev, skipped } = nextTestSetup({
...@@ -14,7 +18,7 @@ describe('Link with legacyBehavior', () => { ...@@ -14,7 +18,7 @@ describe('Link with legacyBehavior', () => {
describe('if the child is an <a> tag', () => { describe('if the child is an <a> tag', () => {
it('forwards the href attribute', async () => { it('forwards the href attribute', async () => {
const $ = await next.render$('/') const $ = await next.render$('/')
const $a = $('a') const $a = $('a[href="/about"]')
expect($a.text()).toBe('About') expect($a.text()).toBe('About')
expect($a.attr('href')).toBe('/about') expect($a.attr('href')).toBe('/about')
...@@ -22,7 +26,7 @@ describe('Link with legacyBehavior', () => { ...@@ -22,7 +26,7 @@ describe('Link with legacyBehavior', () => {
it('navigates correctly', async () => { it('navigates correctly', async () => {
const browser = await next.browser('/') const browser = await next.browser('/')
await browser.elementByCss('a').click() await browser.elementByCss('a[href="/about"]').click()
const title = await browser.elementByCss('#about-page').text() const title = await browser.elementByCss('#about-page').text()
expect(title).toBe('About Page') expect(title).toBe('About Page')
...@@ -31,7 +35,7 @@ describe('Link with legacyBehavior', () => { ...@@ -31,7 +35,7 @@ describe('Link with legacyBehavior', () => {
it('works if the child is a number', async () => { it('works if the child is a number', async () => {
const browser = await next.browser('/child-is-a-number') const browser = await next.browser('/child-is-a-number')
await browser.elementByCss('a').click() await browser.elementByCss('a[href="/about"]').click()
const title = await browser.elementByCss('h1').text() const title = await browser.elementByCss('h1').text()
expect(title).toBe('About Page') expect(title).toBe('About Page')
...@@ -39,7 +43,7 @@ describe('Link with legacyBehavior', () => { ...@@ -39,7 +43,7 @@ describe('Link with legacyBehavior', () => {
it('works if the child is a string', async () => { it('works if the child is a string', async () => {
const browser = await next.browser('/child-is-a-string') const browser = await next.browser('/child-is-a-string')
await browser.elementByCss('a').click() await browser.elementByCss('a[href="/about"]').click()
const title = await browser.elementByCss('h1').text() const title = await browser.elementByCss('h1').text()
expect(title).toBe('About Page') expect(title).toBe('About Page')
...@@ -76,20 +80,85 @@ describe('Link with legacyBehavior', () => { ...@@ -76,20 +80,85 @@ describe('Link with legacyBehavior', () => {
}) })
describe('passHref', () => { describe('passHref', () => {
it('forwards the href attribute', async () => { const expectHrefToBeForwardedInSSR = async (path: string) => {
const $ = await next.render$('/passHref') const $ = await next.render$(path)
const $a = $('a') const $a = $('a[href="/about"]')
expect($a.text()).toBe('About') expect($a.text()).toBe('About')
expect($a.attr('href')).toBe('/about') expect($a.attr('href')).toBe('/about')
}
const expectLinkClickToNavigate = async (path: string) => {
const browser = await next.browser(path)
if (isNextDev) {
// We expect a deprecation warning (in a collapsed redbox), but no other errors (e.g. no errors thrown by Link)
await openRedbox(browser)
expect(await createRedboxSnapshot(browser, next)).toEqual(
expect.objectContaining<Partial<ErrorSnapshot>>({
label: 'Console Error',
description: expect.stringContaining(
`\`legacyBehavior\` is deprecated and will be removed in a future release.`
),
}) })
)
await browser.locateRedbox().press('Escape') // Close redbox so we can click the link
}
it('navigates correctly', async () => { await browser.elementByCss('a[href="/about"]').click()
const browser = await next.browser('/passHref')
await browser.elementByCss('a').click()
const title = await browser.elementByCss('h1').text()
const title = await browser.elementByCss('h1').text()
expect(title).toBe('About Page') expect(title).toBe('About Page')
}
describe('with no prefech config', () => {
it('forwards the href attribute', async () => {
await expectHrefToBeForwardedInSSR('/passHref/default')
})
it('navigates correctly', async () => {
await expectLinkClickToNavigate('/passHref/default')
})
})
describe('with runtime prefetch', () => {
it('forwards the href attribute', async () => {
await expectHrefToBeForwardedInSSR('/passHref/runtime')
})
it('navigates correctly (failing)', async () => {
if (isNextDev) {
// FIXME(NAR-876): false positive due to debug info blocking the child
// await expectLinkClickToNavigate('/passHref/runtime')
const browser = await next.browser('/passHref/runtime')
await expect(browser).toDisplayRedbox(`
{
"code": "E863",
"description": "\`<Link legacyBehavior>\` received a direct child that is either a Server Component, or JSX that was loaded with React.lazy(). This is not supported. Either remove legacyBehavior, or make the direct child a Client Component that renders the Link's \`<a>\` tag.",
"environmentLabel": null,
"label": "Runtime Error",
"source": "app/passHref/runtime/page.tsx (9:7) @ Page
> 9 | <Link href="/about" legacyBehavior passHref>
| ^",
"stack": [
"Page app/passHref/runtime/page.tsx (9:7)",
],
}
`)
} else {
await expectLinkClickToNavigate('/passHref/runtime')
}
})
})
describe('in dynamic code', () => {
it('forwards the href attribute', async () => {
await expectHrefToBeForwardedInSSR('/passHref/dynamic')
})
it('navigates correctly', async () => {
await expectLinkClickToNavigate('/passHref/dynamic')
})
}) })
}) })
}) })
import { isNextDev, nextTestSetup } from 'e2e-utils' import { isNextDev, nextTestSetup } from 'e2e-utils'
import { waitForNoRedbox } from 'next-test-utils' import { waitForNoRedbox } from 'next-test-utils'
import { getDeterministicOutput } from '../app-dir/cache-components-errors/utils'
const partialPrefetching = !!process.env.__NEXT_PARTIAL_PREFETCHING const partialPrefetching = !!process.env.__NEXT_PARTIAL_PREFETCHING
...@@ -9,7 +10,7 @@ describe('Validations for <Link legacyBehavior>', () => { ...@@ -9,7 +10,7 @@ describe('Validations for <Link legacyBehavior>', () => {
skipDeployment: true, skipDeployment: true,
}) })
if (skipped) return if (skipped) return
let previousOutputIndex let previousOutputIndex = 0
beforeEach(() => { beforeEach(() => {
previousOutputIndex = next.cliOutput.length previousOutputIndex = next.cliOutput.length
...@@ -63,16 +64,18 @@ describe('Validations for <Link legacyBehavior>', () => { ...@@ -63,16 +64,18 @@ describe('Validations for <Link legacyBehavior>', () => {
if (partialPrefetching) { if (partialPrefetching) {
// In Partial Prefetching, we do a second render to produce the embedded // In Partial Prefetching, we do a second render to produce the embedded
// runtime prefetch stream, which results in a second log. // runtime prefetch stream, which results in a second log.
expect(newConsoleOutput()).toMatchInlineSnapshot(` expect(
getDeterministicOutput(newConsoleOutput(), { isMinified: true })
).toMatchInlineSnapshot(`
"Using a Server Component as a direct child of \`<Link legacyBehavior>\` is not supported. If you need legacyBehavior, wrap your Server Component in a Client Component that renders the Link's \`<a>\` tag. "Using a Server Component as a direct child of \`<Link legacyBehavior>\` is not supported. If you need legacyBehavior, wrap your Server Component in a Client Component that renders the Link's \`<a>\` tag.
Using a Server Component as a direct child of \`<Link legacyBehavior>\` is not supported. If you need legacyBehavior, wrap your Server Component in a Client Component that renders the Link's \`<a>\` tag. Using a Server Component as a direct child of \`<Link legacyBehavior>\` is not supported. If you need legacyBehavior, wrap your Server Component in a Client Component that renders the Link's \`<a>\` tag."
"
`) `)
} else { } else {
expect(newConsoleOutput()).toMatchInlineSnapshot(` expect(
"Using a Server Component as a direct child of \`<Link legacyBehavior>\` is not supported. If you need legacyBehavior, wrap your Server Component in a Client Component that renders the Link's \`<a>\` tag. getDeterministicOutput(newConsoleOutput(), { isMinified: true })
" ).toMatchInlineSnapshot(
`) `"Using a Server Component as a direct child of \`<Link legacyBehavior>\` is not supported. If you need legacyBehavior, wrap your Server Component in a Client Component that renders the Link's \`<a>\` tag."`
)
} }
} }
}) })
...@@ -124,17 +127,57 @@ describe('Validations for <Link legacyBehavior>', () => { ...@@ -124,17 +127,57 @@ describe('Validations for <Link legacyBehavior>', () => {
} }
}) })
it('does not warn or throw if you pass a client component', async () => { describe('does not warn or throw if you pass a client component', () => {
it('with no prefetch config', async () => {
const browser = await next.browser( const browser = await next.browser(
'/validations/rsc-that-renders-link/client' '/validations/rsc-that-renders-link/client/default'
) )
if (isNextDev) {
await waitForNoRedbox(browser)
} else {
expect(newConsoleOutput()).toEqual('')
}
})
it('with runtime prefetch', async () => {
const browser = await next.browser(
'/validations/rsc-that-renders-link/client/runtime'
)
if (isNextDev) {
// FIXME(NAR-876): false positive due to debug info blocking the child
// await waitForNoRedbox(browser)
await expect(browser).toDisplayRedbox(`
{
"code": "E863",
"description": "\`<Link legacyBehavior>\` received a direct child that is either a Server Component, or JSX that was loaded with React.lazy(). This is not supported. Either remove legacyBehavior, or make the direct child a Client Component that renders the Link's \`<a>\` tag.",
"environmentLabel": null,
"label": "Runtime Error",
"source": "app/validations/rsc-that-renders-link/client/runtime/page.tsx (9:7) @ Page
> 9 | <Link href="/about" legacyBehavior passHref>
| ^",
"stack": [
"Page app/validations/rsc-that-renders-link/client/runtime/page.tsx (9:7)",
],
}
`)
} else {
expect(newConsoleOutput()).toEqual('')
}
})
it('in dynamic code', async () => {
const browser = await next.browser(
'/validations/rsc-that-renders-link/client/dynamic'
)
if (isNextDev) { if (isNextDev) {
await waitForNoRedbox(browser) await waitForNoRedbox(browser)
} else { } else {
expect(newConsoleOutput()).toEqual('') expect(newConsoleOutput()).toEqual('')
} }
}) })
})
it('does not warn or throw if you pass a server component into a client component', async () => { it('does not warn or throw if you pass a server component into a client component', async () => {
const browser = await next.browser( const browser = await next.browser(
...@@ -241,21 +284,63 @@ describe('Validations for <Link legacyBehavior>', () => { ...@@ -241,21 +284,63 @@ describe('Validations for <Link legacyBehavior>', () => {
} }
}) })
it('does not warn or throw if you pass a client component', async () => { describe('does not warn or throw if you pass a client component', () => {
it('with no prefetch config', async () => {
const browser = await next.browser(
'/validations/rsc-that-renders-client/client/default'
)
if (isNextDev) {
await waitForNoRedbox(browser)
} else {
expect(newConsoleOutput()).toEqual('')
}
})
it('with runtime prefetch', async () => {
const browser = await next.browser( const browser = await next.browser(
'/validations/rsc-that-renders-client/client' '/validations/rsc-that-renders-client/client/runtime'
) )
if (isNextDev) {
// FIXME(NAR-876): false positive due to debug info blocking the child
// await waitForNoRedbox(browser)
await expect(browser).toDisplayRedbox(`
{
"code": "E863",
"description": "\`<Link legacyBehavior>\` received a direct child that is either a Server Component, or JSX that was loaded with React.lazy(). This is not supported. Either remove legacyBehavior, or make the direct child a Client Component that renders the Link's \`<a>\` tag.",
"environmentLabel": null,
"label": "Runtime Error",
"source": "app/validations/rsc-that-renders-client/client-link.tsx (7:10) @ ClientLink
> 7 | return <Link legacyBehavior passHref {...props} />
| ^",
"stack": [
"ClientLink app/validations/rsc-that-renders-client/client-link.tsx (7:10)",
"Page app/validations/rsc-that-renders-client/client/runtime/page.tsx (9:7)",
],
}
`)
} else {
expect(newConsoleOutput()).toEqual('')
}
})
it('in dynamic code', async () => {
const browser = await next.browser(
'/validations/rsc-that-renders-client/client/dynamic'
)
if (isNextDev) { if (isNextDev) {
await waitForNoRedbox(browser) await waitForNoRedbox(browser)
} else { } else {
expect(newConsoleOutput()).toEqual('') expect(newConsoleOutput()).toEqual('')
} }
}) })
})
it('does not warn or throw if you pass a server component into a client component', async () => { describe('does not warn or throw if you pass a server component into a client component', () => {
it('with no prefetch config', async () => {
const browser = await next.browser( const browser = await next.browser(
'/validations/rsc-that-renders-client/client-with-rsc-child' '/validations/rsc-that-renders-client/client-with-rsc-child/default'
) )
if (isNextDev) { if (isNextDev) {
...@@ -264,6 +349,48 @@ describe('Validations for <Link legacyBehavior>', () => { ...@@ -264,6 +349,48 @@ describe('Validations for <Link legacyBehavior>', () => {
expect(newConsoleOutput()).toEqual('') expect(newConsoleOutput()).toEqual('')
} }
}) })
it('with runtime prefetch', async () => {
const browser = await next.browser(
'/validations/rsc-that-renders-client/client-with-rsc-child/runtime'
)
if (isNextDev) {
// FIXME(NAR-876): false positive due to debug info blocking the child
// await waitForNoRedbox(browser)
await expect(browser).toDisplayRedbox(`
{
"code": "E863",
"description": "\`<Link legacyBehavior>\` received a direct child that is either a Server Component, or JSX that was loaded with React.lazy(). This is not supported. Either remove legacyBehavior, or make the direct child a Client Component that renders the Link's \`<a>\` tag.",
"environmentLabel": null,
"label": "Runtime Error",
"source": "app/validations/rsc-that-renders-client/client-link.tsx (7:10) @ ClientLink
> 7 | return <Link legacyBehavior passHref {...props} />
| ^",
"stack": [
"ClientLink app/validations/rsc-that-renders-client/client-link.tsx (7:10)",
"Page app/validations/rsc-that-renders-client/client-with-rsc-child/runtime/page.tsx (9:7)",
],
}
`)
} else {
expect(newConsoleOutput()).toEqual('')
}
})
it('in dynamic code', async () => {
const browser = await next.browser(
'/validations/rsc-that-renders-client/client-with-rsc-child/dynamic'
)
if (isNextDev) {
await waitForNoRedbox(browser)
} else {
expect(newConsoleOutput()).toEqual('')
}
})
})
}) })
}) })
......
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