Next.js Discord

Discord Forum

Disable a route at build time

Unanswered
Balinese posted this in #help-forum
Open in Discord
BalineseOP
Hey all,

I'm wondering whether there is a way to completely disable a route at build time, so that it never gets registered in the first place. Something like passing an environment variable at the time of building, and that changing whether the route gets registered. Lets say the app looks like this:

- /app
| - /togglable_route
| | - page.tsx
| page.tsx


Would there be a way for me to disable the /togglable_route at build time? Maybe make it so it's only enabled whenever I pass a special environment variable like ENABLE_MY_SECRET_ROUTE? I know I could just check for this variable at runtime, but that not only seems like a waste of time, I would also imagine that it's going to ship more unnecessary JavaScript to the client. Additionally, if there are many more routes under this specific route, and I would want to disable them all, it doesn't really seem like a feasible solution, as one could miss a single if statement and expose a certain route.

5 Replies

Giant panda
Yes, you can conditionally disable a route at build time. One way to do this is by using a dynamyc next.config.js file and environment variables. This approach lets you change your application's configuration and routes before the build even start
You can modify your next.config.js file to check for an environment variable. Based on this variable, you can exclude specific pages or directories from the build process.
You can add prebuild condition and on that remove one folder for the build to start
Pacific sand lance
maybe renaming /_togglable_route to /togglable_route before build if ENABLE_MY_SECRET_ROUTE is present, but it seems unmaintainable. id rather create sth like /(secret)/layout.tsx and check value of ENABLE_MY_SECRET_ROUTE here. if present render, otherwise throw not found.
@Pacific sand lance maybe renaming `/_togglable_route` to `/togglable_route` before build if `ENABLE_MY_SECRET_ROUTE` is present, but it seems unmaintainable. id rather create sth like `/(secret)/layout.tsx` and check value of `ENABLE_MY_SECRET_ROUTE` here. if present render, otherwise throw not found.
BalineseOP
That's what I thought at first, but I would imagine that by just throwing a not found error, it will bundle the route and unnecessarily ship it to the client, which I would like to avoid. And moving the route outside of the source before the build opens a brand new can of worms, because later on I would have to somehow restore it back to its place, even when it errors, etc., it really doesn't seem feasible, which is why I'm looking for a solution that would allow me to remove the route at build time, without affecting the sources (except removing the route, obviously).
Pacific sand lance
yes, that's why i said it seems unmaintainable. maybe checking against ENABLE_MY_SECRET_ROUTE in middleware? then you can either throw 404 or redirect (no extra pages in bundle)