Replies: 10 comments
-
It looks awesome @ccssmnn, is there any repo that you could share where I can have a look over the code? |
Beta Was this translation helpful? Give feedback.
-
@sairaj2119 a repo for the workaround? |
Beta Was this translation helpful? Give feedback.
-
yup |
Beta Was this translation helpful? Give feedback.
-
You could do it like this: import { RemixLinkProps } from "@remix-run/react/components";
import { useEffect, useRef, useState } from "react";
import { Link } from "remix";
interface MyLinkProps extends RemixLinkProps {
prefetchOnIntersect?: boolean; // add our own prop to the game
}
export const MyLink: React.FC<MyLinkProps> = (props) => {
const { prefetchOnIntersect, ...rest } = props; // split the props to not break anything
const ref = useRef<any>();
const [visible, setVisible] = useState(false);
useEffect(() => {
if (props.prefetchOnIntersect) {
const observer = new IntersectionObserver(([entry]) => {
setVisible(entry.isIntersecting);
});
observer.observe(ref.current);
return () => {
observer.disconnect();
};
}
}, [props.prefetchOnIntersect]);
return (
<Link
ref={ref}
{...rest}
prefetch={
props.prefetchOnIntersect && visible ? "render" : props.prefetch
}
/>
);
}; And use it anywhere like you would use the normal Link component. <MyLink to="/" prefetchOnIntersect>Hello</MyLink> |
Beta Was this translation helpful? Give feedback.
-
That helps thankyou @ccssmnn |
Beta Was this translation helpful? Give feedback.
-
The prefetch intent should support this, right now intent is just hover but I know the idea is to make it smarter, eg start the prefetch when the cursor is near the link not just hovering. |
Beta Was this translation helpful? Give feedback.
-
Relying on the cursor will fail on mobile. But I see how I saw prefetch only fetching some JS. Only clicking the link actually makes the data query. When showing a few links which are all links to blog posts (same |
Beta Was this translation helpful? Give feedback.
-
Yes, but that will be progressive enhancement for desktop users, mobile users can use another techniques to do the prefetch with accuracy
It prefetch data, code and links (from links functions), except in Safari where it only prefetch code and links I think |
Beta Was this translation helpful? Give feedback.
-
I like SvelteKit's implementation details as an example. Issue: sveltejs/kit#3633
NextJS also supports viewport prefetching using IntersectionObserver. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
What is the new or updated feature that you are suggesting?
Add the option
prefetch="screen"
orprefetch="intersect"
to the Link components.Why should this feature be included?
Currently, 3 types of prefetching links are supported:
none
does not prefetchintent
does prefetch on hover (only saves time between hover and click)render
does prefetch when being rendered (prefetches a link that is not even clickable, e.g. hidden or not visible yet)render
prefetches the link even if it is not viewed.intent
is quite precise, but only when the page is viewed on a device with a mouse.screen
/intersect
could take advantage of the Intersection Observer API to prefetch links, that appear on the screen.The current workaround would be to manually set the property to
render
with the Intersection Observer.I would be happy to contribute. 😊
Beta Was this translation helpful? Give feedback.
All reactions