Files
Zack TannerandGitHub d330f7b02c fix: ensure mpa navigation render side effects are only fired once (#55032)
This is to fix an issue where these redirect side effects can be fired multiple times when the router reducer state changes. This block is still run when the router state updates, which can lead to superfluous attempts to redirect to a page.

With these changes, we keep track of the page that is being redirected to. If a re-render occurs while that request is in flight, we don't trigger the side effects. 

[Slack x-ref](https://vercel.slack.com/archives/C04DUD7EB1B/p1694049914264839)
2023-09-07 20:53:07 +00:00

39 lines
794 B
JavaScript

'use client'
import Link from 'next/link'
import { useEffect, useRef } from 'react'
export default function Page() {
const prefetchRef = useRef()
const slowPageRef = useRef()
useEffect(() => {
function triggerPrefetch() {
const event = new MouseEvent('mouseover', {
view: window,
bubbles: true,
cancelable: true,
})
prefetchRef.current.dispatchEvent(event)
console.log('dispatched')
}
slowPageRef.current.click()
setInterval(() => {
triggerPrefetch()
}, 1000)
}, [])
return (
<>
<Link id="link-to-slow-page" href="/slow-page" ref={slowPageRef}>
To /slow-page
</Link>
<Link id="prefetch-link" href="/hash" ref={prefetchRef}>
Prefetch link
</Link>
</>
)
}