Prerender at Build Time
Components are compiled to static HTML during the build process.
This is an Alpha Release. APIs may change and some features might be unstable.
Prerender components to static HTML for lightning-fast lazy loading
Boost your Astro site's performance by prerendering below-the-fold components to static HTML fragments at build time. Tree-shaken Tailwind CSS included.
Components are compiled to static HTML during the build process.
HTML fragments are fetched only when they enter the viewport.
Optional CSS generation extracts only used classes.
Built-in file hashing skips unchanged components.
Choose between Parser or Container API renderers.
Changes are watched and automatically reprocessed.
npm install vite-plugin-astro-prerender pnpm add vite-plugin-astro-prerender bun add vite-plugin-astro-prerender
Add the integration to your astro.config.mjs:
import { defineConfig } from 'astro/config';
import { astroPrerenderIntegration } from 'vite-plugin-astro-prerender';
export default defineConfig({
integrations: [
astroPrerenderIntegration({
componentsDir: 'src/components/Lazy',
outputDir: 'public/prerendered',
renderer: 'parser'
})
]
});
Create your component in src/components/Lazy/MyComponent.astro:
---
// Your component logic
---
I am lazy loaded!
This HTML was generated at build time.
Use the loader to fetch and inject content:
import { createLazyLoader } from "vite-plugin-astro-prerender/client";
import { prerenderedPath } from "virtual:astro-prerender-config";
const loader = createLazyLoader({
baseUrl: prerenderedPath, // Auto-uses base from astro.config.mjs
});
// Load when element enters viewport
loader.observeAndLoad(
"MyComponent",
"#container-id",
{ rootMargin: "200px" }
); The sections below are lazy-loaded. Watch the console for debug logs!