Skip to content

Commit f225179

Browse files
authored
fix(Link): Do not ignore onMouseEnter prop with absolute href (vercel#32012)
Fixes vercel#22733 Regardless of whether it's recommended that Link be used with external href values or not, they can be used and `onMouseEnter` being swallowed with an external href value is unexpected behavior. ## Bug - [x] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [x] Make sure the linting passes by running `yarn lint`
1 parent 332cd06 commit f225179

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

packages/next/client/link.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,12 @@ function Link(props: React.PropsWithChildren<LinkProps>) {
291291
}
292292

293293
childProps.onMouseEnter = (e: React.MouseEvent) => {
294-
if (!isLocalURL(href)) return
295294
if (child.props && typeof child.props.onMouseEnter === 'function') {
296295
child.props.onMouseEnter(e)
297296
}
298-
prefetch(router, href, as, { priority: true })
297+
if (isLocalURL(href)) {
298+
prefetch(router, href, as, { priority: true })
299+
}
299300
}
300301

301302
// If child is an <a> tag and doesn't have a href attribute, or if the 'passHref' property is

test/integration/client-navigation/pages/absolute-url.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export async function getServerSideProps({ query: { port } }) {
1111

1212
export default function Page({ port }) {
1313
const router = useRouter()
14+
const [hover, setHover] = React.useState(false)
15+
1416
return (
1517
<>
1618
<Link href="https://vercel.com/">
@@ -61,6 +63,17 @@ export default function Page({ port }) {
6163
<Link href="mailto:[email protected]">
6264
<a id="mailto-link">mailto:[email protected]</a>
6365
</Link>
66+
<br />
67+
<Link href="https://vercel.com/">
68+
<a
69+
id="absolute-link-mouse-events"
70+
data-hover={hover}
71+
onMouseEnter={() => setHover(true)}
72+
onMouseLeave={() => setHover(false)}
73+
>
74+
https://vercel.com/
75+
</a>
76+
</Link>
6477
</>
6578
)
6679
}

test/integration/client-navigation/test/index.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,21 @@ describe('Client Navigation', () => {
118118
)
119119
})
120120

121+
it('should call mouse handlers with an absolute url', async () => {
122+
const browser = await webdriver(
123+
context.appPort,
124+
`/absolute-url?port=${context.appPort}`
125+
)
126+
127+
await browser.elementByCss('#absolute-link-mouse-events').moveTo()
128+
129+
expect(
130+
await browser
131+
.waitForElementByCss('#absolute-link-mouse-events')
132+
.getAttribute('data-hover')
133+
).toBe('true')
134+
})
135+
121136
it('should navigate an absolute local url', async () => {
122137
const browser = await webdriver(
123138
context.appPort,

0 commit comments

Comments
 (0)