Skip to content

Skip prefetching for Link with a download attribute - #102392

Draft
icyJoseph wants to merge 2 commits into
canaryfrom
link-skip-prefetch-download
Draft

icyJoseph wants to merge 2 commits into
canaryfrom
link-skip-prefetch-download

Conversation

@icyJoseph

@icyJoseph icyJoseph commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

What?

App Router <Link> no longer prefetches when the rendered anchor has a download attribute (viewport, hover, and touch prefetches, including prefetch={true}).

Why?

linkClicked already leaves a click on a download link 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-effectful GET handlers 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.ts checks the element's download attribute when a prefetch would be scheduled. onLinkVisibilityChanged keeps such a link out of the visible set, and onNavigationIntent returns early, so viewport, hover, and touch prefetches are all skipped.
  • The check reads the DOM, like the click bailout in linkClicked, so it covers legacyBehavior children and a download attribute added after mount.
  • I left the Pages Router next/link unchanged. 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.tsx by reading the download prop. The second commit replaces it with the links.ts check.

New e2e suite test/e2e/app-dir/link-download-prefetch, which uses createRouterAct + LinkAccordion with includeAppShellRequests: true, so any request to the handler fails 'no-requests':

  • A regular <Link> still prefetches (control).
  • <Link download> with the default prefetch and with prefetch={true} makes no router requests on reveal or hover.
  • Clicking <Link download="report.csv"> makes no router request, keeps the URL, and downloads the file as report.csv (the handler sends Content-Disposition: attachment without a filename).

Verification

  • Without the links.ts change: 3 of 4 tests fail with pnpm test-start-turbo (the control passes).
  • With the change: all pass with pnpm test-start-turbo, pnpm test-start-webpack, and __NEXT_CACHE_COMPONENTS=true pnpm test-start-turbo; pnpm test-dev-turbo skips (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 types is clean.

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>
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Tests Passed

Commit: 8a4a012
View workflow run

@github-actions github-actions Bot added the created-by: Next.js team PRs by the Next.js team. label Nov 26, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant