Unverified Commit 0c91f506 authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

fix: preserve paused navigation shells during deferred renders

Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 2705e7c1
......@@ -338,7 +338,24 @@ function InnerLayoutRouter({
// We use `useDeferredValue` to handle switching between the prefetched and
// final values. The second argument is returned on initial render, then it
// re-renders with the first argument.
const rsc: any = useDeferredValue(renderTree.data.rsc, resolvedPrefetchRsc)
let rscToRender = renderTree.data.rsc
if (
process.env.__NEXT_EXPOSE_TESTING_API &&
renderTree.data.prefetchRsc !== null &&
isDeferredRsc(rscToRender) &&
rscToRender.status === 'pending'
) {
const { isNavigationLocked } =
require('./segment-cache/navigation-testing-lock') as typeof import('./segment-cache/navigation-testing-lock')
if (isNavigationLocked()) {
// A deferred render can skip useDeferredValue's initial value. While
// instant navigation is paused, the final RSC is deliberately withheld
// until unlock, so selecting it would prevent the shell from rendering.
// Keep rendering the prefetched shell until the lock is released.
rscToRender = resolvedPrefetchRsc
}
}
const rsc: any = useDeferredValue(rscToRender, resolvedPrefetchRsc)
// `rsc` is either a React node or a promise for a React node, except we
// special case `null` to represent that this segment's data is missing. If
......
export { default } from '../../target-page/[slug]/page'
export const prefetch = 'partial'
'use client'
import { useDeferredValue, type ReactNode } from 'react'
export default function DeferredLayout({ children }: { children: ReactNode }) {
// Render the page in a deferred lane, where useDeferredValue may skip its
// initial value. A paused navigation must still select the prefetched shell.
return useDeferredValue(children, null)
}
......@@ -64,6 +64,11 @@ export default function Page() {
</Link>
</div>
<div>
<LinkAccordion href="/deferred-target/my-post?search=foo">
Target page in a deferred render
</LinkAccordion>
</div>
<div>
<LinkAccordion href="/await-connection" id="link-to-await-connection">
Page with await connection
......
......@@ -101,14 +101,9 @@ describe('instant-nav-panel', () => {
await browser.eval((accordionSelector) => {
document.querySelector<HTMLInputElement>(accordionSelector)!.click()
}, accordionSelector)
// Let React commit the Link and IntersectionObserver schedule its
// prefetch before the click starts the navigation.
await browser.eval(
() =>
new Promise<void>((resolve) =>
requestIdleCallback(() => resolve(), { timeout: 100 })
)
)
// Wait for React to mount the Link before clicking it. router-act
// controls any viewport prefetch started by the same commit.
await browser.locator(selector).waitFor({ state: 'attached' })
await navigate()
}, expectedResponse)
}
......@@ -682,6 +677,29 @@ describe('instant-nav-panel', () => {
await clearInstantModeCookie(browser)
})
it('should render the prefetched shell in a deferred render while paused', async () => {
const browser = await openHomeWithTargetPageWarmup()
await openInstantNavPanel(browser)
await clickStartCapturing(browser)
await clickLink(browser, '/deferred-target/my-post?search=foo')
await expectSpaPanel(browser)
await expectTargetPageSpaShell(browser)
expect(await browser.url()).toContain('/deferred-target/my-post')
await clickResume(browser)
await expectTargetPageRendered(browser)
})
it('should render the final content in a deferred render without pausing', async () => {
const browser = await openHomeWithTargetPageWarmup()
await clickLink(browser, '/deferred-target/my-post?search=foo')
await expectTargetPageRendered(browser)
expect(await browser.url()).toContain('/deferred-target/my-post')
await waitForInstantModeCookieAbsent(browser)
})
it('should continue capturing loading navigations when starting on a dynamic route', async () => {
const browser = await next.browser('/post/1')
await clearInstantModeCookie(browser)
......
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