Unverified Commit 7229934b authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

fix: always ensure element before set to weakmap (#75012)

### What

Regression introduced in #74670, where the element somehow is invalid to
be observed by observer or be set as key in to WeakMap. The possible
cause is `mountInstance` is calling with `null`.

```
TypeError: Failed to execute 'observe' on 'IntersectionObserver': parameter 1 is not of type 'Element'.
```

```
TypeError: Invalid value used as weak map key
    at WeakMap.set (<anonymous>)
```

The root cause is the `useMergedRef` could return `null` in callback, we
need to filter them out before passing to `mountLinkInstance` or
`unmountLinkInstance`
parent ba7957e1
...@@ -640,12 +640,17 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>( ...@@ -640,12 +640,17 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>(
// currently mounted <Link> instances, e.g. so we can re-prefetch them after // currently mounted <Link> instances, e.g. so we can re-prefetch them after
// a revalidation or refresh. // a revalidation or refresh.
const observeLinkVisibilityOnMount = React.useCallback( const observeLinkVisibilityOnMount = React.useCallback(
(element: HTMLAnchorElement | SVGAElement) => { (element: HTMLAnchorElement | SVGAElement | null) => {
if (prefetchEnabled && router !== null) { if (prefetchEnabled && router !== null) {
mountLinkInstance(element, href, router, appPrefetchKind) // FIXME: element still can be null here in some cases. Require further investigation.
if (element) {
mountLinkInstance(element, href, router, appPrefetchKind)
}
} }
return () => { return () => {
unmountLinkInstance(element) if (element) {
unmountLinkInstance(element)
}
} }
}, },
[prefetchEnabled, href, router, appPrefetchKind] [prefetchEnabled, href, router, appPrefetchKind]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment