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

Eager image prefetch for search and fix prefetch for categories #37

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
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