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')
}) }
it('navigates correctly', async () => { const expectLinkClickToNavigate = async (path: string) => {
const browser = await next.browser('/passHref') const browser = await next.browser(path)
await browser.elementByCss('a').click()
const title = await browser.elementByCss('h1').text() 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
}
await browser.elementByCss('a[href="/about"]').click()
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')
})
}) })
}) })
}) })
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