Skip to content

fix(react-router): let the browser handle clicks on <Link download> - #15815

Open
burrows99 wants to merge 2 commits into
remix-run:mainfrom
burrows99:fix/link-download-attribute
Open

burrows99 wants to merge 2 commits into
remix-run:mainfrom
burrows99:fix/link-download-attribute

Conversation

@burrows99

Copy link
Copy Markdown

<Link download> renders <a download>, but its click handler still prevents the default and navigates with client-side routing, so the browser never downloads the file. With a static file such as <Link to="/report.csv" download>, the router navigates to /report.csv, no route matches, and the page shows the 404 boundary. The same happens to a custom link built with useLinkClickHandler.

shouldProcessLinkClick already leaves clicks to the browser for non-left buttons, modifier keys and target="_blank". This adds the same check for an element with a download attribute:

https://github.com/remix-run/react-router/blob/9916920a/packages/react-router/lib/dom/dom.ts#L34-L43

It reads the attribute from event.currentTarget, so download, download="name.csv" and custom links are covered, and download={false} (which React does not render) still navigates as before. Links without download are unchanged.

flowchart TD
    A[User clicks a Link with download] --> B{Click handler}
    B -->|Before| C[Client side navigation to a 404 page]
    B -->|After| D[Browser downloads the file]
Loading

Narrated recording, before then after (sound on):

evidence.mp4

The recording is the Framework Mode playground (playground/framework, react-router dev) with a public/report.csv and two links added locally (not part of this PR): <Link to="/report.csv" download> and <Link to="/report.csv" download="sales-2026.csv">. Before uses react-router built from main, after uses this branch.

Edge cases and unchanged behaviour checked

In the running app, on this branch:

  • download="sales-2026.csv": the file is saved as sales-2026.csv, the page stays on /.
  • Keyboard: Tab to the link and Enter: the file is downloaded, the page stays on /.
  • The Product and Home links still navigate client side (same document, no reload).

By tests only:

  • download={false} still navigates client side.
  • A custom link built with useLinkClickHandler and download leaves the click to the browser.

Tests

  • link-click-test.tsx: <Link download> does not prevent default and stays on the page; <Link download={false}> still navigates.
  • useLinkClickHandler-test.tsx: a custom link with download does not prevent default.

On main (fix reverted):

Tests:       2 failed, 17 passed, 19 total

On this branch:

pnpm test packages/react-router/__tests__/dom/   # Tests: 864 passed (34 suites)
npx tsc --noEmit -p packages/react-router         # no errors
npx eslint / prettier --check on the changed files # clean

Integration tests were not run.

AI assistance (Claude Code) was used to find the bug, write the change and tests, and record the video.

🤖 Generated with Claude Code

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants