Astro gives you a few different tools that, combined, get you close to “fast SPA-like navigation,” but they work within normal HTTP caching rules rather than bypassing them.
1. Use Astro’s prefetching
Enable prefetch globally and (optionally) for all links:
import { defineConfig } from 'astro/config';
export default defineConfig({
prefetch: {
prefetchAll: true,
defaultStrategy: 'viewport',
},
});
This injects a small script that:
- Prefetches internal links using
<link rel="prefetch"> when supported, falling back to fetch() otherwise. [Prefetch guide; Browser support]
- Respects data-saver and slow connections, and lets you tune behavior per-link via
data-astro-prefetch. [Prefetch guide]
You can also prefetch programmatically for non-<a> navigations:
<script>
import { prefetch } from 'astro:prefetch';
prefetch('/next-page');
</script>
[Prefetch programmatically]
2. (Optional) Enable client-side prerendering (Speculation Rules)
To get closer to SPA-like “instant” navigations, you can turn on the experimental clientPrerender flag, which uses the Speculation Rules API to prerender prefetched pages in supported browsers (currently Chrome):
export default defineConfig({
prefetch: {
prefetchAll: true,
defaultStrategy: 'viewport',
},
experimental: {
clientPrerender: true,
},
});