Next.js Discord

Discord Forum

List all routes in app dir

Answered
Scottish Fold posted this in #help-forum
Open in Discord
Scottish FoldOP
How to get a list of all routes available in my project ?
It must ignore things like route groups and etc.
Answered by joulev
you can only either parse the app directory with fs yourself, or parse the output of next build
View full answer

20 Replies

@Scottish Fold How to get a list of all routes available in my project ? It must ignore things like route groups and etc.
just build the project (npm run build). you'll see the list of routes in the terminal log when the build completes
@joulev just build the project (`npm run build`). you'll see the list of routes in the terminal log when the build completes
Scottish FoldOP
I forgot to mention it should be done on the server, at execution time
Other possibility, perhaps better, is to generate it statically, at build time, and save it somewhere to be accessed at execution time.
@Scottish Fold Other possibility, perhaps better, is to generate it statically, at build time, and save it somewhere to be accessed at execution time.
hmm depending on why you need this feature, you could implement this in different ways. why do you need the feature?
@joulev hmm depending on why you need this feature, you could implement this in different ways. why do you need the feature?
Scottish FoldOP
I would like to allow my users to access their orgs with short urls like mysite.com/org-name.
however to do it I must ensure users cannot use certain names in their orgs
like route names that it's already used by the project
@Scottish Fold I would like to allow my users to access their orgs with short urls like mysite.com/org-name.
in that case i think you should hardcode a list of forbidden URLs. because let's consider this:

* your app doesn't have /hello (yet)
* a user makes /hello theirs
* in a later deployment, you ship a landing page at /hello, say, to greet newly created accounts
* what happens now?
so just hardcode a list of forbidden URLs and ensure that:
* users cannot create their pages at those URLs and
* you do not use anything outside those URLs for your app routes
so I am searching a way to automatically generating this list
@Scottish Fold so I am searching a way to automatically generating this list
you can make a simple nodejs script that uses fs to read the app folder, find page|route.js files in it and extract the routes from it
run the script at build time and write the output to a json file
then your app importing that json file
@joulev you can make a simple nodejs script that uses `fs` to read the `app` folder, find `page|route.js` files in it and extract the routes from it
Scottish FoldOP
sure, however I must consider some specific cases like route groups and probably another things that I am not considering yet.
Since Next.js has a route system, I believe the better option would extend it in some way
Scottish FoldOP
I just don't know how
@Scottish Fold Since Next.js has a route system, I believe the better option would extend it in some way
it doesn't expose anything like that for you to consume
you can only either parse the app directory with fs yourself, or parse the output of next build
Answer