#Disable routes based on feature flags
9 messages · Page 1 of 1 (latest)
Hi @zenith heath. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Astro doesn’t document a way to conditionally register routes at build time based on env flags. The router is purely file‑system based: any supported page file in src/pages becomes a route, unless it’s excluded. [Routing ref]
From the docs, there are only two supported ways to prevent a file from becoming a route:
-
Prefix with
_to exclude from routingAny file or directory in
src/pageswhose name starts with_is ignored by the router and not built intodist/. [Excluding pages]src/pages/ _experimental/ ← not built feature-a.astro _feature-b.astro ← not built index.astro ← builtThis is static (not env‑driven), but it’s the only documented way to “not even build” a page.
-
Use
prerender/output: 'server'for runtime behavior (does not disable the route)You can control whether a route is prerendered vs. server‑rendered with an exported
prerenderflag, and that flag can depend onimport.meta.env. [Routing ref]--- // src/pages/feature-a.astro export const prerender = import.meta.env.FEATURE_A_STATIC === 'true'; ---But this only affects how it’s rendered, not whether the route exists.
Because the docs don’t describe any env‑based mechanism to skip creating routes, the best you can do with official features is:
- Use
_prefixes to permanently/temporarily exclude routes from builds. [Excluding pages] - Optionally combine that with your own build tooling (outside of Astro, e.g. scripts that rename/move files before
astro build) to apply your feature flags.
There’s no documented, built‑in way to say “if ENV=false, don’t register this page as a route at all” purely from within Astro’s API.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@zenith heath Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Let me check the knowledge base for you again. One moment! :books:
The knowledge sources only describe one built‑in way to prevent routes from being created: prefixing files/directories in src/pages with _, which makes Astro ignore them and not build them. [Excluding pages]
They do not mention using a custom integration (or any plugin API) to conditionally disable routes based on env/feature flags. Because of that, I can’t confidently say that an integration‑based solution is supported or how it should be implemented.
Given the docs we have, the only documented approach is:
- Use
_prefixes to exclude pages or directories from routing and from the build output. [Excluding pages]