Astro shows “Invalid prerender export” when a page exports prerender in a form it can’t read while building: a let, a variable, a function call or an environment variable. Astro decides which routes to prerender by reading the source statically, before your code runs. Write it as a literal: export const prerender = false; or export const prerender = true;.
Key Takeaways
- Only
export const prerender = trueor= falseis valid.let, computed values andimport.meta.envlookups are not supported here.- To decide prerendering per route programmatically, use an integration with the
astro:route:setuphook.- The hint in the error says it directly: “Mutable values declared at runtime are not supported.”
What triggers it
---
// Wrong: let instead of const
export let prerender = false;
---
---
// Wrong: computed at runtime
export const prerender = import.meta.env.PREVIEW !== 'true';
---
---
// Wrong: imported from elsewhere
import { isStatic } from '../config';
export const prerender = isStatic;
---
The fix
---
export const prerender = false;
---
Put it in the page or endpoint’s frontmatter (or at the top level of a .ts endpoint). Nothing else is required.
Setting prerender for many routes at once
If you wanted a computed value because many routes share a rule, use an integration hook instead. It runs at build time and can set prerender per route:
// astro.config.mjs
import { defineConfig } from 'astro/config';
const dynamicApi = {
name: 'dynamic-api',
hooks: {
'astro:route:setup': ({ route }) => {
if (route.component.startsWith('src/pages/api/')) {
route.prerender = false;
}
},
},
};
export default defineConfig({
integrations: [dynamicApi],
});
Every file under src/pages/api/ now renders on demand without each one exporting prerender. You still need an adapter; see Cannot use server-side rendering without an adapter.
Different builds for preview and production
If the goal was “server-rendered in preview, static in production”, use two configs, or set output from an environment variable in astro.config.mjs (config is plain JavaScript and can read process.env). Page-level prerender must stay literal.
The hybrid rendering tip explains prerender and output together, and the Astro SSR guide covers when on-demand rendering is worth it. Astro’s integration API reference documents astro:route:setup.
FAQ
Can I use export const prerender = ‘false’ as a string?
No. It must be the boolean literal true or false.
Do I need prerender = true on static pages?
Not with the default output: 'static'. Add it only when output: 'server' is set and you want a page prerendered.