Skip to content
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

Cancel images on mouse-leave #38

Merged
merged 3 commits into from
Oct 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 37 additions & 13 deletions src/components/ui/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function sleep(ms: number) {
}

async function prefetchImages(href: string) {
if (!href.startsWith("/") || href.startsWith("/order")) {
if (!href.startsWith("/") || href.startsWith("/order") || href === "/") {
return [];
}
const url = new URL(href, window.location.href);
Expand All @@ -36,6 +36,7 @@ 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();
let prefetchTimeout: NodeJS.Timeout | null = null; // Track the timeout ID
Expand Down Expand Up @@ -85,21 +86,21 @@ export const Link: typeof NextLink = (({ children, ...props }) => {
<NextLink
ref={linkRef}
prefetch={false}
onMouseOver={() => {
onMouseEnter={() => {
router.prefetch(String(props.href));
if (preloading.length) return;
const p: (() => void)[] = [];
for (const image of images) {
if (image.loading === "lazy" || seen.has(image.srcset)) {
continue;
}
const img = new Image();
img.decoding = "async";
img.fetchPriority = "low";
img.sizes = image.sizes;
seen.add(image.srcset);
img.srcset = image.srcset;
img.src = image.src;
img.alt = image.alt;
const remove = prefetchImage(image);
if (remove) p.push(remove);
}
setPreloading(p);
}}
onMouseLeave={() => {
for (const remove of preloading) {
remove();
}
setPreloading([]);
}}
onMouseDown={(e) => {
const url = new URL(String(props.href), window.location.href);
Expand All @@ -121,3 +122,26 @@ export const Link: typeof NextLink = (({ children, ...props }) => {
</NextLink>
);
}) as typeof NextLink;

function prefetchImage(image: PrefetchImage) {
Copy link
Collaborator

@gaojude gaojude Oct 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach works, but a more idiomatic solution would be to use React’s preload function in the RSC and prefetching the RSC payload would trigger the image to download at the time of prefetching?

if (image.loading === "lazy" || seen.has(image.srcset)) {
return;
}
const img = new Image();
img.decoding = "async";
img.fetchPriority = "low";
img.sizes = image.sizes;
seen.add(image.srcset);
img.srcset = image.srcset;
img.src = image.src;
img.alt = image.alt;
let done = false;
img.onload = img.onerror = () => {
done = true;
};
return () => {
if (done) return;
img.src = img.srcset = "";
seen.delete(image.srcset);
};
}