Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: treat absolute/same-origin/different-basename <Link to> values as external #10135

Merged
merged 4 commits into from
Feb 23, 2023
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
5 changes: 5 additions & 0 deletions .changeset/big-olives-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router-dom": patch
---

Treat absolute/same-origin/different-basename <Link to> values as external
73 changes: 72 additions & 1 deletion packages/react-router-dom/__tests__/link-click-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ describe("A <Link> click", () => {
<div>
<h1>Home</h1>
<Link
reloadDocument
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't have this on this Link since the external URL should be what forces it to behave as a full-document link click

to="https://remix.run"
onClick={(e) => {
handlerCalled = true;
Expand Down Expand Up @@ -171,6 +170,78 @@ describe("A <Link> click", () => {
});
});

describe("when a same-origin/different-basename absolute URL is specified", () => {
it.only("does not prevent default", () => {
brophdawg11 marked this conversation as resolved.
Show resolved Hide resolved
function Home() {
return (
<div>
<h1>Home</h1>
<Link to="http://localhost/not/base">About</Link>
</div>
);
}

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

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

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

expect(event.defaultPrevented).toBe(false);
});

it("calls provided listener", () => {
let handlerCalled;
let defaultPrevented;

function Home() {
return (
<div>
<h1>Home</h1>
<Link
to="http://localhost/not/base"
onClick={(e) => {
handlerCalled = true;
defaultPrevented = e.defaultPrevented;
}}
>
About
</Link>
</div>
);
}

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

act(() => {
click(node.querySelector("a"));
});

expect(handlerCalled).toBe(true);
expect(defaultPrevented).toBe(false);
});
});

describe("when reloadDocument is specified", () => {
it("does not prevent default", () => {
function Home() {
Expand Down
8 changes: 7 additions & 1 deletion packages/react-router-dom/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
createHashHistory,
UNSAFE_invariant as invariant,
joinPaths,
stripBasename,
ErrorResponse,
} from "@remix-run/router";

Expand Down Expand Up @@ -420,6 +421,8 @@ export const Link = React.forwardRef<HTMLAnchorElement, LinkProps>(
},
ref
) {
let { basename } = React.useContext(NavigationContext);

// Rendered into <a href> for absolute URLs
let absoluteHref;
let isExternal = false;
Expand All @@ -434,7 +437,10 @@ export const Link = React.forwardRef<HTMLAnchorElement, LinkProps>(
let targetUrl = to.startsWith("//")
? new URL(currentUrl.protocol + to)
: new URL(to);
if (targetUrl.origin === currentUrl.origin) {
let isSameBasename =
stripBasename(targetUrl.pathname, basename) != null;

if (targetUrl.origin === currentUrl.origin && isSameBasename) {
// Strip the protocol/origin for same-origin absolute URLs
to = targetUrl.pathname + targetUrl.search + targetUrl.hash;
} else {
Expand Down