Created
September 9, 2026 23:36
-
-
Save maiah/83f67121a2264f3c805cc10d4cc50bf2 to your computer and use it in GitHub Desktop.
Standalone React Link component with Astro prefetching and client side caching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { prefetch } from "astro:prefetch"; | |
| import { pathCache as cache } from "./path-cache"; | |
| export function Link({ | |
| href, | |
| children, | |
| pathCacheKey, | |
| }: { | |
| href: string; | |
| pathCacheKey: string; | |
| children: React.ReactNode; | |
| }) { | |
| const handlePrefetch = () => { | |
| if (!cache.get(pathCacheKey)) { | |
| prefetch(href); | |
| } | |
| }; | |
| return ( | |
| <a | |
| href={href} | |
| data-astro-cache-path={pathCacheKey} | |
| onMouseEnter={handlePrefetch} | |
| onTouchStart={handlePrefetch} | |
| > | |
| {children} | |
| </a> | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment