feat: Add prefetching for links to improve performance
All checks were successful
Update Memento Dev on VPS / deploy (push) Successful in 53s

This commit is contained in:
Gauthier Daniels 2025-04-17 20:04:47 +02:00
parent 48a644b74a
commit 67f2ff697e
2 changed files with 4 additions and 0 deletions

View File

@ -1,4 +1,5 @@
import { usePageContext } from "vike-react/usePageContext"; import { usePageContext } from "vike-react/usePageContext";
import { prefetch } from "vike/client/router";
import clsx from "clsx"; import clsx from "clsx";
export function Link(props: React.AnchorHTMLAttributes<HTMLAnchorElement> & { href: string }) { export function Link(props: React.AnchorHTMLAttributes<HTMLAnchorElement> & { href: string }) {
@ -7,6 +8,8 @@ export function Link(props: React.AnchorHTMLAttributes<HTMLAnchorElement> & { hr
const isSameDomain = !(props.href.startsWith("http") || props.href.startsWith("mailto")); const isSameDomain = !(props.href.startsWith("http") || props.href.startsWith("mailto"));
const isDownload = props.href.endsWith(".pdf") || props.href.endsWith(".zip"); const isDownload = props.href.endsWith(".pdf") || props.href.endsWith(".zip");
const handleMouseEnter = () => prefetch(props.href);
return ( return (
<a <a
{...props} {...props}
@ -14,6 +17,7 @@ export function Link(props: React.AnchorHTMLAttributes<HTMLAnchorElement> & { hr
className={clsx(isActive && "is-active", props.className)} className={clsx(isActive && "is-active", props.className)}
{...(isDownload ? { download: true } : {})} {...(isDownload ? { download: true } : {})}
{...(!isSameDomain || isDownload ? { target: "_blank", rel: "noopener noreferrer" } : {})} {...(!isSameDomain || isDownload ? { target: "_blank", rel: "noopener noreferrer" } : {})}
{...(isSameDomain ? { onMouseEnter: handleMouseEnter } : {})}
> >
{props.children} {props.children}
</a> </a>

Binary file not shown.