Unverified Commit 27accf62 authored by Andrew Clark's avatar Andrew Clark Committed by GitHub

Add more test coverage for non-Partial Prefetching behavior (#100133)

When working on some upcoming refactors of the prefetching
implementation, I discovered some cases scenarios where we didn't have
test coverage of non-Partial Prefetch behavior. This adds new tests to
ensure the behavior does not regress:

- A `prefetch={true}` link fetches only the dynamic data that a default
prefetch of the same URL left out, and a default link doesn't re-request
what a `prefetch={true}` link already fetched.
- Back/forward navigations are served from the back/forward cache
without requests.
- A loading-boundary prefetch counts as complete for the segments above
the boundary, so the navigation requests only the page below it.
parent fb20d9af
import { connection } from 'next/server'
export default async function Layout({ children }) {
await connection()
return (
<div>
<p id="loading-boundary-outer-layout">Outer layout [lb-outer-layout]</p>
{children}
</div>
)
}
export default function Layout({ children }) {
return (
<div>
<p id="loading-boundary-inner-layout">Inner layout [lb-inner-layout]</p>
{children}
</div>
)
}
export default function Loading() {
return <p id="loading-boundary-loading">Loading [lb-loading]</p>
}
export default function Page() {
return <p id="loading-boundary-page">Page content [lb-page]</p>
}
......@@ -22,6 +22,12 @@ export default function HomePage() {
<LinkAccordion href="/prefetch-auto/foobar" id="to-dynamic-page">
To Dynamic Slug Page
</LinkAccordion>
<LinkAccordion
href="/loading-boundary/target"
id="to-loading-boundary-target"
>
To Loading Boundary Target
</LinkAccordion>
<a href="/static-page" id="to-static-page-hard">
Hard Nav to Static Page
</a>
......
......@@ -344,6 +344,48 @@ describe('app dir - prefetching', () => {
await browser.waitForElementByCss('#prefetch-auto-page-data')
})
it('treats a loading boundary prefetch as complete for the segments above the boundary', async () => {
let act: ReturnType<typeof createRouterAct>
const browser = await next.browser('/', {
beforePageLoad(page) {
act = createRouterAct(page)
},
})
// The route is dynamic, so it can't be prefetched per segment. The
// prefetch renders down to the loading boundary, so it includes the
// layouts but not the page.
await act(async () => {
const reveal = await browser.elementByCss(
'#accordion-to-loading-boundary-target'
)
await reveal.click()
}, [
{ includes: '[lb-outer-layout]' },
{ includes: '[lb-page]', block: 'reject' },
])
// The navigation requests only the page below the boundary. The layouts
// above it are not requested again.
await act(async () => {
const link = await browser.elementByCss('#to-loading-boundary-target')
await link.click()
}, [
{ includes: '[lb-page]' },
{ includes: '[lb-outer-layout]', block: 'reject' },
{ includes: '[lb-inner-layout]', block: 'reject' },
])
expect(
await browser.elementByCss('#loading-boundary-outer-layout').text()
).toBe('Outer layout [lb-outer-layout]')
expect(
await browser.elementByCss('#loading-boundary-inner-layout').text()
).toBe('Inner layout [lb-inner-layout]')
expect(await browser.elementByCss('#loading-boundary-page').text()).toBe(
'Page content [lb-page]'
)
})
it('should not unintentionally modify the requested prefetch by escaping the uri encoded query params', async () => {
const rscRequests = []
const browser = await next.browser('/uri-encoded-prefetch', {
......
......@@ -14,6 +14,20 @@ export default function Page() {
Dynamic page (no prefetch)
</Link>
</li>
<li>
<LinkAccordion href="/partially-static" id="partially-static-default">
Partially static page (default prefetch)
</LinkAccordion>
</li>
<li>
<LinkAccordion
href="/partially-static"
prefetch={true}
id="partially-static-full"
>
Partially static page (prefetch=true)
</LinkAccordion>
</li>
</ul>
)
}
export default function Layout({ children }: { children: React.ReactNode }) {
return (
<div>
<div id="partially-static-layout">Static layout content</div>
{children}
</div>
)
}
export default function Loading() {
return <div>Loading partially static page...</div>
}
import { connection } from 'next/server'
export default async function Page() {
await connection()
return <div id="partially-static-page">Partially static page content</div>
}
......@@ -7,10 +7,13 @@ export function LinkAccordion({
href,
children,
prefetch,
id,
}: {
href: string
children: React.ReactNode
prefetch?: LinkProps['prefetch']
// Distinguishes accordions that share an href.
id?: string
}) {
const [isVisible, setIsVisible] = useState(false)
return (
......@@ -19,10 +22,10 @@ export function LinkAccordion({
type="checkbox"
checked={isVisible}
onChange={() => setIsVisible(!isVisible)}
data-link-accordion={href}
data-link-accordion={id ?? href}
/>
{isVisible ? (
<Link href={href} prefetch={prefetch}>
<Link href={href} prefetch={prefetch} id={id}>
{children}
</Link>
) : (
......
......@@ -93,4 +93,130 @@ describe('force stale', () => {
}, 'no-requests')
}
)
it(
'a prefetch={true} link fetches only the dynamic data that a default ' +
'prefetch of the same URL left out',
async () => {
let act: ReturnType<typeof createRouterAct>
const browser = await next.browser('/', {
beforePageLoad(p: Playwright.Page) {
act = createRouterAct(p)
},
})
// Reveal a default link. The prefetch includes the static layout and
// the loading state, but not the dynamic page content. (With Cache
// Components this is a per-segment static prefetch; without, it's a
// prefetch up to the loading boundary.)
await act(async () => {
const toggle = await browser.elementByCss(
'input[data-link-accordion="partially-static-default"]'
)
await toggle.click()
}, [
{ includes: 'Static layout content' },
{ includes: 'Partially static page content', block: 'reject' },
])
// Reveal a prefetch={true} link to the same URL. It requests the
// dynamic page content, but not the layout, which is already cached.
await act(async () => {
const toggle = await browser.elementByCss(
'input[data-link-accordion="partially-static-full"]'
)
await toggle.click()
}, [
{ includes: 'Partially static page content' },
{ includes: 'Static layout content', block: 'reject' },
])
// Everything was prefetched, so the navigation makes no requests.
await act(async () => {
const link = await browser.elementById('partially-static-default')
await link.click()
}, 'no-requests')
expect(await browser.elementById('partially-static-layout').text()).toBe(
'Static layout content'
)
expect(await browser.elementById('partially-static-page').text()).toBe(
'Partially static page content'
)
}
)
it(
'a default link does not re-request data already fetched by a ' +
'prefetch={true} link to the same URL',
async () => {
let act: ReturnType<typeof createRouterAct>
const browser = await next.browser('/', {
beforePageLoad(p: Playwright.Page) {
act = createRouterAct(p)
},
})
// The prefetch={true} link fetches the whole page, including the
// dynamic content.
await act(async () => {
const toggle = await browser.elementByCss(
'input[data-link-accordion="partially-static-full"]'
)
await toggle.click()
}, [
{ includes: 'Static layout content' },
{ includes: 'Partially static page content' },
])
// A default link to the same URL has nothing left to prefetch.
await act(async () => {
const toggle = await browser.elementByCss(
'input[data-link-accordion="partially-static-default"]'
)
await toggle.click()
}, 'no-requests')
// Navigating from the default link makes no requests either.
await act(async () => {
const link = await browser.elementById('partially-static-default')
await link.click()
}, 'no-requests')
expect(await browser.elementById('partially-static-page').text()).toBe(
'Partially static page content'
)
}
)
it('back/forward navigations do not make requests', async () => {
let act: ReturnType<typeof createRouterAct>
const browser = await next.browser('/', {
beforePageLoad(p: Playwright.Page) {
act = createRouterAct(p)
},
})
await act(
async () => {
const link = await browser.elementById('link-without-prefetch')
await link.click()
},
{ includes: 'Dynamic page content' }
)
expect(await browser.elementById('dynamic-page-content').text()).toBe(
'Dynamic page content'
)
// Both pages are restored from the back/forward cache.
await act(async () => {
await browser.back()
}, 'no-requests')
await browser.elementById('link-without-prefetch')
await act(async () => {
await browser.forward()
}, 'no-requests')
expect(await browser.elementById('dynamic-page-content').text()).toBe(
'Dynamic page content'
)
})
})
......@@ -2,7 +2,8 @@
* @type {import('next').NextConfig}
*/
const nextConfig = {
cacheComponents: true,
// cacheComponents is left unset so this suite runs both with and without
// Cache Components.
productionBrowserSourceMaps: true,
experimental: {
prefetchInlining: false,
......
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