Repository navigation
Conversation
A click on <Link download> is already left to the browser, so it never becomes a client-side navigation. Prefetching it still requested the target with RSC headers, which runs a Route Handler (for example a CSV export) before the user clicks. Skip registering these links for viewport and hover prefetching. Co-authored-by: vercel-v[bot] <353008837+vercel-v[bot]@users.noreply.github.com>
Contributor
Tests PassedCommit: 8a4a012 |
This branch has not been deployed
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What?
App Router
<Link>no longer prefetches when the rendered anchor has adownloadattribute (viewport, hover, and touch prefetches, includingprefetch={true}).Why?
linkClickedalready leaves a click on adownloadlink to the browser, so these links never become client-side navigations. But they were still registered for prefetching, so in production the router requested the target with RSC headers as soon as the link scrolled into view. When the target is a Route Handler, that runs the handler: a CSV export runs before the user clicks, and side-effectfulGEThandlers fire early.This brings prefetch in line with the existing click bailout. It also closes part of the gap described in #38060: a
<Link download>now behaves like a plain<a download>.Related: #72909, #65764. The docs guidance for Route Handlers is in #102390.
How?
links.tschecks the element'sdownloadattribute when a prefetch would be scheduled.onLinkVisibilityChangedkeeps such a link out of the visible set, andonNavigationIntentreturns early, so viewport, hover, and touch prefetches are all skipped.linkClicked, so it coverslegacyBehaviorchildren and adownloadattribute added after mount.next/linkunchanged. Its prefetch loads page bundles and doesn't request the target URL.The first commit has an alternative that decides at mount time in
link.tsxby reading thedownloadprop. The second commit replaces it with thelinks.tscheck.New e2e suite
test/e2e/app-dir/link-download-prefetch, which usescreateRouterAct+LinkAccordionwithincludeAppShellRequests: true, so any request to the handler fails'no-requests':<Link>still prefetches (control).<Link download>with the default prefetch and withprefetch={true}makes no router requests on reveal or hover.<Link download="report.csv">makes no router request, keeps the URL, and downloads the file asreport.csv(the handler sendsContent-Disposition: attachmentwithout a filename).Verification
links.tschange: 3 of 4 tests fail withpnpm test-start-turbo(the control passes).pnpm test-start-turbo,pnpm test-start-webpack, and__NEXT_CACHE_COMPONENTS=true pnpm test-start-turbo;pnpm test-dev-turboskips (no prefetching in dev).pnpm test-start-turbo test/e2e/app-dir/segment-cache/dynamic-on-hover test/e2e/app-dir/segment-cache/no-prefetch(existing tests, confirm behavior is unchanged).pnpm --filter=next typesis clean.