Don't prerend as static HTML with Astro
In Astro, setting export const prerender = false; in a page or component file prevents it from being prerendered as static HTML during the build process. Here's when you should consider using this option
When to Use export const prerender = false;
-
Dynamic Content Requirements: If a page fetches data at request time (like real-time API responses or data that frequently changes), setting
prerender = falseensures that content isn’t outdated, as it will be dynamically rendered with each request instead of at build time. -
Authentication or User-Specific Content: For pages that require user authentication or display user-specific data (like a profile page), you’ll want the page to be rendered on request. Static rendering here could expose sensitive information or cause stale, unauthorized views.
-
Client-Side Rendering Needs: If your page relies heavily on client-side JavaScript or has components that need to initialize on page load with up-to-date data, setting
prerender = falseallows it to function similarly to a typical SPA (Single Page Application) page. -
Frequent Updates: For pages with frequently changing content—such as news feeds, dashboards, or live data streams—setting
prerender = falsehelps ensure users always see the latest data, as the page will always be rendered fresh for each request.
Example Use Case
// src/pages/live-data.astro
export const prerender = false;
// This page fetches live data from an API
---
const data = await fetch('https://api.example.com/live-data').then((res) => res.json());
---
"
Live Data
{JSON.stringify(data, null, 2)}
"
In this example, the live-data page is rendered on each request, so users always get the most current API data.