Skip to content

Commit 739eeec

Browse files
committed
no basePath for external links
1 parent da89d17 commit 739eeec

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/ui/link/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ const Link = forwardRef<HTMLButtonElement | HTMLAnchorElement, LinkProps>(
7272
)) ||
7373
''
7474
: to;
75+
const IS_EXTERNAL_LINK_REGEX = /^(?:(http|https):\/\/)/;
7576
const staticBasePath =
76-
href != null || typeof to === 'string' ? routeAttributes.basePath : '';
77+
(href != null && !IS_EXTERNAL_LINK_REGEX.test(href)) || typeof to === 'string' ? routeAttributes.basePath : '';
7778

7879
const triggerPrefetch = useCallback(() => {
7980
// ignore if async route not ready yet

src/ui/link/test.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ describe('<Link />', () => {
9191
);
9292
});
9393

94-
it('should push history with correct link for href when given basePath', () => {
94+
it('should push history with correct link for relative href when given basePath', () => {
9595
renderInRouter(
9696
'my link',
9797
{
@@ -105,6 +105,20 @@ describe('<Link />', () => {
105105
);
106106
});
107107

108+
it('should not add basePath for external links', () => {
109+
renderInRouter(
110+
'my link',
111+
{
112+
href: 'https://www.atlassian.com/',
113+
},
114+
'/base'
115+
);
116+
expect(screen.getByRole('link', { name: 'my link' })).toHaveAttribute(
117+
'href',
118+
'https://www.atlassian.com/'
119+
);
120+
});
121+
108122
it('should pass props to the child element', () => {
109123
renderInRouter('my link', {
110124
...defaultProps,

0 commit comments

Comments
 (0)