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

08 Mar, 2025 4 min min de lecture
Don't prerend as static HTML with Astro

When to Use export const prerender = false;

  1. Dynamic Content Requirements: If a page fetches data at request time (like real-time API responses or data that frequently changes), setting prerender = false ensures that content isn’t outdated, as it will be dynamically rendered with each request instead of at build time.

  2. 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.

  3. 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 = false allows it to function similarly to a typical SPA (Single Page Application) page.

  4. Frequent Updates: For pages with frequently changing content—such as news feeds, dashboards, or live data streams—setting prerender = false helps 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.