Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(react-router): let the browser handle clicks on <Link download>
  • Loading branch information
burrows99 committed Nov 23, 2026
commit 3f3fbb4b9a4da6d82dfd810ec852c868dd1483af
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Let the browser handle clicks on a `<Link download>` (and on custom links built with `useLinkClickHandler`) so the file is downloaded instead of being navigated to with client-side routing
76 changes: 76 additions & 0 deletions packages/react-router/__tests__/dom/link-click-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,82 @@ describe("A <Link> click", () => {
});
});

describe("when the link has a download attribute", () => {
it("lets the browser download the file", () => {
function Home() {
return (
<div>
<h1>Home</h1>
<Link to="../report.csv" download>
Report
</Link>
</div>
);
}

act(() => {
ReactDOM.createRoot(node).render(
<MemoryRouter initialEntries={["/home"]}>
<Routes>
<Route path="home" element={<Home />} />
<Route path="*" element={<h1>Not Found</h1>} />
</Routes>
</MemoryRouter>,
);
});

let anchor = node.querySelector("a");
expect(anchor).not.toBeNull();

let event: MouseEvent;
act(() => {
event = click(anchor);
});

expect(event.defaultPrevented).toBe(false);
let h1 = node.querySelector("h1");
expect(h1).not.toBeNull();
expect(h1?.textContent).toEqual("Home");
});

it("navigates to the new page when download is false", () => {
function Home() {
return (
<div>
<h1>Home</h1>
<Link to="../about" download={false}>
About
</Link>
</div>
);
}

act(() => {
ReactDOM.createRoot(node).render(
<MemoryRouter initialEntries={["/home"]}>
<Routes>
<Route path="home" element={<Home />} />
<Route path="about" element={<h1>About</h1>} />
</Routes>
</MemoryRouter>,
);
});

let anchor = node.querySelector("a");
expect(anchor).not.toBeNull();

let event: MouseEvent;
act(() => {
event = click(anchor);
});

expect(event.defaultPrevented).toBe(true);
let h1 = node.querySelector("h1");
expect(h1).not.toBeNull();
expect(h1?.textContent).toEqual("About");
});
});

describe("when the modifier keys are used", () => {
it("stays on the same page", () => {
function Home() {
Expand Down
42 changes: 42 additions & 0 deletions packages/react-router/__tests__/dom/useLinkClickHandler-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,48 @@ describe("Custom link with useLinkClickHandler", () => {
});
});

describe("when the link has a download attribute", () => {
it("stays on the same page", () => {
act(() => {
ReactDOM.createRoot(node).render(
<MemoryRouter initialEntries={["/home"]}>
<Routes>
<Route
path="home"
element={
<div>
<h1>Home</h1>
<CustomLink to="../about" download>
About
</CustomLink>
</div>
}
/>
<Route path="about" element={<h1>About</h1>} />
</Routes>
</MemoryRouter>,
);
});

let anchor = node.querySelector("a");
expect(anchor).not.toBeNull();

let event = new MouseEvent("click", {
view: window,
bubbles: true,
cancelable: true,
});
act(() => {
anchor?.dispatchEvent(event);
});

expect(event.defaultPrevented).toBe(false);
let h1 = node.querySelector("h1");
expect(h1).not.toBeNull();
expect(h1?.textContent).toEqual("Home");
});
});

describe("when the modifier keys are used", () => {
it("stays on the same page", () => {
act(() => {
Expand Down
8 changes: 6 additions & 2 deletions packages/react-router/lib/dom/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function isInputElement(object: any): object is HTMLInputElement {

type LimitedMouseEvent = Pick<
MouseEvent,
"button" | "metaKey" | "altKey" | "ctrlKey" | "shiftKey"
"button" | "metaKey" | "altKey" | "ctrlKey" | "shiftKey" | "currentTarget"
>;

function isModifiedEvent(event: LimitedMouseEvent) {
Expand All @@ -38,7 +38,11 @@ export function shouldProcessLinkClick(
return (
event.button === 0 && // Ignore everything but left clicks
(!target || target === "_self") && // Let browser handle "target=_blank" etc.
!isModifiedEvent(event) // Ignore clicks with modifier keys
!isModifiedEvent(event) && // Ignore clicks with modifier keys
!(
isHtmlElement(event.currentTarget) &&
event.currentTarget.hasAttribute("download")
) // Let browser handle <a download>
);
}

Expand Down
Loading