#Disable routes based on feature flags

9 messages · Page 1 of 1 (latest)

zenith heath
#

I have a bunch of pages that I want to enable/disable (not even build them) based on some env variables.... can you help me understand th ebest way to do this?

smoky trenchBOT
#

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:

  1. Prefix with _ to exclude from routing

    Any file or directory in src/pages whose name starts with _ is ignored by the router and not built into dist/. [Excluding pages]

    src/pages/
      _experimental/      ← not built
        feature-a.astro
      _feature-b.astro    ← not built
      index.astro         ← built
    

    This is static (not env‑driven), but it’s the only documented way to “not even build” a page.

  2. 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 prerender flag, and that flag can depend on import.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.

zenith heath
#

Maybe custom astro integation?

#

@smoky trench maybe custom astro ingration?

smoky trenchBOT
#

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]

If you need env‑driven behavior, you’d have to combine this with external tooling (e.g. a script that renames/moves files before astro build), but that is not covered in the Astro docs in these sources.

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