Unverified Commit 7b0ab1ec authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

Current behavior for fallbacks with async metadata and prefetching (#73106)

parent 560bfdb3
...@@ -3,13 +3,13 @@ import React from 'react' ...@@ -3,13 +3,13 @@ import React from 'react'
// ensure this page is dynamically rendered so we always trigger a loading state // ensure this page is dynamically rendered so we always trigger a loading state
export const dynamic = 'force-dynamic' export const dynamic = 'force-dynamic'
export default function page() { export default function Page() {
return <div id="page-content">Content</div> return <div id="page-content">Content</div>
} }
async function getTitle() { async function getTitle() {
return await new Promise((resolve) => return await new Promise((resolve) =>
setTimeout(() => resolve('Async Title'), 1000) setTimeout(() => resolve('Async Title'), 5000)
) )
} }
......
import Link from 'next/link' import Link from 'next/link'
export default function page() { export default function Page() {
return ( return (
<div> <div>
<Link href="/metadata-await-promise/nested">Link to nested</Link> <Link href="/metadata-await-promise/nested">Link to nested</Link>
......
...@@ -881,29 +881,63 @@ describe('app dir - navigation', () => { ...@@ -881,29 +881,63 @@ describe('app dir - navigation', () => {
}) })
describe('navigating to a page with async metadata', () => { describe('navigating to a page with async metadata', () => {
it('should render the final state of the page with correct metadata', async () => { it('shows a fallback when prefetch was pending', async () => {
const resolveMetadataDuration = 5000
const browser = await next.browser('/metadata-await-promise') const browser = await next.browser('/metadata-await-promise')
// dev doesn't trigger the loading boundary as it's not prefetched // Hopefully this click happened before the prefetch was completed.
if (isNextDev) { // TODO: Programmatically trigger prefetch e.g. by mounting the link later.
await browser await browser
.elementByCss("[href='/metadata-await-promise/nested']") .elementByCss("[href='/metadata-await-promise/nested']")
.click() .click()
} else {
const loadingText = await browser
.elementByCss("[href='/metadata-await-promise/nested']")
.click()
.waitForElementByCss('#loading')
.text()
expect(loadingText).toBe('Loading') if (!isNextDev) {
// next-dev has no prefetch
expect(
await browser
.waitForElementByCss(
'#loading',
// Wait a bit longer than the prefetch duration since the click takes a while to register and the fallback render also takes time.
resolveMetadataDuration + 500
)
.text()
).toEqual('Loading')
expect(await browser.elementByCss('title').text()).toBe('Async Title')
} }
await retry(async () => { await waitFor(resolveMetadataDuration)
expect(await browser.elementById('page-content').text()).toBe('Content')
expect(await browser.elementById('page-content').text()).toBe('Content')
})
it('shows a fallback when prefetch completed', async () => {
const resolveMetadataDuration = 5000
const browser = await next.browser('/metadata-await-promise')
if (!isNextDev) {
await waitFor(resolveMetadataDuration + 500)
}
await browser
.elementByCss("[href='/metadata-await-promise/nested']")
.click()
if (!isNextDev) {
expect(
await browser
.waitForElementByCss(
'#loading',
// Give it some time to commit
100
)
.text()
).toEqual('Loading')
expect(await browser.elementByCss('title').text()).toBe('Async Title') expect(await browser.elementByCss('title').text()).toBe('Async Title')
})
await waitFor(resolveMetadataDuration + 500)
}
expect(await browser.elementById('page-content').text()).toBe('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