Unverified Commit cc926543 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

fix app router prefetch deduping (#68642)

The `Link` component has built in prefetch deduping behavior that was
added for pages router, since pages only supports a single kind of
prefetch.

Both pages & app routers share the `Link` component, meaning the
deduping behavior was enabled for both. However, app router supports
different [prefetch
types](https://nextjs.org/docs/app/api-reference/components/link#prefetch).
This means that if a page contains a link to a URL with auto
prefetching, followed by a URL with `prefetch={true}`, the full prefetch
would not be applied because it was short-circuited by `Link`.

Closes NDX-124
parent 71882dbc
...@@ -148,8 +148,11 @@ function prefetch( ...@@ -148,8 +148,11 @@ function prefetch(
} }
// We should only dedupe requests when experimental.optimisticClientCache is // We should only dedupe requests when experimental.optimisticClientCache is
// disabled. // disabled & when we're not using the app router. App router handles
if (!options.bypassPrefetchedCheck) { // reusing an existing prefetch entry (if it exists) for the same URL.
// If we dedupe in here, we will cause a race where different prefetch kinds
// to the same URL (ie auto vs true) will cause one to be ignored.
if (!options.bypassPrefetchedCheck && !isAppRouter) {
const locale = const locale =
// Let the link's locale prop override the default router locale. // Let the link's locale prop override the default router locale.
typeof options.locale !== 'undefined' typeof options.locale !== 'undefined'
......
import Link from 'next/link'
import React from 'react'
export default function Page() {
return (
<>
<div>
<Link href="/force-dynamic/test-page">
/force-dynamic/test-page (prefetch: auto)
</Link>
</div>
<div>
<Link href="/force-dynamic/test-page" prefetch>
/force-dynamic/test-page (prefetch: true)
</Link>
</div>
</>
)
}
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
import { check, waitFor, retry } from 'next-test-utils' import { check, waitFor, retry } from 'next-test-utils'
import type { Page, Request } from 'playwright' import type { Page, Request, Route } from 'playwright'
import { NEXT_RSC_UNION_QUERY } from 'next/dist/client/components/app-router-headers' import { NEXT_RSC_UNION_QUERY } from 'next/dist/client/components/app-router-headers'
const browserConfigWithFixedTime = { const browserConfigWithFixedTime = {
...@@ -462,5 +462,36 @@ describe('app dir - prefetching', () => { ...@@ -462,5 +462,36 @@ describe('app dir - prefetching', () => {
expect(dashboardRequests[1].priority).toBe('auto') // the second request is the lazy fetch to fill in missing data expect(dashboardRequests[1].priority).toBe('auto') // the second request is the lazy fetch to fill in missing data
}) })
}) })
it('should respect multiple prefetch types to the same URL', async () => {
let interceptRequests = false
const browser = await next.browser('/prefetch-race', {
beforePageLoad(page: Page) {
page.route('**/force-dynamic/**', async (route: Route) => {
if (!interceptRequests) {
return route.continue()
}
const request = route.request()
const headers = await request.allHeaders()
if (headers['rsc'] === '1') {
// intentionally stall the request,
// as after the initial page load, there shouldn't be any additional fetches
// since the data should already be available.
} else {
await route.continue()
}
})
},
})
await browser.waitForIdleNetwork()
interceptRequests = true
await browser.elementByCss('[href="/force-dynamic/test-page"]').click()
await browser.waitForElementByCss('#test-page')
})
}) })
}) })
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