[RFC] `src` directory
This is now supported in the latest versions of Next.js!
Goals
- Allow users to have their pages in
src/pages
Background
Next.js has a special pages directory where every file becomes a separate route, following the convention over configuration approach this directory is required to be in the root of a Next.js application.
Over time there's been multiple issues asking for a configuration option to change pages to something else, generally with the argument of "I want to change it". However, over time in talking with users I've established a common pattern that what is generally the actual question is "I want to put my code in a src folder, how can I do that?".
When looking at various closed source codebases it was quite common to see next dev ./src or next build ./src to handle having pages in the src directory.
However in doing so you move the Next.js root to src instead of the main directory of the app, meaning that compilation (like Babel) won't be applied to files outside of src.
On top of this Next.js creates tsconfig.json and next-env.d.ts in the Next.js root, as the Next.js root is where your app lives.
There's a few reference issues #4789, #8415, #4315, #3921, #5303, #2202, #3416
Proposal
Add support for src to hold pages. There can only be one pages directory, so the user either has ./src/pages or ./pages, having both would throw an error. public (experimental) and static would still exist next to the src directory, so they wouldn't be inside of src. This would be similar to the Create React App enforced directory structure.
I'd love to see api folder inside src also. The problem is user might use different programming languages for api as zeit allows multiple builders.
The api directory is already inside of pages/api, so it'll be in src/pages/api.
Iād prefer if it was completely customizable. In my case I have a monorepo using yarn workspaces and Iād like for the pages directory to be inside my packages/landing, not the root of the monorepo.
That would be great! For instance, when running CLIs like eslint I often do eslint ./src. At the moment, I needed special handling for the pages directory. This proposal would fix this.
@brunolemos in that case your project structure seems off, Next.js should be installed and booted inside packages/landing.
I really like this approach as well, I feel like it will produce a more navigatable structure for new comers. Something to the effect of /server has all the custom server things and /src has the app specific code :) Great work @timneutkens I absolutely love this tool!
in that case your project structure seems off, Next.js should be installed and booted inside packages/landing.
Then I would lose the monorepo benefits, since I use code from packages/core inside my packages/landing. But ok, I can move Next.js to packages/landing and install packages/core from npm instead (or leave it as is, since it works).
@brunocascio you can use https://github.com/martpie/next-transpile-module to transpile core module.
Cool that this is coming! Is this about src only or will it be configurable? (For us, src is the name we use but I've been on the wrong side of the "convention over configuration" before š.)
This is awesome. Thank you @timneutkens. I think just covering src/pages will already fix the majority of "issues".
@borekb It won't be configurable, as this covers the majority use case š And allowing this to be configurable could cause misuse.
This is a major improvement, for sure; however:
This would be similar to the Create React App enforced directory structure.
Create-React-App allows for the developer to eject the app from its defaults. Following this line, I haven't heard a compelling argument why an unrestricted config option (where pages can be whatever you want) is off the table. There are plenty of ways a developer can misconfigure next if they so want, why draw the line here?
@SirensOfTitan i haven't heard a good argument to allow it, and by allowing it you open up a can of worms of weird use-cases that break in many ways, we've seen this happen with distDir many times and has caused quite a maintenance burden on our side (and developer frustration when things don't work as expected). The way it is currently we can guarantee things work through integration tests and continue to work correctly in the future.
Time to move back to next when this is ready, cheer
@jeremy-coleman I don't see how this relate to the RFC.
That is already the case. pages get compiled into .next.
There's no reason for doing so at this point.
That's not something we're pursuing and unrelated to this RFC.
I just started picking up Next, and I'm quite surprised that this issue is only 10 days old. I'd like to give some feedback, in case it might be useful:
-
I've explored the issue tracker a bit, and I think you guys are closing issues too aggressively, there shouldn't be a 10 days old issue about this, there should have been an issue about this somebody started years ago, where even if there was no plan to implement this at that time people could have still upvoted it via ":+1:"s.
-
It's great that Next is opinionated, up until the moment when I want to do something and I either simply can't do it or I have to jump through multiple unnecessary hoops to do it, just because somebody in the core team don't like my use case. Being opinionated is good, but flexibility is also good.
@fabiospampinato
I've explored the issue tracker a bit, and I think you guys are closing issues too aggressively,
I disagree. We reply on basically all issues, even closed ones. The ones that we do close are related to either not following the issue template (questions) or things that we won't pursue anytime in the future. You can still use š on closed issues and we regularly check that to course correct the roadmap if needed. This particular RFC is one of such. For reference we get hundreds of GitHub notifications per day and both me and @Timer are going through them all multiple times per day.
there shouldn't be a 10 days old issue about this, there should have been an issue about this somebody started years ago, where even if there was no plan to implement this at that time people could have still upvoted it via "š"s.
If we take this approach there would be a multitude of non-actionable open GitHub issues, making it unmanageable to find actionable ones and to prioritize those.
It's great that Next is opinionated, up until the moment when I want to do something and I either simply can't do it or I have to jump through multiple unnecessary hoops to do it, just because somebody in the core team don't like my use case. Being opinionated is good, but flexibility is also good.
The main goal for Next.js is to have the right defaults for building websites / web applications and giving flexibility between tradeoffs (eg static, ssr, etc) while putting you / your team into the pit of success. I recently did an interview that covered this a bit: https://infoq.com/news/2019/08/nextjs9-interview-neutkens/
I want to do something and I either simply can't do it or I have to jump through multiple unnecessary hoops to do it, just because somebody in the core team don't like my use case.
We're always happy to take feedback! š Feel free to reach out to me directly or create a feature request issue.