Unverified Commit 50e41a25 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

backport: fix prefetch bailout detection for nested loading segments (#70618)

Backports:
- https://github.com/vercel/next.js/pull/67358

Fixes #70527

Note: Turbopack dev failures seem to be pre-existing on the base branch.
parent e19d91cf
...@@ -108,14 +108,16 @@ export async function walkTreeWithFlightRouterState({ ...@@ -108,14 +108,16 @@ export async function walkTreeWithFlightRouterState({
// Explicit refresh // Explicit refresh
flightRouterState[3] === 'refetch' flightRouterState[3] === 'refetch'
// Pre-PPR, the `loading` component signals to the router how deep to render the component tree
// to ensure prefetches are quick and inexpensive. If there's no `loading` component anywhere in the tree being rendered,
// the prefetch will be short-circuited to avoid requesting a potentially very expensive subtree. If there's a `loading`
// somewhere in the tree, we'll recursively render the component tree up until we encounter that loading component, and then stop.
const shouldSkipComponentTree = const shouldSkipComponentTree =
// loading.tsx has no effect on prefetching when PPR is enabled // loading.tsx has no effect on prefetching when PPR is enabled
!experimental.ppr && !experimental.ppr &&
isPrefetch && isPrefetch &&
!Boolean(components.loading) && !Boolean(components.loading) &&
(flightRouterState || !hasLoadingComponentInTree(loaderTree)
// If there is no flightRouterState, we need to check the entire loader tree, as otherwise we'll be only checking the root
!hasLoadingComponentInTree(loaderTree))
if (!parentRendered && renderComponentsOnThisLevel) { if (!parentRendered && renderComponentsOnThisLevel) {
const overriddenSegment = const overriddenSegment =
......
...@@ -8,6 +8,9 @@ export default function HomePage() { ...@@ -8,6 +8,9 @@ export default function HomePage() {
<Link href="/static-page" id="to-static-page"> <Link href="/static-page" id="to-static-page">
To Static Page To Static Page
</Link> </Link>
<Link href="/prefetch-auto/foobar" id="to-dynamic-page">
To Dynamic Slug Page
</Link>
</> </>
) )
} }
...@@ -2,7 +2,18 @@ import Link from 'next/link' ...@@ -2,7 +2,18 @@ import Link from 'next/link'
export const dynamic = 'force-dynamic' export const dynamic = 'force-dynamic'
function getData() {
const res = new Promise((resolve) => {
setTimeout(() => {
resolve({ message: 'Layout Data!' })
}, 2000)
})
return res
}
export default async function Layout({ children }) { export default async function Layout({ children }) {
const result = await getData()
return ( return (
<div> <div>
<h1>Layout</h1> <h1>Layout</h1>
...@@ -10,6 +21,7 @@ export default async function Layout({ children }) { ...@@ -10,6 +21,7 @@ export default async function Layout({ children }) {
Prefetch Link Prefetch Link
</Link> </Link>
{children} {children}
<h3>{JSON.stringify(result)}</h3>
</div> </div>
) )
} }
export default function Loading() { export default function Loading() {
return <h1>Loading Prefetch Auto</h1> return <h1 id="loading-text">Loading Prefetch Auto</h1>
} }
...@@ -3,7 +3,7 @@ export const dynamic = 'force-dynamic' ...@@ -3,7 +3,7 @@ export const dynamic = 'force-dynamic'
function getData() { function getData() {
const res = new Promise((resolve) => { const res = new Promise((resolve) => {
setTimeout(() => { setTimeout(() => {
resolve({ message: 'Hello World!' }) resolve({ message: 'Page Data!' })
}, 2000) }, 2000)
}) })
return res return res
...@@ -13,9 +13,9 @@ export default async function Page({ params }) { ...@@ -13,9 +13,9 @@ export default async function Page({ params }) {
const result = await getData() const result = await getData()
return ( return (
<> <div id="prefetch-auto-page-data">
<h3>{JSON.stringify(params)}</h3> <h3>{JSON.stringify(params)}</h3>
<h3>{JSON.stringify(result)}</h3> <h3>{JSON.stringify(result)}</h3>
</> </div>
) )
} }
...@@ -263,7 +263,8 @@ createNextDescribe( ...@@ -263,7 +263,8 @@ createNextDescribe(
}) })
const prefetchResponse = await response.text() const prefetchResponse = await response.text()
expect(prefetchResponse).not.toContain('Hello World') expect(prefetchResponse).toContain('Page Data')
expect(prefetchResponse).not.toContain('Layout Data!')
expect(prefetchResponse).not.toContain('Loading Prefetch Auto') expect(prefetchResponse).not.toContain('Loading Prefetch Auto')
}) })
...@@ -297,10 +298,23 @@ createNextDescribe( ...@@ -297,10 +298,23 @@ createNextDescribe(
}) })
const prefetchResponse = await response.text() const prefetchResponse = await response.text()
expect(prefetchResponse).not.toContain('Hello World') expect(prefetchResponse).not.toContain('Page Data!')
expect(prefetchResponse).toContain('Loading Prefetch Auto') expect(prefetchResponse).toContain('Loading Prefetch Auto')
}) })
it('should immediately render the loading state for a dynamic segment when fetched from higher up in the tree', async () => {
const browser = await next.browser('/')
const loadingText = await browser
.elementById('to-dynamic-page')
.click()
.waitForElementByCss('#loading-text')
.text()
expect(loadingText).toBe('Loading Prefetch Auto')
await browser.waitForElementByCss('#prefetch-auto-page-data')
})
describe('dynamic rendering', () => { describe('dynamic rendering', () => {
describe.each(['/force-dynamic', '/revalidate-0'])('%s', (basePath) => { describe.each(['/force-dynamic', '/revalidate-0'])('%s', (basePath) => {
it('should not re-render layout when navigating between sub-pages', async () => { it('should not re-render layout when navigating between sub-pages', async () => {
......
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