Commit dd6ab93d authored by Jiachi Liu's avatar Jiachi Liu Committed by Zack Tanner

Fix loading navigation with metadata and prefetch (#66447)

Fixes NEXT-3498

Fixed loading shows up and disappear during client navigation, when you
defined `prefetch` is enabled and slow `generateMetadata` is defined. In
suspense, adding it back so that the app can still remain suspensy
during navigation.

Prefetch -> Link Navigation -> Show `loading.js` -> RSC payload fetched
(no page content) -> the page content will display later when the
promise is resolved

Prefetch -> Link Navigation -> Show `loading.js` -> RSC payload fetched
-> suspensy page content still triggering `loading.js` -> display the
resolved page content when the promise is resolved

---------
Co-authored-by: 's avatarZack Tanner <1939140+ztanner@users.noreply.github.com>
parent 663c6af4
...@@ -439,10 +439,10 @@ function InnerLayoutRouter({ ...@@ -439,10 +439,10 @@ function InnerLayoutRouter({
// It's important that we mark this as resolved, in case this branch is replayed, we don't want to continously re-apply // It's important that we mark this as resolved, in case this branch is replayed, we don't want to continously re-apply
// the patch to the tree. // the patch to the tree.
childNode.lazyDataResolved = true childNode.lazyDataResolved = true
// Suspend infinitely as `changeByServerResponse` will cause a different part of the tree to be rendered.
use(unresolvedThenable) as never
} }
// Suspend infinitely as `changeByServerResponse` will cause a different part of the tree to be rendered.
// A falsey `resolvedRsc` indicates missing data -- we should not commit that branch, and we need to wait for the data to arrive.
use(unresolvedThenable) as never
} }
// If we get to this point, then we know we have something we can render. // If we get to this point, then we know we have something we can render.
......
'use client'
import { useEffect } from 'react'
export default function Loading() { export default function Loading() {
useEffect(() => {
window.shownLoading = true
}, [])
return <div id="loading">Loading</div> return <div id="loading">Loading</div>
} }
...@@ -841,16 +841,22 @@ createNextDescribe( ...@@ -841,16 +841,22 @@ createNextDescribe(
it('should render the final state of the page with correct metadata', async () => { it('should render the final state of the page with correct metadata', async () => {
const browser = await next.browser('/metadata-await-promise') const browser = await next.browser('/metadata-await-promise')
await browser // dev doesn't trigger the loading boundary as it's not prefetched
.elementByCss("[href='/metadata-await-promise/nested']") if (isNextDev) {
.click() await browser
.elementByCss("[href='/metadata-await-promise/nested']")
.click()
} else {
const loadingText = await browser
.elementByCss("[href='/metadata-await-promise/nested']")
.click()
.waitForElementByCss('#loading')
.text()
await retry(async () => { expect(loadingText).toBe('Loading')
// dev + PPR doesn't trigger the loading boundary as it's not prefetched }
if (!(isNextDev && process.env.__NEXT_EXPERIMENTAL_PPR)) {
expect(await browser.eval(`window.shownLoading`)).toBe(true)
}
await retry(async () => {
expect(await browser.elementById('page-content').text()).toBe( expect(await browser.elementById('page-content').text()).toBe(
'Content' 'Content'
) )
......
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