-
Describe the feature you'd like to requestRight now, it's difficult to share state across only some routes. There's a very "all or nothing" approach to sharing state with just React's I'm proposing that there's some kind of "wrapper" component for groups of routes in folders, as seen below. Describe the solution you'd likeI'm proposing a folder structure similar to this example: If this could exist in Next.js, where As a developer, I would expect the A solution like this would make for almost a "microsite" type of structure across folders in an app, allowing developers to intuitively make shared layouts and state and have a solid separation of application "sections". Describe alternatives you've consideredThere are some escape hatches built in now for this that aren't very intuitive. The easiest solutions are using swr and react-query, but it would be nice to have a more "vanilla React" way of doing it. There is something similar to this in Nuxt.js for a potential API alternative, as well! |
Beta Was this translation helpful? Give feedback.
Replies: 27 comments 11 replies
-
|
Would be cool if that wrapper could also allow for rendering stuff for all routes. For exemple rendering a specific header above every route in a folder without having to include it in every single route or a global navbar (and so on). |
Beta Was this translation helpful? Give feedback.
-
Yes @antoinegag! That's what I meant by "set up a layout from there" exactly. Being able to do it this way would reduce the need to use external libraries like this one to make easy layout groups. |
Beta Was this translation helpful? Give feedback.
-
|
I really like the idea of having a built-in mechanism like this one, it's something I've had to re-create in a couple of different Next apps so far. I wonder whether tying this to directory tree/routing might be overly restrictive, though. I could see people wanting to specify shared wrapper/layouts with more granularity than what this would allow (i.e. on a page-by-page basis). Maybe a named export on the page could be used to reference a wrapper/layout instead? I guess that would make it more annoying to have the same wrapper used on all pages, so maybe that is just a different proposal with a different set of use cases :) |
Beta Was this translation helpful? Give feedback.
-
|
Would the wrappers in nested _index.js files override or ”merge” with their top-level wrappers? E.g. if the |
Beta Was this translation helpful? Give feedback.
-
|
This is an interesting proposal! My initial reaction was "ugghh, that sounds complicated". I've yet to fully grasp the difference between Though on second though, I'm growing to like it! Especially the Layout part of it, which allows for much easier nested layouts compared to the getLayout pattern. @apkoponen To enable both nested layouts and deep-nested paths that don't share layout, I'd imagine this to be configurable. Maybe even by importing import ExampleWrapper2 from "../_index"
export default function ExampleWrapper3(props) {
return (
<ExampleWrapper2>
<h1>This is a header</>
{props.children}
</ExampleWrapper2>
}Some more questions:
|
Beta Was this translation helpful? Give feedback.
-
|
You all might want to look into SvelteKit's nested layouts for inspiration which sounds like it is exactly this. https://kit.svelte.dev/docs#layouts-nested-layouts EDIT: As mentioned on Twitter there's also a Nested Pages feature in Nuxt that can inspire. |
Beta Was this translation helpful? Give feedback.
-
|
I have a These pages all share a common layout that isn’t used anywhere else. It would be awesome to have an At first, to solve this, I conditionally rendered the This proposal would be a much more elegant solution. I’d prefer a name other than |
Beta Was this translation helpful? Give feedback.
-
|
A more “vanilla React" approach would be awesome. |
Beta Was this translation helpful? Give feedback.
-
|
I definitely think we need a more first-class solution to nested layout. I think tying it to the folder structure would be limiting or at least be a paradigm where it forces devs to structure the paths in their app in a way that allows the sharing of state. For example, if someapp.com/home and someapp.com/data/1 share the same layout or state, I’m not sure how the proposed solution would resolve the issue. An alternative API might be similar to the “Bonus” solution outlined in Adam Wathan’s Persistant Layouts article. I could see 2 functions:
The combination of these functions can be used to provide shared UI and shared state in any route; however, at that point maybe you’d only need getLayout since you could put providers there anyway. |
Beta Was this translation helpful? Give feedback.
-
|
This would be extremely helpful in building or using component libraries with next.js 🚀 |
Beta Was this translation helpful? Give feedback.
-
|
Very interested and needy solution. Having constant layout patterns across a folder is very much needed for my application too. |
Beta Was this translation helpful? Give feedback.
-
|
I would really like to see this in next! We currently attach a It would be great to have the ability to control whether nested layouts overrode or composed their parent layouts. |
Beta Was this translation helpful? Give feedback.
-
|
It was amazing literally 5 minutes into converting a react app to next I had this epiphany and thought surely this is supported. I imaged it worked just like |
Beta Was this translation helpful? Give feedback.
-
|
This solution would be very useful for building something like this: |
Beta Was this translation helpful? Give feedback.
-
|
Related: I just published documentation on how you can currently handle layouts with Next.js. To be clear, this is documenting what already exists, not solving this proposal. Thank you for this proposal! We are continuing to investigate built-in layouts support 😄 |
Beta Was this translation helpful? Give feedback.
-
|
I love this idea, I've often thought of the FWIW it looks like Gatsby 4 has caught up to this idea too: |
Beta Was this translation helpful? Give feedback.
-
|
For inspiration purposes only I post my Gatsby Plugin, where you can add layouts – or all kinds of wrappers, even nested wrappers. In the README file there are a lot of details. https://github.com/tujoworker/gatsby-plugin-wrap-pages I works even inside your own plugin. Which makes it a very interesting to use for micro frontends. But native |
Beta Was this translation helpful? Give feedback.
-
|
The release of middleware shows how this nested-file approach can work quite well! Personally, I'd love to see nested The nested Update: I've created |
Beta Was this translation helpful? Give feedback.
-
|
|
Beta Was this translation helpful? Give feedback.
-
|
Passing the path to page component being rendered by // dynamic-layout.tsx
import React from "react";
import ExampleLayout1 from "../components/ExampleLayout1";
import ExampleLayout2 from "../components/ExampleLayout2";
import DefaultLayout from "../components/DefaultLayout";
const DynamicLayout: React.FC<{ componentPath: string }> = ({
componentPath,
children,
}) => {
if (componentPath.includes("layout1")) {
return <ExampleLayout1>{children}</ExampleLayout1>;
}
if (componentPath.includes("layout2")) {
return <ExampleLayout2>{children}</ExampleLayout2>;
}
return <DefaultLayout>{children}</DefaultLayout>;
};
export default DynamicLayout;// _app.tsx
import { AppProps } from "next/app";
import DynamicLayout from "../dynamic-layout";
function MyApp({ Component, pageProps, __N_PAGE_PATH }: AppProps,) {
return (
<DynamicLayout componentPath={__N_PAGE_PATH }>
<Component {...pageProps} />
</DynamicLayout>
);
}
export default MyApp;Will this work in your case? |
Beta Was this translation helpful? Give feedback.
-
|
I've solved this with It automatically loads // pages/_app.jsx
const filename = preval`module.exports = __filename`;
const withLayoutLoader = codegen.require("@ceteio/next-layout-loader", filename);
// The magic lives here!
export default withLayoutLoader(({ Component, pageProps }) => (
<Component {...pageProps} />
));// pages/dashboard/_layout.jsx
export default function Layout({ children }) {
return (
<div style={{ backgroundColor: "#e2e8f0", padding: "1rem" }}>
{children}
</div>
);
}
// To hide this layout component from the router / build pipeline
export const getStaticProps = async () => ({ notFound: true });// pages/dashboard/index.jsx
import NextLink from "next/link";
export default function User() {
return (
<div style={{ backgroundColor: "white" }}>
<NextLink href="/">Home</NextLink>
</div>
);
}
export async function getStaticProps() {
return {
// props are also passed to _layout components
props: {
greeting: "Hello world"
}
};
}Check it out:
|
Beta Was this translation helpful? Give feedback.
-
|
This is functionality I would very much appreciate I tried out sveltekit recently, and this one of the bigger niceties of sveltekit that I am missing in nextjs. |
Beta Was this translation helpful? Give feedback.
-
|
What are the alternatives now |
Beta Was this translation helpful? Give feedback.
-
|
https://twitter.com/leeerob/status/1521713571377287171?s=20&t=3ANpHD6sbd2n05psZjmbow |
Beta Was this translation helpful? Give feedback.
-
|
Hey everyone! Excited to share we are actively working on a solution for nested routing and better layouts support. Expect an RFC very soon! 🥳 |
Beta Was this translation helpful? Give feedback.
-
|
Hey folks, the RFC has been shared!! 🥳 |
Beta Was this translation helpful? Give feedback.
-
|
Update: We’re excited to announce Next.js 13, which includes the new Inside the |
Beta Was this translation helpful? Give feedback.



Update: We’re excited to announce Next.js 13, which includes the new
app/directory (beta) with support for nested layouts, colocation of data fetching, streaming, Server Components by default, and much more.Inside the
app/directory, there is a powerful new way to fetch data with React’suse()hook and the extended Webfetch()API. This allows you to colocate data fetching directly inside components for the most flexibility, including inside layouts.