Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(router): apply preload delay to viewport links#8044
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
d44878d9ae858c71a0e130c370795234e790956e76dddc18dFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| '@tanstack/react-router': patch | ||
| '@tanstack/solid-router': patch | ||
| '@tanstack/vue-router': patch | ||
| --- | ||
| Apply `preloadDelay` to viewport link preloading and cancel pending preloads when links leave the viewport. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -598,7 +598,7 @@ export function useLinkProps< | ||
| const hasRenderFetched = React.useRef(false) | ||
| const preload = | ||
| options.reloadDocument || externalLink | ||
| options.reloadDocument || externalLink || disabled | ||
| ? false | ||
| : (userPreload ?? router.options.defaultPreload) | ||
| const preloadDelay = | ||
| @@ -615,33 +615,58 @@ export function useLinkProps< | ||
| }, [router, _options]) | ||
| // eslint-disable-next-line react-hooks/rules-of-hooks | ||
| const preloadViewportIoCallback = React.useCallback( | ||
| (entry: IntersectionObserverEntry | undefined) => { | ||
| if (entry?.isIntersecting) { | ||
| const enqueuePreload = React.useCallback( | ||
| (e?: React.MouseEvent | React.FocusEvent | IntersectionObserverEntry) => { | ||
| if (!e) { | ||
| cancelPreload(innerRef) | ||
| return | ||
| } | ||
| if ( | ||
| !( | ||
| (e as IntersectionObserverEntry).isIntersecting ?? | ||
| preload === 'intent' | ||
| ) | ||
| ) { | ||
| if ((e as IntersectionObserverEntry).isIntersecting === false) { | ||
| cancelPreload(innerRef) | ||
| } | ||
| return | ||
| } | ||
| if (!preloadDelay) { | ||
| doPreload() | ||
| return | ||
| } | ||
| if (timeoutMap.has(innerRef)) { | ||
| return | ||
| } | ||
| timeoutMap.set( | ||
| innerRef, | ||
| setTimeout(() => { | ||
| timeoutMap.delete(innerRef) | ||
| doPreload() | ||
| }, preloadDelay), | ||
| ) | ||
| }, | ||
| [doPreload], | ||
| [doPreload, innerRef, preload, preloadDelay], | ||
| ) | ||
| // eslint-disable-next-line react-hooks/rules-of-hooks | ||
| useIntersectionObserver( | ||
| innerRef, | ||
| preloadViewportIoCallback, | ||
| intersectionObserverOptions, | ||
| !!disabled || preload !== 'viewport', | ||
| ) | ||
| useIntersectionObserver(innerRef, enqueuePreload, preload !== 'viewport') | ||
| // eslint-disable-next-line react-hooks/rules-of-hooks | ||
| React.useEffect(() => { | ||
| if (hasRenderFetched.current) { | ||
| return | ||
| } | ||
| if (!disabled && preload === 'render') { | ||
| if (preload === 'render') { | ||
| doPreload() | ||
| hasRenderFetched.current = true | ||
| } | ||
| }, [disabled, doPreload, preload]) | ||
| }, [doPreload, preload]) | ||
| // The click handler | ||
| const handleClick = (e: React.MouseEvent) => { | ||
| @@ -702,39 +727,14 @@ export function useLinkProps< | ||
| } | ||
| } | ||
| const enqueueIntentPreload = (e: React.MouseEvent | React.FocusEvent) => { | ||
| if (disabled || preload !== 'intent') return | ||
| if (!preloadDelay) { | ||
| doPreload() | ||
| return | ||
| } | ||
| const eventTarget = e.currentTarget | ||
| if (timeoutMap.has(eventTarget)) { | ||
| return | ||
| } | ||
| const id = setTimeout(() => { | ||
| timeoutMap.delete(eventTarget) | ||
| doPreload() | ||
| }, preloadDelay) | ||
| timeoutMap.set(eventTarget, id) | ||
| } | ||
| const handleTouchStart = (_: React.TouchEvent) => { | ||
| if (disabled || preload !== 'intent') return | ||
| const handleTouchStart = () => { | ||
| if (preload !== 'intent') return | ||
coderabbitai[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| doPreload() | ||
| } | ||
| const handleLeave = (e: React.MouseEvent | React.FocusEvent) => { | ||
| if (disabled || !preload || !preloadDelay) return | ||
| const eventTarget = e.currentTarget | ||
| const id = timeoutMap.get(eventTarget) | ||
| if (id) { | ||
| clearTimeout(id) | ||
| timeoutMap.delete(eventTarget) | ||
| const handleLeave = () => { | ||
| if (preload === 'intent') { | ||
| cancelPreload(innerRef) | ||
| } | ||
| } | ||
| @@ -746,8 +746,8 @@ export function useLinkProps< | ||
| ref: innerRef as React.ComponentPropsWithRef<'a'>['ref'], | ||
| onClick: composeHandlers([onClick, handleClick]), | ||
| onBlur: composeHandlers([onBlur, handleLeave]), | ||
| onFocus: composeHandlers([onFocus, enqueueIntentPreload]), | ||
| onMouseEnter: composeHandlers([onMouseEnter, enqueueIntentPreload]), | ||
| onFocus: composeHandlers([onFocus, enqueuePreload]), | ||
| onMouseEnter: composeHandlers([onMouseEnter, enqueuePreload]), | ||
| onMouseLeave: composeHandlers([onMouseLeave, handleLeave]), | ||
| onTouchStart: composeHandlers([onTouchStart, handleTouchStart]), | ||
| disabled: !!disabled, | ||
| @@ -766,10 +766,10 @@ const STATIC_DISABLED_PROPS = { role: 'link', 'aria-disabled': true } | ||
| const STATIC_ACTIVE_PROPS = { 'data-status': 'active', 'aria-current': 'page' } | ||
| const STATIC_TRANSITIONING_PROPS = { 'data-transitioning': 'transitioning' } | ||
| const timeoutMap = new WeakMap<EventTarget, ReturnType<typeof setTimeout>>() | ||
| const intersectionObserverOptions: IntersectionObserverInit = { | ||
| rootMargin: '100px', | ||
| const timeoutMap = new WeakMap<object, ReturnType<typeof setTimeout>>() | ||
| const cancelPreload = (eventTarget: object) => { | ||
| clearTimeout(timeoutMap.get(eventTarget)) | ||
| timeoutMap.delete(eventTarget) | ||
| } | ||
| const composeHandlers = | ||
| @@ -955,7 +955,7 @@ export function createLink<const TComp>( | ||
| * | ||
| * Props: | ||
| * - `preload`: Controls route preloading (eg. 'intent', 'render', 'viewport', true/false) | ||
| * - `preloadDelay`: Delay in ms before preloading on hover | ||
| * - `preloadDelay`: Delay in ms before preloading on focus, hover, or viewport entry | ||
| * - `activeProps`/`inactiveProps`: Additional props merged when link is active/inactive | ||
| * - `resetScroll`/`hashScrollIntoView`: Control scroll behavior on navigation | ||
| * - `viewTransition`/`startTransition`: Use View Transitions/React transitions for navigation | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.