withastro/astro
Prefetch
Astro ships a built-in prefetch script that warms up navigation by fetching link targets ahead of click. Source: packages/astro/src/prefetch/.
Purpose
Make navigations feel instant on slow networks by issuing a fetch (or <link rel="prefetch">) for likely-next pages. Configurable per-link with the data-astro-prefetch attribute.
Key files
| File | Purpose |
|---|---|
packages/astro/src/prefetch/index.ts |
The browser runtime (link observer + prefetch dispatcher). |
packages/astro/src/prefetch/vite-plugin-prefetch.ts |
Vite plugin that injects the prefetch script into the page. |
Public types for prefetch config live in packages/astro/src/types/public/config.ts.
Configuration
import { defineConfig } from 'astro/config';
export default defineConfig({
prefetch: {
prefetchAll: false, // Default: only links with data-astro-prefetch
defaultStrategy: 'hover', // 'tap' | 'hover' | 'viewport' | 'load'
},
});Per-link override:
<a href="/about" data-astro-prefetch="viewport">About</a>
<a href="/big-page" data-astro-prefetch="false">Skip prefetch</a>Strategies
| Strategy | When the prefetch fires |
|---|---|
tap |
mousedown / touchstart — first frame of intent. |
hover |
mouseenter after a short delay. |
viewport |
IntersectionObserver reports the link is visible. |
load |
Immediately on page load. |
The runtime observes links matching either the global setting or data-astro-prefetch="…", computes the URL, and schedules a fetch (with a small in-memory cache to avoid duplicates).
How it works
graph LR
A[Page load] --> B[Prefetch runtime starts]
B --> C[Find matching links]
C --> D{Strategy?}
D -- viewport --> E[IntersectionObserver]
D -- hover --> F[mouseenter timer]
D -- tap --> G[mousedown / touchstart]
D -- load --> H[Schedule immediately]
E --> I[fetch(href, { priority: 'low' })]
F --> I
G --> I
H --> I
I --> J[(in-memory cache)]Combined with view transitions, prefetch turns most navigations into a sub-100ms experience.
Related pages
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.