Next.js Discord

Discord Forum

How to turn /app/01.filename.tsx to /filename?

Unanswered
Asiatic Lion posted this in #help-forum
Open in Discord
Asiatic LionOP
I'm writing an app and I want to sort files/folders for routes with NN.filename.ext,
but, I don't want those prefixes to polute the final routes for the end-user
Is there a way to achieve this in NextJS?

29 Replies

So since your pages and route in app directory needs to be either page.tsx or route.ts(x) and the folder dictating the page or route url then you only have the possibility to do grouped folders like "(01)" so the path would become "app/(01)/filename.ext/page.tsx"
Using a group folder, denominated by the "()", they won't affect the url, but will allow you to group pages / routes in the code.

These will also allow you to have a sub-layout for that group, if wanted
Don't know what "grouped folders" are
Can you give an example tree of what it would look like?
It's just a normal folder but having those () in the folder name. They are discarded when it comes to the url structure
Asiatic LionOP
(for reference, its my first day in nextjs, so I'm new to 99% of the concepts)
No worries 😊
Ideally I would want something like:
ROOT
|- 01.thing1.tsx
|- 02.thing2.tsx
|- 03.thing3.tsx
/thing1   <------- Accessible route
/thing2   <------- Accessible route
/thing3   <------- Accessible route
"/app/(somegroupname)/blog/page.tsx" becomes "/blog"
Asiatic LionOP
is there a way to programatically generate that from files stored in a certain folder?
that syntax/structure looks awful, so wondering if there is a way around it
So for any given page, the filename needs to be "page.tsx" or "page.jsx" and it's the folder which decides the name of the route
Asiatic LionOP
totally defeats the purpose of using NN.file.ext to begin with 😦
is there a way to programatically generate that from files stored in a certain folder?
Well that's the syntax that next uses
So what you can do is that you can use dynamic paths, but it's not advised unless it's a specific scenario like "/products/<id>" urls
Then your folder structure would be "/app/products/[id]/page.tsx"
That would allow you get the id from the parameters in your page function
Asiatic LionOP
I cannot find the link, but I just want behavior similar to what docusaurus does with .md and mdx files
it makes no sense to have to do this manually
that's why I'm asking if there is a way to programatically generate those folders from contents of a different folder
the 3file example is simple.... but add 50x files per folder, 10+levels deep... and this structure would be unmaintainable by a human
I don't mind doing it at buildtime or something, but... I don't know what NextJS has to achieve this or if I have to resort to some sort of external shell script to create the structure to feed to nextjs
Not that I know of. Not automated that way.
Then you'd have to create a dynamic catch-all route that can traverse through and load things. But that will complicate stuff a lot.

But if sounds like what you should do is creating one catch-all route that dynamically can render something based on data. Then use the generatestaticparams function to pretender all those pages
See how the file structure is "app/[slug]/page.tsx".

You can also have "app/[[...slug]]/page.tsx" if you want to have multiple dynamic paths in the url
Asiatic LionOP
looks promising indeed. will take a look ✍️
That function should return an array of the different "versions" of the url parameters that can exist for that page, which it will then pretender on build time