#Render pages at the same path both statically and on demand

9 messages · Page 1 of 1 (latest)

dawn glade
#

I have a [...preview].astro page where I render several static pages using getStaticPaths. However, I want some of those paths to be rendered on demand with prerender = false. How can I do this in the same file?

left breachBOT
#

Hi @dawn glade. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

You can’t have the same route (same URL pattern) be both statically prerendered and on‑demand from a single [...preview].astro file. Astro’s routing model gives each route a single prerender value, and prerender must be a static boolean per file/route, not per‐path inside getStaticPaths. [Routing prerender; Removed dynamic prerender]

Relevant points from the docs:

Because of that, a single [...preview].astro file must be either:

  • prerendered with getStaticPaths() (all its paths static), or
  • on‑demand (export const prerender = false) with no getStaticPaths() (all its paths SSR).

If you need a mix of static and on‑demand pages:

#
  1. Put static routes in a file that uses getStaticPaths() and is prerendered.
  2. Put on‑demand routes in a separate file (different pattern) with export const prerender = false. [Routing reference; On-demand dynamic routes]

Or, if you truly must decide per route outside the file, you can only toggle prerender per route definition via an integration’s astro:route:setup hook (e.g. match /blog/[slug].astro and set route.prerender), but again this is per route, not per individual getStaticPaths entry. [astro:route:setup]

The knowledge sources don’t describe any supported way to render some getStaticPaths entries statically and others on demand from the same Astro file.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@dawn glade Kapa.ai is still learning and improving, please let me know how I did by reacting below.

dawn glade
#

@left breach I wrote [...preview].astro to handle static pages like /react/previews/static-page, and then I created another file at src/pages/react/previews/[...dynamic].astro for the dynamic paths. This works in production because the server matches the static pages first and only falls back to the dynamic pages if there is no static match. In development though, it seems to match the dynamic path first. How can I fix this in development?

left breachBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

In Astro, all routes (including multiple rest-parameter routes) are prioritized using the same “route priority order” rules in both dev and build. [Routing priority]

Those rules say, in part:

  • Routes with more path segments win over less specific routes.
  • Static routes win over dynamic.
  • Named-parameter routes win over rest-parameter routes.
  • If still tied, they’re sorted alphabetically. [Routing priority]

There is no documented special‑case for “production server falls back differently than dev,” and the knowledge sources don’t describe any configuration to override the priority rules separately for dev. So if:

  • /react/previews/[...preview].astro
  • /react/previews/[...dynamic].astro

both exist and can match the same URL, they will be ordered only by those generic rules (path length, static vs dynamic, named vs rest, then alphabetical). The docs do not state that one of these two patterns should always win over the other, nor do they describe a way to force dev to prefer one rest route over another beyond those rules. [Routing priority]

Because of that, the documentation doesn’t give a supported way to “fix” dev to behave differently from what the global priority rules dictate. The reliable approaches described are:

#
  • Ensure URLs that should be static are handled by clearly higher‑priority routes (e.g. use a more specific pattern with named parameters instead of two competing rest routes). [Dynamic routes; Static SSG mode]
  • Or consolidate behavior into a single dynamic route and decide inside the component how to handle “static‑like” vs “dynamic” pages, but then you lose per‑route prerender control. The docs do not show a mixed approach per‑URL from separate files. [On-demand dynamic routes; prerender]

If you need precise, different handling for these two sets of URLs, the safest change (within what’s documented) is to redesign the route patterns so they don’t compete under the same priority rules—for example, give the “static” pages a non‑rest or more specific pattern so they will always win under the documented ordering.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: