Next.js Discord

Discord Forum

Multiple pages in one route?

Answered
Dwarf Hotot posted this in #help-forum
Open in Discord
Avatar
Dwarf HototOP
Maybe I'm overlooking something, but is the only way to have multiple pages under one route to export an implementation of getStaticPaths?

Use case: Creating a static html site with multiple pages under root ala
/index.html
/foo.html
/bar.html

These pages all have unique content and layout and are not dynamic pages generated from any data source. But, per what I can find the docs, it does seem to suggest that I will need to create some sort of data-sourcing layer to have these pages created.
Answered by josh
Maybe I'm misunderstanding, but wouldn't app/index/page.ts, app/foo/page.ts and app/bar/page.ts solve this problem?
View full answer

26 Replies

Avatar
Maybe I'm misunderstanding, but wouldn't app/index/page.ts, app/foo/page.ts and app/bar/page.ts solve this problem?
Answer
Avatar
Dwarf HototOP
Maybe? I'll try it out. From reading, that pattern (using the new App Router) seemed to suggest that I would end up with /index.html, /foo/index.html and /bar/index.html
Avatar
Nope, you'll end up with index.html, foo.html and bar.html
I just ran a static export of one of my sites
Image
Image
Avatar
Dwarf HototOP
You're right, that did work. But now I have a question about why. This is the page I was reading that suggested the opposite behavior: https://nextjs.org/docs/app/building-your-application/routing/defining-routes
Since foo is a folder under app, I would've expected it to define a corresponding foo folder in the output.
Actually, I kinda do see this behavior if I run next dev.
Avatar
That page doesn't prescribe the output structure, only how it works route-wise

If you have foo/page.ts then /foo will show you the output of foo/page.ts
Avatar
Dwarf HototOP
I was just about to arrive at a similar conclusion.
it's about the router, not the generation
Avatar
yeah
the output differs between targets but the end result is the same, and that's what the doc is talking abuot
Avatar
Dwarf HototOP
does that mean that everything gets flattend with output: export?
Avatar
what do you mean? /foo/bar/xax/page.ts will resolve at /foo/bar/xax
Avatar
Dwarf HototOP
I was going to use NJS to build a chrome extension
static output, handles 'webpack', managed react components, etc...
but that means needing certain html files to be put in certain places and I'm wondering now how much I'll have to manually lift vs leveraging nextjs build rules.
Avatar
ah i have no idea about that sorry
Avatar
Dwarf HototOP
I thought the app router + export rules would mean that I could use the folder structure to layout my pages/content, but maybe not in the way I was thinking...
No worries, I appreciate the insights!
Avatar
np! you can mark a message as solution with Right click -> Apps -> Mark Solution
Avatar
Dwarf HototOP
After some testing, there's a bit of clarification on the answer.
Avatar
Dwarf HototOP
A page.ts file under a directory turns that directory into a route with the same name as the directory. i.e. under output: export, app/foo/page.ts becomes /foo.html and app/bar/page.ts becomes /bar.html like you might expect, but adding another intermediate directory like, app/bar/spam/page.ts becomes /bar/spam.html
So you can get multiple pages under one sub-directory with export by using dir/page.ts to identify your leafs while any intermediate directory (with and without a page.ts file) will become a sub-directory
Avatar
Sounds right to me