Skip to content

Commit 80832fb

Browse files
authored
Added better detection for absolute urls in link component (#9994)
1 parent fca5e55 commit 80832fb

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

.changeset/afraid-scissors-rhyme.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-router-dom": patch
3+
---
4+
5+
Improved absolute url detection in `Link` component (now also supports `mailto:` urls)

packages/react-router-dom/__tests__/link-href-test.tsx

+44
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,50 @@ describe("<Link> href", () => {
128128

129129
expect(renderer.root.findByType("a").props.href).toEqual("//remix.run");
130130
});
131+
132+
test('<Link to="mailto:[email protected]"> is treated as external link', () => {
133+
let renderer: TestRenderer.ReactTestRenderer;
134+
TestRenderer.act(() => {
135+
renderer = TestRenderer.create(
136+
<MemoryRouter initialEntries={["/inbox/messages"]}>
137+
<Routes>
138+
<Route path="inbox">
139+
<Route
140+
path="messages"
141+
element={<Link to="mailto:[email protected]" />}
142+
/>
143+
</Route>
144+
</Routes>
145+
</MemoryRouter>
146+
);
147+
});
148+
149+
expect(renderer.root.findByType("a").props.href).toEqual(
150+
151+
);
152+
});
153+
154+
test('<Link to="web+remix://somepath"> is treated as external link', () => {
155+
let renderer: TestRenderer.ReactTestRenderer;
156+
TestRenderer.act(() => {
157+
renderer = TestRenderer.create(
158+
<MemoryRouter initialEntries={["/inbox/messages"]}>
159+
<Routes>
160+
<Route path="inbox">
161+
<Route
162+
path="messages"
163+
element={<Link to="web+remix://somepath" />}
164+
/>
165+
</Route>
166+
</Routes>
167+
</MemoryRouter>
168+
);
169+
});
170+
171+
expect(renderer.root.findByType("a").props.href).toEqual(
172+
"web+remix://somepath"
173+
);
174+
});
131175
});
132176

133177
describe("in a dynamic route", () => {

packages/react-router-dom/index.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,7 @@ export const Link = React.forwardRef<HTMLAnchorElement, LinkProps>(
420420
) {
421421
// `location` is the unaltered href we will render in the <a> tag for absolute URLs
422422
let location = typeof to === "string" ? to : createPath(to);
423-
let isAbsolute =
424-
/^[a-z+]+:\/\//i.test(location) || location.startsWith("//");
423+
let isAbsolute = /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i.test(location);
425424

426425
// Location to use in the click handler
427426
let navigationLocation = location;

0 commit comments

Comments
 (0)