Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanniser committed Oct 21, 2024
2 parents a282a1e + bdc7343 commit 25d9be5
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/components/ui/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ function sleep(ms: number) {
}

async function prefetchImages(href: string) {
if (!href.startsWith("/products")) {
if (!href.startsWith("/") || href.startsWith("/order")) {
return [];
}
// Delay the prefetch until after Next.js has time to prefetch the page itself.
await sleep(1000);
const url = new URL(href, window.location.href);
const imageResponse = await fetch(`/api/prefetch-images${url.pathname}`, {
priority: "low",
Expand Down Expand Up @@ -55,7 +53,9 @@ export const Link: typeof NextLink = (({ children, ...props }) => {
const entry = entries[0];
if (entry.isIntersecting) {
// Set a timeout to trigger prefetch after 1 second
prefetchTimeout = setTimeout(() => {
prefetchTimeout = setTimeout(async () => {
router.prefetch(String(props.href));
await sleep(0); // We want the doc prefetches to happen first.
void prefetchImages(String(props.href)).then((images) => {
setImages(images);
}, console.error);
Expand Down Expand Up @@ -84,7 +84,9 @@ export const Link: typeof NextLink = (({ children, ...props }) => {
return (
<NextLink
ref={linkRef}
prefetch={false}
onMouseOver={() => {
router.prefetch(String(props.href));
for (const image of images) {
if (image.loading === "lazy" || seen.has(image.srcset)) {
continue;
Expand Down

0 comments on commit 25d9be5

Please sign in to comment.