11 ms·
Next.js Layouts RFC: Nested routes and layouts, designed for Server Components
- hermanradtke 5y agoI am not a fan of the “directory structure creating an implicit router” convention of next.js. I wish I would define a handler in code instead.
- cyral 5y agoIt gets so ugly as soon as you have dynamic routes like `[...postName].ts`
- andrewingram 5y agoI never liked it either. At my last job I actually had a system which generated the Next.is pages folder based on a route config. The irony being that at build time, Next would then turn this folder into an internal route config that probably looked quite a lot like mine.
- aj0strow 5y agoOh interesting. Why not? Directory and file based routing is my favorite feature of Next.js, Remix, and other frameworks that do it.
- kilburn 5y agoIt makes route localization a second-class citizen in the framework at best, or a totally unsupported hack-your-own-solution thing at worst.
- julianbuse 5y agoif it helps, you could use middleware in next.js to handle routing to separate folders easier.
- kilburn 5y agoMiddleware won't work if you use "next export" to get a truly static site though, which is by far my preferred mode of operation whenever possible. This is what I meant when I said "second-class citizen at best".
- julianbuse 4y agoah I see. Maybe try the route rewriting in next.conf.js? I think thats what the file is. That might work for static sites.
- aj0strow 4y agoIt seems like a path prefix like "/en/books" or "/es/books" could be supported with a base url config. Localized path segments like "/books" or "/libros" could be supported with a path segment mapping configuration. It should work fine in theory. I guess those configs don't exist yet. Or is there something else that route localization requires?
- Zealotux 5y agoI don't like this approach either as maintenance gets complex over time. Changing routes in my React-router project is one file, one commit that's easy to verify, try changing a dozen routes with Next and you end up with a dozen of files moved around, with folder structure possibly impacted as well, I see this as an anti-pattern.
- aj0strow 4y agoThat's true, the pull request diff viewer does not make viewing file renaming very clear and there could be conflicts. On the other hand, what I see in practice is adding or removing context providers and layout components in the react router file, it becomes huge, and all the diffs change all of the lines with new indentation.
- a_chris 5y agoComing from a React Router background, the Nextjs file structure is a bless to work with
- gherkinnn 5y agoI've worked with both. There are days when I prefer a programmatic approach, and days I love the file-based router. With the introduction of routing middleware [0], Next.js finally offers anything I'd want to build. An option would be to add a catch-all route [1] at the root and implement your own code-based routing. You can still define the desired paths using getStaticPaths [2] but my guess is that some functionality like route-based code splitting will stop working. 0 - https://nextjs.org/docs/advanced-features/middleware https://nextjs.org/docs/advanced-features/middleware 1 - https://nextjs.org/docs/routing/dynamic-routes#catch-all-routes https://nextjs.org/docs/routing/dynamic-routes#catch-all-rou... 2 - https://nextjs.org/docs/basic-features/data-fetching/get-static-paths https://nextjs.org/docs/basic-features/data-fetching/get-sta...
- wdb 5y agoI prefer the directory structure approach as it makes it a lot easier to find the code related to a route. In other approach you can spend a lot of time finding the right files. The directory structure based routing really makes things a lot more convenient. I can imagine it's annoying when you move routes/urls a lot
- amcsi 5y agoEvery time I hear any news about routing in a React framework I always get disappointed that the news isn't that they're introducing type-safe (TypeScript) routes to enable type-safe linking.
- onion2k 5y agoI'm curious about what you mean. There's no way to embed type information into a URL - every GET parameter is a string. Are you suggesting that visiting a link like /123 would go to the 'string: "123"' route, while clicking a <Link to={123}> would route you to 'number: "123"'? That would make deep linking in an app really hard to manage. I'm struggling to understand why you'd want to overload routes like that.
- kall 5y agoI'm pretty sure what they are looking for is that something like a Link that takes a route name and a params object that are both typed.
- gherkinnn 5y agoGenerating a set of links from the routing as a union of strings as type Route. And a <Link /> component that only accepts an href of that type. Like so: https://www.typescriptlang.org/play?jsx=4#code/C4TwDgpgBASg9gV2NAvAWAFBSgHygIgHp9Ns8iATAQwGcALAIzioCcKStcDDr6nWKhKhQC2ASwB2HMt16NmbQnABuEFsrEQA7tK4ADHrXkCeEYFTEAbQgBIA3hIQiGagL57S+w3wWCKZi2t7R2c3QhE4FggPTnJCGjNgSQBzGg5MAGM4CRpgKCoALlhEZCgUWSN+RX9zK0IARgAmAGYOLJy8hiL4JFQKnxMawIaW8MiIdIx23KgAGUkAazKoAAo7DLorCiiJABooOiiAM1ci9c3LbYgJbogqDOAAOTh-fcOII+6SiFcASlv7k8XqgAHyrTwAHioB2OKDs7xOIPOWx2rghQhBmF+QA https://www.typescriptlang.org/play?jsx=4#code/C4TwDgpgBASg9... TS' string literal types are powerful. https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types https://www.typescriptlang.org/docs/handbook/2/everyday-type... edit: I did implement this a while back but found it gave me only so much additional value. Though I can imagine that large applications with dozens of developers might indeed benefit from type-safe routes to avoid dead links.
- onion2k 5y ago
- leerob 5y agoLee from Vercel, happy to answer questions about the RFC or future plans for Next.js. This RFC has been years in the making – and I'm pretty happy with where we've landed.
- saeedjabbar 5y agoExcited to try this out in my next project :)
- leerob 5y agoOpen to any feedback you have – we've already started work on the implementation, and will adjust as necessary based on the RFC discussion.
- codefined 5y agoNot seeing it in the article, but how are you handling `index` routing in the `app` folder? If you are putting it in the top directory (e.g. `/app/index.js`), is it not able to get a custom `layout.js` file (as that position is used for the global variant)? If it's in an `index` folder (e.g. `/app/index/page.js`), how would one create a route for `example.com/index`?
- leerob 5y agoThe current working idea is that you wouldn't use index.js. There are Layout Components and Page Components, both explicitly defined by `layout.js` and `page.js`. We are open to other opinions here, though. Others have shared similar sentiment: https://github.com/vercel/next.js/discussions/37136 https://github.com/vercel/next.js/discussions/37136
- realce 5y agoAm I correct thinking this will help create a simpler integration with - just hypothetically - Prisma calls that are dependent on app auth states in a single page? Replacing auth providers and moving them into Layouts? Daily user, very excited!
- austinkhale 5y agoWe're irrationally excited about this internally and we're super happy with how the Vercel team has continued to push Next.js forward. Thank you! We're using a version of the persistent layout pattern Adam Wathan [blogged about](https://adamwathan.me/2019/10/17/persistent-layout-patterns-in-nextjs/ https://adamwathan.me/2019/10/17/persistent-layout-patterns-...) but having first-class support for this paradigm will be awesome.
- leerob 5y agoThank you! We've added documentation[1] on the current layouts solutions, which is similar to Adam's post. It's a good workaround, but I'm very excited for improving the developer experience here. [1]: https://nextjs.org/docs/basic-features/layouts https://nextjs.org/docs/basic-features/layouts
- dalmo3 5y agoThere's a fundamental flaw with the solution proposed in the documentation (getLayout as a function), which could be fixed by using a pure JSX approach: https://github.com/vercel/next.js/issues/36029 https://github.com/vercel/next.js/issues/36029
- nullcipher 5y ago"irrationally excited" - what does this phrase mean? that there is no basis for your excitement? sounds so cheesy
- bobkazamakis 5y agoexcited beyond reason? try lactaid.
- sdmike1 5y agobeing more excited that you can readily account for
- vgrafe 4y agoI love this approach and use it, but just realized it does not prevent Layout components from re-rendering upon navigation... have you found a fix for this?
- gedy 5y agoLooks like a nice improvement, looking forward to trying this out. Nested layouts was the one stumbling block we had with next and felt very odd that we had to invent our own TabPane component to handle this pretty common use case.
- michaelsbradley 5y agoWill there ever be a fix or blessed workaround re: #32216? https://github.com/vercel/next.js/discussions/32216 https://github.com/vercel/next.js/discussions/32216 previously: https://github.com/vercel/next.js/issues/2581 https://github.com/vercel/next.js/issues/2581
- reilly3000 5y agoCould a smart person please compare and contrast this RFC and the ideas of remix.run?
- pier25 5y agoAccording to Ryan Florence (Remix's creator) they are "ripping features straight from Remix". https://twitter.com/ryanflorence/status/1528859776930545665 https://twitter.com/ryanflorence/status/1528859776930545665
- Vinnl 5y agoCredit's been added: https://twitter.com/leeerob/status/1528887380681588736 https://twitter.com/leeerob/status/1528887380681588736
- madjam002 5y agoSo we need to give credit now to every little bit of inspiration we get when coming up with new features and ideas? Not really sure what the problem is here.
- pier25 5y agoYou'd have to ask Ryan :) IMO it's really just a matter of time before good ideas end up in other projects. It's inevitable. Obviously everyone wants their framework to be as good and popular as possible.
- zdragnar 5y agoIt's really not an especially novel idea either- there's not really a good reason for remix itself to be getting the credit here.
- dimgl 5y agoI definitely understand the reaction. This is an existential threat to Remix. However, this is why I don't use Twitter. Remix is not just "an open-source project", it's a full-fledged company. This is definitely not the time (or the forum) to lay your cards out on the table...
- ElijahLynn 5y agoNext.js Layouts RFC Discussion is Here > https://github.com/vercel/next.js/discussions/37136 https://github.com/vercel/next.js/discussions/37136
- ushakov 5y agolooks similar to what Nuxt 3 has https://v3.nuxtjs.org https://v3.nuxtjs.org
- freedomben 5y agoI'm excited to see the wheels turning, but it feels a bit like the best feature of Next.js IMHO (SSG) is going to get more difficult, or possibly become unusable. A deprecation feels incoming. It could be confirmation bias as I've been a little worried about that for awhile since people using SSG aren't as likely to buy cloud services, so there's some incentive misalignment there, but ever since the Image component was only supported with SSR it's felt like SSG was going to either become less usable or disappear. Am I the only person who loves that feature? It really revolutionized how I approach web dev and enlarged my imagination of what was possible, so there's definitely some emotional attachment for me :-) Am I correct about SSG's future?
- dimgl 5y agoNaw, I don't agree. SSG is a killer feature. The fact that Next.js is able to do both SSG and SSR means that it's positioned to be a wholesale replacement for most React tech stacks. You can still have a "CSR-like" app with SSG, while having all of the Next.js conventions (such routing, layouts, etc.). Additionally, it's actually pretty useful for sites that need to be heavily SEO-optimized. Next.js is an impressive framework.
- leerob 5y agoStatic isn't going anywhere. In my opinion, one the best parts about Next.js is you can blend static with dynamic, on a per-page basis. Your landing page can be completely static + periodically updated in the background (i.e. ISR) and your /dashboard page could be a fully server-rendered application behind auth. You can do both without ejecting from the framework and being able to update and receive performance/security improvements. We've called Next.js a "hybrid framework" because of this, but I don't have a great name for it.
- kilburn 5y agoI suspect the fear was about "next export" going away, not the SSG functionality within a next.js server-side app.
- rglover 5y ago
- hu3 5y agoPlease don't do this. You're doing a disfavour to all the hard work you've put into Joystick framework. Keep up the effort and try to be positive.
- rglover 5y agoI’m remarkably positive. Just trying to ruffle some feathers but unfortunately my sense of humor falls on deaf ears here.
- seattle_spring 5y agoNext.js looks so, so much more mature than Joystick, the latter appearing to be one person's pet project. I don't understand why you shared it in this thread, and i really don't understand the "don't do drugs" bit.
- rglover 5y agoYes, it’s been around for several years. I’ve been working on Joystick for a year. The “don’t take drugs” is a joke.
- gitgud 5y agoWhat amazing news! Nextjs is fun to use, but the pages concept has felt a little clunky. Nuxtjs (vue's version) has a great pages abstraction, but it doesn't nest layouts, like this proposal. There's some obvious inspiration from other frameworks, as Remix and Angular 2.x have had nested routing for years. Hopefully this will allow for less repetition in pages and a more elegant structure to projects.
- szines 5y agoExactly. Remix creators wrote in a blog post that they inspired by Ember.js. It looks to me people reinvent the wheel all the time. Luckily Ember.js provides all these functionalities for a decade now and just getting better.
- ttymck 5y agoI've stayed at the periphery of front end tech. I am comfortable with react, but often eschew it for the impedance mismatch with the server. With the rise of server side front end practices, it dawns on me that there may be opportunities for integration with other server side runtimes? Asked naively, is there any possibility of an "FFI" integration for Next APIs? Anything more sophisticated than running a subprocess javascript VM?
- httgp 5y agoThis might probably happen a few years down the line. But I’m curious, is there a compelling use-case for this?
- 29athrowaway 5y agoSell me next.js in a tweet sized paragraph. Why should I use next.js?
- SOLAR_FIELDS 5y agoEasy SEO-optimized and performance optimized pages in a small package.
- gitgud 5y agoQuickly develop websites in React. Build statically generated pages or dynamically generated server-rendered pages, and deploy them almost anywhere.
- nsonha 5y ago"try to use react bare bone"
- code_runner 5y agoWhen learning next recently I was immediately wondering how to do accomplish certain routing patterns that I had used in a previous project… routing to a specific tab of a modal that pops over a particular page… Luckily that level of deep linking hasn’t been asked for, but this sounds really promising for adding some flexibility
- httgp 5y ago> With this RFC, you can start using React features I’m guessing this should say React 18 instead?
- leerob 5y agoFixed!
- fabian2k 5y agoPotentially pretty stupid question, I've never used any React framework like Next.js but I have used React for a while. Are these frameworks always meant to provide the backend as well? I'm using .NET as the backend, so I'm curious if they are something I should look closer at. I mean for SSR they have to run on the backend, but even that I could imagine running parallel to the main backend. I'm just not sure if you get much benefits out of these frameworks without the tight integration on the backend. Are next.js and other similar React frameworks worth looking at if you don't use node.js on the backend?
- fulafel 5y agoNo, there are many frontend only frameworks.
- gempir 5y agoYou need some form of backend if you want Server Side Rendering, and SSR is why most people opt-in to a React framework. And then you already have a backend so it's an easy jump to just using Nodejs on your backend as well. But a lot of people also just use NextJS as is and never build any server side code and just run their own API beside it. Or use other frameworks that don't include any SSR features, such as Create-React-App
- EnKopVand 5y ago> Are next.js and other similar React frameworks worth looking at if you don't use node.js on the backend? It depends on why you’re doing so. We’ve recently moved from a C# .net backend to a TypeScript node backend and in such a case it (well obviously) makes perfect sense to utilise next.js. In other cases where you split your front end and backend quite clearly, like using .net APIs and a JS front end it can make sense to use some degree of nextjs if you need to integrate some compiled parts into your react, maybe you need a fast loading and/or front page then it can be valuable to mix it. If you don’t know why you’d need it, you probably don’t, however.
- amcsi 5y agoI've heard people suggest to always use Next.js, because the alternative (Creat-React-App) is being developed too slowly.
- gliiics 5y agoI like the way this works, it feels inspired by SvelteKit's __layout.svelte files and I think it's just very intuitive to work with. Edit: welp, seems i glanced over too quickly, it's literally written there: > The layout.js file convention was inspired by the work done in SvelteKit
- nsonha 5y agowhy do I remember that nextjs always has had layout. Only worked with it briefly 2 years ago.
- mcintyre1994 5y agoIt’s always (or at least for a long time) had a layout, where you can define components that your page fits into. It’s really nice for putting a login status in a navbar, or something like toast notifications, because the layout components don’t get replaced when you navigate client-side. But it’s not nested currently, you only get one layout - so if you want some pages to have a sidebar or something then you’re not getting the benefit of the layout system for that.
- ostenning 5y agoFrameworks like nextjs are in an eternal existential battle to stay relevant. In my 4 years of using Next, I experienced a large number (6 or 7 releases) of major version releases. What the industry needs is not more breaking changes, but solidified frameworks that companies can rely on. Every major release adds another wave of technical debt as things become deprecated. I appreciate updates, but to me this particular proposal shows a lack of long term vision for the framework, attempting to throw in all the exciting bells and whistles from competing frameworks. This philosophy of “lets stay relevant” will ultimately cost businesses.
- deleted 5y ago[deleted]
- leerob 5y agoWe've been very intentional about incremental adoption and reducing the number of breaking changes, as well as giving folks codemods when they upgrade and providing escape hatches. For example, when upgrading to Next.js 12, which turned on SWC by default, we had already been testing an experimental version for many months which folks could try out. Then with 12, you could opt-out in the config if you didn't want to use it by default.
- freedomben 5y agoMy comment is probably not super helpful, but after several years in javascript land I found myself exhausted by the churn and constant rapid change. I've found myself really happy to be using the PETAL stack (Phoenix Elixir Tailwind Alpine LiveView) where the pace is a lot slower. There are now similar options in Rails (Hotwire) and Laravel (LiveWire), and probably others. If you haven't heard of those, it's definitely worth a look. The only situations in which I don't use them now are when there's a need for rich offline functionality. I still have several static sites in Next.js that I'll leave in place but I've been a lot happier since shifting my bulk to PETAL. (I'm also close to replacing my last big WordPress site with an Elixir one!! Knock on wood.)
- GeertJohan 5y agoNext.js is very nice. One thing that always strikes a nerve with me though is that it goes against 12 factor. There is no proper way to build once, deploy (with config) multiple times. So either you have to build for each environment, which on kubernetes means building different container images (one for each environment). Or do a full re-build when the container starts, which means slower container startup, but at least you can re-use the container across environments and properly inline environment variables. There's a discussion about this; https://github.com/vercel/next.js/discussions/24010 https://github.com/vercel/next.js/discussions/24010
- amcsi 5y agoNearly all front end metaframeworks/starters are like this, which is why I've given up on trying to run them in Docker, because it's just over-complicating the deploy process with no real benefit due to what you mentioned, and I'd rather just use simpler tools like Netlify.
- snorremd 5y agoPretty much. I've been able to use some regex-replacements on the build output in the past with varying luck. But it definitely feels like a hack and can break in weird ways. Another option is building all environments into a single container image and then selecting the right build runtime using an environment variable. This makes the build costlier, even if you can build in parallell, and a bigger image takes longer to distribute into your environment. At that point dedicated frontend hosting infrastructures like Netlify, Vercel, Cloudflare Pages and others seems like a better choice.
- andrewingram 5y agoThe fact that Vercel can do deploys to different environments almost instantly, suggests to me that internally they use some variant of the solution suggested in the first reply in that discussion.
- aemre 5y agoThere will be too many `page.js`, `layout.js` in the editor at the same time, which can be confusing.
- deleted 5y ago[deleted]
- bennettdams 5y agoI can recommend giving your component name (not file name) some identifier (like an underscore), so using VS Code's "Symbols" via command palette makes it easy to find and open them. Screenshots: https://twitter.com/BennettDams/status/1529051702351085568 https://twitter.com/BennettDams/status/1529051702351085568
- gabrieledarrigo 4y agoI'm totally not sold about this. Next.js routing was wrong from the start, and now it introduces a tons of new concepts that will inevitably require the rewriting of entire apps in a far future, since it will be "the new way to do routing in Next.js". What I really don't like is the coupling between the filesystem hierarchy and the actual routes, that is difficult to reason about when you have non english routes names with multiple route parameters. I mean, the routing problem for server side applications was solved a lot of time ago: a simple regex for the route definition, and the related handler that will responde to that route. I never understood why Next.js guys went with this inconvenient approach of the file system routing.
- likortera 4y agoDoes this affect in any way the `api` routes? Could these also be moved under the new `app` folder?