Skip to content

Commit

Permalink
move to ref check
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Jan 17, 2025
1 parent a5eb34d commit 8331e39
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions packages/next/src/client/app-dir/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,6 @@ function mountLinkInstance(
router: AppRouterInstance,
kind: PrefetchKind.AUTO | PrefetchKind.FULL
) {
// Element could be possible undefined which leads to failed on later
// WeakMap and IntersectionObserver operations.
if (!element) {
return
}
let prefetchUrl: URL | null = null
try {
prefetchUrl = createPrefetchURL(href)
Expand Down Expand Up @@ -645,12 +640,18 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>(
// currently mounted <Link> instances, e.g. so we can re-prefetch them after
// a revalidation or refresh.
const observeLinkVisibilityOnMount = React.useCallback(
(element: HTMLAnchorElement | SVGAElement) => {
(element: HTMLAnchorElement | SVGAElement | null) => {
if (prefetchEnabled && router !== null) {
mountLinkInstance(element, href, router, appPrefetchKind)
// The useMergedRef could return null
if (element) {
mountLinkInstance(element, href, router, appPrefetchKind)
}
}
return () => {
unmountLinkInstance(element)
// The useMergedRef could return null
if (element) {
unmountLinkInstance(element)
}
}
},
[prefetchEnabled, href, router, appPrefetchKind]
Expand Down

0 comments on commit 8331e39

Please sign in to comment.