-
Notifications
You must be signed in to change notification settings - Fork 26k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
cbeecba
commit c6c4a3d
Showing
8 changed files
with
102 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
test/e2e/app-dir/app-prefetch-false/app-prefetch-false.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
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) | ||
}) | ||
} | ||
} | ||
) |
3 changes: 3 additions & 0 deletions
3
test/e2e/app-dir/app-prefetch-false/app/[slugA]/[slugB]/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function Page() { | ||
return <p>hello world</p> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function Layout({ children }) { | ||
return children | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function Page() { | ||
return <p>hello world</p> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
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> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function Page() { | ||
return <p>hello world</p> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* @type {import('next').NextConfig} | ||
*/ | ||
const nextConfig = {} | ||
|
||
module.exports = nextConfig |