@@ -52,6 +52,12 @@ export const Link = React.forwardRef(function Link(
52
52
const { externalLinksTarget } = React . useContext ( LinkSettingsContext ) ;
53
53
const trackEvent = useTrackEvent ( ) ;
54
54
const forwardedClassNames = useClassnames ( classNames || [ ] ) ;
55
+ // Use specified target or fallback to the default target based on the external links setting.
56
+ const target =
57
+ props . target ??
58
+ ( externalLinksTarget === SiteExternalLinksTarget . Blank ? '_blank' : undefined ) ;
59
+ // Automatically set rel if target is _blank, or use the specified rel.
60
+ const rel = props . rel ?? ( target === '_blank' ? 'noopener noreferrer' : undefined ) ;
55
61
56
62
// Use a real anchor tag for external links,s and a Next.js Link for internal links.
57
63
// If we use a NextLink for external links, Nextjs won't rerender the top-level layouts.
@@ -68,12 +74,10 @@ export const Link = React.forwardRef(function Link(
68
74
isExternalLink ( href , window . location . origin ) &&
69
75
// When the page is embedded in an iframe, for security reasons other urls cannot be opened.
70
76
// In this case, we open the link in a new tab.
71
- ( window . self !== window . top ||
72
- // If the site is configured to open links in a new tab
73
- externalLinksTarget === SiteExternalLinksTarget . Blank )
77
+ ( window . self !== window . top || target === '_blank' )
74
78
) {
75
79
event . preventDefault ( ) ;
76
- window . open ( href , '_blank' ) ;
80
+ window . open ( href , '_blank' , rel ) ;
77
81
}
78
82
79
83
domProps . onClick ?.( event ) ;
@@ -89,9 +93,8 @@ export const Link = React.forwardRef(function Link(
89
93
{ ...domProps }
90
94
href = { href }
91
95
onClick = { onClick }
92
- { ...( externalLinksTarget === SiteExternalLinksTarget . Blank && ! domProps . target
93
- ? { target : '_blank' , rel : 'noopener noreferrer' }
94
- : { } ) }
96
+ target = { target }
97
+ rel = { rel }
95
98
>
96
99
{ children }
97
100
</ a >
0 commit comments