Skip to content

Commit a918011

Browse files
eps1lonwyattjoh
authored andcommitted
Current behavior for fallbacks with async metadata and prefetching (#73106)
1 parent 7eef669 commit a918011

File tree

3 files changed

+53
-19
lines changed

3 files changed

+53
-19
lines changed

test/e2e/app-dir/navigation/app/metadata-await-promise/nested/page.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import React from 'react'
33
// ensure this page is dynamically rendered so we always trigger a loading state
44
export const dynamic = 'force-dynamic'
55

6-
export default function page() {
6+
export default function Page() {
77
return <div id="page-content">Content</div>
88
}
99

1010
async function getTitle() {
1111
return await new Promise((resolve) =>
12-
setTimeout(() => resolve('Async Title'), 1000)
12+
setTimeout(() => resolve('Async Title'), 5000)
1313
)
1414
}
1515

test/e2e/app-dir/navigation/app/metadata-await-promise/page.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Link from 'next/link'
22

3-
export default function page() {
3+
export default function Page() {
44
return (
55
<div>
66
<Link href="/metadata-await-promise/nested">Link to nested</Link>

test/e2e/app-dir/navigation/navigation.test.ts

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -881,29 +881,63 @@ describe('app dir - navigation', () => {
881881
})
882882

883883
describe('navigating to a page with async metadata', () => {
884-
it('should render the final state of the page with correct metadata', async () => {
884+
it('shows a fallback when prefetch was pending', async () => {
885+
const resolveMetadataDuration = 5000
885886
const browser = await next.browser('/metadata-await-promise')
886887

887-
// dev doesn't trigger the loading boundary as it's not prefetched
888-
if (isNextDev) {
889-
await browser
890-
.elementByCss("[href='/metadata-await-promise/nested']")
891-
.click()
892-
} else {
893-
const loadingText = await browser
894-
.elementByCss("[href='/metadata-await-promise/nested']")
895-
.click()
896-
.waitForElementByCss('#loading')
897-
.text()
888+
// Hopefully this click happened before the prefetch was completed.
889+
// TODO: Programmatically trigger prefetch e.g. by mounting the link later.
890+
await browser
891+
.elementByCss("[href='/metadata-await-promise/nested']")
892+
.click()
898893

899-
expect(loadingText).toBe('Loading')
894+
if (!isNextDev) {
895+
// next-dev has no prefetch
896+
expect(
897+
await browser
898+
.waitForElementByCss(
899+
'#loading',
900+
// Wait a bit longer than the prefetch duration since the click takes a while to register and the fallback render also takes time.
901+
resolveMetadataDuration + 500
902+
)
903+
.text()
904+
).toEqual('Loading')
905+
expect(await browser.elementByCss('title').text()).toBe('Async Title')
900906
}
901907

902-
await retry(async () => {
903-
expect(await browser.elementById('page-content').text()).toBe('Content')
908+
await waitFor(resolveMetadataDuration)
909+
910+
expect(await browser.elementById('page-content').text()).toBe('Content')
911+
})
912+
913+
it('shows a fallback when prefetch completed', async () => {
914+
const resolveMetadataDuration = 5000
915+
const browser = await next.browser('/metadata-await-promise')
916+
917+
if (!isNextDev) {
918+
await waitFor(resolveMetadataDuration + 500)
919+
}
904920

921+
await browser
922+
.elementByCss("[href='/metadata-await-promise/nested']")
923+
.click()
924+
925+
if (!isNextDev) {
926+
expect(
927+
await browser
928+
.waitForElementByCss(
929+
'#loading',
930+
// Give it some time to commit
931+
100
932+
)
933+
.text()
934+
).toEqual('Loading')
905935
expect(await browser.elementByCss('title').text()).toBe('Async Title')
906-
})
936+
937+
await waitFor(resolveMetadataDuration + 500)
938+
}
939+
940+
expect(await browser.elementById('page-content').text()).toBe('Content')
907941
})
908942
})
909943

0 commit comments

Comments
 (0)