-
Notifications
You must be signed in to change notification settings - Fork 27.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Control <Image /> prefetching with React (#18904)
This pull request fixes `<Image />` not updating when new props are passed by removing external DOM mutations and relying on React to do it instead. As an added bonus, I've extracted the intersection observer from both the `<Image />` and `<Link />` component, as their instance can be shared! The increase in size is minor (+3B), and actually a decrease for apps using both `<Image />` and `<Link />`. --- Fixes #18698 Fixes #18369
- Loading branch information
Showing
7 changed files
with
183 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { useCallback, useEffect, useRef, useState } from 'react' | ||
|
||
type UseIntersectionObserverInit = Pick<IntersectionObserverInit, 'rootMargin'> | ||
type UseIntersection = { disabled?: boolean } & UseIntersectionObserverInit | ||
type ObserveCallback = (isVisible: boolean) => void | ||
|
||
const hasIntersectionObserver = typeof IntersectionObserver !== 'undefined' | ||
|
||
export function useIntersection<T extends Element>({ | ||
rootMargin, | ||
disabled, | ||
}: UseIntersection): [(element: T | null) => void, boolean] { | ||
const isDisabled = disabled || !hasIntersectionObserver | ||
|
||
const unobserve = useRef<Function>() | ||
const [visible, setVisible] = useState(false) | ||
|
||
const setRef = useCallback( | ||
(el: T | null) => { | ||
if (unobserve.current) { | ||
unobserve.current() | ||
unobserve.current = undefined | ||
} | ||
|
||
if (isDisabled || visible) return | ||
|
||
if (el && el.tagName) { | ||
unobserve.current = observe( | ||
el, | ||
(isVisible) => isVisible && setVisible(isVisible), | ||
{ rootMargin } | ||
) | ||
} | ||
}, | ||
[isDisabled, rootMargin, visible] | ||
) | ||
|
||
useEffect(() => { | ||
if (!hasIntersectionObserver) { | ||
if (!visible) setVisible(true) | ||
} | ||
}, [visible]) | ||
|
||
return [setRef, visible] | ||
} | ||
|
||
function observe( | ||
element: Element, | ||
callback: ObserveCallback, | ||
options: UseIntersectionObserverInit | ||
) { | ||
const { id, observer, elements } = createObserver(options) | ||
elements.set(element, callback) | ||
|
||
observer.observe(element) | ||
return function unobserve() { | ||
observer.unobserve(element) | ||
|
||
// Destroy observer when there's nothing left to watch: | ||
if (elements.size === 0) { | ||
observer.disconnect() | ||
observers.delete(id) | ||
} | ||
} | ||
} | ||
|
||
const observers = new Map< | ||
string, | ||
{ | ||
id: string | ||
observer: IntersectionObserver | ||
elements: Map<Element, ObserveCallback> | ||
} | ||
>() | ||
function createObserver(options: UseIntersectionObserverInit) { | ||
const id = options.rootMargin || '' | ||
let instance = observers.get(id) | ||
if (instance) { | ||
return instance | ||
} | ||
|
||
const elements = new Map<Element, ObserveCallback>() | ||
const observer = new IntersectionObserver((entries) => { | ||
entries.forEach((entry) => { | ||
const callback = elements.get(entry.target) | ||
const isVisible = entry.isIntersecting || entry.intersectionRatio > 0 | ||
if (callback && isVisible) { | ||
callback(isVisible) | ||
} | ||
}) | ||
}, options) | ||
|
||
observers.set( | ||
id, | ||
(instance = { | ||
id, | ||
observer, | ||
elements, | ||
}) | ||
) | ||
return instance | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.