Unverified Commit c6c4a3d3 authored by Jimmy Lai's avatar Jimmy Lai Committed by GitHub

app router: fix double fetch on prefetch={false} (#51292)

This PR fixes a few reports that we were double fetching when navigating via a link that had prefetch false.

## Context

The bug was happening because we were inadvertently eagerly fetching even if we potentially bailed out of the optimistic navigation, which would then trigger another fetch from going through the regular navigation path.

There's potentially another bug here where we should potentially not bail out of optimistic navigation in the cases reported but we can fix that later.



fix #49844
link NEXT-1287
parent cbeecba8
...@@ -157,9 +157,16 @@ export function navigateReducer( ...@@ -157,9 +157,16 @@ export function navigateReducer(
temporaryCacheNode.subTreeData = state.cache.subTreeData temporaryCacheNode.subTreeData = state.cache.subTreeData
temporaryCacheNode.parallelRoutes = new Map(state.cache.parallelRoutes) temporaryCacheNode.parallelRoutes = new Map(state.cache.parallelRoutes)
const data = createRecordFromThenable( let data: ReturnType<typeof createRecordFromThenable> | undefined
fetchServerResponse(url, optimisticTree, state.nextUrl, state.buildId)
) const fetchResponse = () => {
if (!data) {
data = createRecordFromThenable(
fetchServerResponse(url, optimisticTree, state.nextUrl, state.buildId)
)
}
return data
}
// TODO-APP: segments.slice(1) strips '', we can get rid of '' altogether. // TODO-APP: segments.slice(1) strips '', we can get rid of '' altogether.
// TODO-APP: re-evaluate if we need to strip the last segment // TODO-APP: re-evaluate if we need to strip the last segment
...@@ -174,7 +181,7 @@ export function navigateReducer( ...@@ -174,7 +181,7 @@ export function navigateReducer(
temporaryCacheNode, temporaryCacheNode,
state.cache, state.cache,
optimisticFlightSegmentPath, optimisticFlightSegmentPath,
() => data, fetchResponse,
true true
) )
......
import { createNextDescribe } from 'e2e-utils'
const getPathname = (url: string) => {
const urlObj = new URL(url)
return urlObj.pathname
}
const createRequestsListener = async (browser: BrowserInterface) => {
// wait for network idle
await browser.waitForIdleNetwork()
let requests = []
browser.on('request', (req: Request) => {
requests.push([req.url(), !!req.headers()['next-router-prefetch']])
})
await browser.refresh()
return {
getRequests: () => requests,
clearRequests: () => {
requests = []
},
}
}
createNextDescribe(
'app-prefetch-false',
{
files: __dirname,
},
({ next, isNextDev }) => {
if (isNextDev) {
it.skip('should skip test in dev mode', () => {})
} else {
it('should avoid double-fetching when optimistic navigation fails', async () => {
const browser = await next.browser('/foo')
const { getRequests } = await createRequestsListener(browser)
await browser.elementByCss('[href="/foo"]').click()
await browser.elementByCss('[href="/foo/bar"]').click()
console.log('getRequests()', getRequests())
expect(
getRequests().filter(([req]) => getPathname(req) === '/foo/bar')
.length
).toBe(1)
})
}
}
)
export default function Page() {
return <p>hello world</p>
}
export default function Layout({ children }) {
return children
}
export default function Page() {
return <p>hello world</p>
}
import Link from 'next/link'
import React from 'react'
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>
<div>
<Link prefetch={false} href="/foo">
foo
</Link>
</div>
<div>
<Link prefetch={false} href="/foo/bar">
foo/bar
</Link>
</div>
{children}
</body>
</html>
)
}
export default function Page() {
return <p>hello world</p>
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}
module.exports = nextConfig
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