Skip to content

Commit

Permalink
Update link.tsxremove aggresssive prefetching
Browse files Browse the repository at this point in the history
  • Loading branch information
RhysSullivan committed Oct 23, 2024
1 parent 6fe5511 commit 16bb2a4
Showing 1 changed file with 3 additions and 33 deletions.
36 changes: 3 additions & 33 deletions src/components/ui/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import NextLink from "next/link";
import { useRouter } from "next/navigation";
import { useEffect, useRef, useState } from "react";
import { useRef, useState } from "react";

type PrefetchImage = {
srcset: string;
Expand Down Expand Up @@ -31,46 +31,16 @@ async function prefetchImages(href: string) {
const seen = new Set<string>();

export const Link: typeof NextLink = (({ children, ...props }) => {
const [images, setImages] = useState<PrefetchImage[]>([]);
const [preloading, setPreloading] = useState<(() => void)[]>([]);
const linkRef = useRef<HTMLAnchorElement>(null);
const router = useRouter();

useEffect(() => {
if (props.prefetch === false) {
return;
}

const linkElement = linkRef.current;
if (!linkElement) return;

const observer = new IntersectionObserver(
(entries) => {
const entry = entries[0];
if (entry.isIntersecting) {
router.prefetch(String(props.href));
void prefetchImages(String(props.href)).then((images) => {
setImages(images);
}, console.error);
// Stop observing once images are prefetched
observer.unobserve(entry.target);
}
},
{ rootMargin: "0px", threshold: 0.1 }, // Trigger when at least 10% is visible
);

observer.observe(linkElement);

return () => {
observer.disconnect(); // Cleanup the observer when the component unmounts
};
}, [props.href, props.prefetch, router]);

return (
<NextLink
ref={linkRef}
prefetch={false}
onMouseEnter={() => {
onMouseEnter={async () => {
const images = await prefetchImages(String(props.href));
router.prefetch(String(props.href));
if (preloading.length) return;
const p: (() => void)[] = [];
Expand Down

0 comments on commit 16bb2a4

Please sign in to comment.