https://nextjs.org/blog/layouts-rfc Deploy Next.js in seconds - Skip to content Next.js Learn ShowcaseDocsBlogAnalyticsExamplesEnterprise Feedback Email [ ] Feedback [ ] emojiemojiemojiemoji Send Learn Layouts RFC Monday, May 23rd 2022 ( ago) []Tim NeutkensTim Neutkens Tim Neutkens@timneutkens []Sebastian MarkbageSebastian Markbage Sebastian Markbage@sebmarkbage []Delba de OliveiraDelba de Oliveira Delba de Oliveira@delba_oliveira []Lee RobinsonLee Robinson Lee Robinson@leeerob This RFC outlines the biggest update to Next.js since it was introduced in 2016: * Nested Layouts: Build complex applications with nested routes. * Designed for Server Components: Optimized for subtree navigation. * Improved Data Fetching: Fetch in layouts while avoiding waterfalls. * Using React 18 Features: Streaming, Transitions, and Suspense. * Client and Server Routing: Server-centric routing with SPA-like behavior. * 100% incrementally adoptable: No breaking changes so you can adopt gradually. * Advanced Routing Conventions: Offscreen stashing, instant transitions, and more. The new Next.js router will be built on top of the recently released React 18 features. We plan to introduce defaults and conventions to allow you to easily adopt these new features and take advantage of the benefits they unlock. Timeline This RFC will be divided into two parts: * Part 1 (This Post): Overview of the new routing system and how it integrates with React Server Components and Data Fetching. * Part 2 (Next Post): Advanced routing examples and conventions, and how Next.js will use Suspense behind the scenes for streaming and selective hydration. Motivation We've been gathering community feedback from GitHub, Discord, Reddit, and our developer survey about the current limitations of routing in Next.js. We've found that: * The developer experience of creating layouts can be improved. It should be easy to create layouts that can be nested, shared across routes, and have their state preserved on navigation. * Many Next.js applications are dashboards or consoles, which would benefit from more advanced routing solutions. While the current routing system has worked well since the beginning of Next.js, we want to make it easier for developers to build more performant and feature-rich web applications. As framework maintainers, we also want to build a routing system that is backwards compatible and aligns with the future of React. Terminology This RFC introduces new routing conventions and syntax. The terminology is based on React and standard web platform terms. Throughout the RFC, you'll see these terms linked back to their definitions below. * Tree: A convention for visualizing a hierarchical structure. For example, a component tree with parent and children components, a folder structure, etc. * Subtree Part of the tree, starting at the root (first) and ending at the leaf (last). [][yH5BAEAAAA][image] * URL Path: Part of the URL that comes after the domain. * URL Segment: Part of the URL path delimited by slashes. [][yH5BAEAAAA][image] How Routing Currently Works Today, Next.js uses the file system to map individual folders and files in the Pages directory to routes accessible through URLs. Each Page file exports a React Component and has an associated Route based on its file name. For example: [][yH5BAEAAAA][image] Next.js also supports Dynamic Routes (including catch all variations) with the [param].js, [...param].js and [[...param]].js conventions. * Layouts: Next.js offers support for simple component-based layouts, per-page layouts using a component property pattern, and a single global layout using a custom app. * Data Fetching: Next.js provides data fetching methods ( getStaticProps, getServerSideProps) which can be used at the page (route) level. These methods are used to determine if a page should be Statically Generated (getStaticProps) or Server-Side Rendered (getServerSideProps). In addition, you can use Incremental Static Regeneration (ISR) to create or update static pages after a site is built. * Rendering: Next.js provides three rendering options: Static Generation, Server-Side Rendering, and Client-Side Rendering. By default, pages are statically generated unless they have a blocking data fetching requirement (getServerSideProps). Introducing the App Folder To ensure these new improvements can be incrementally adopted and avoid breaking changes, we are proposing a new directory called app. [][yH5BAEAAAA][image] The app directory will work alongside the pages directory. For backwards compatibility, the behavior of the pages directory will remain the same and continue to be supported. You can incrementally move parts of your application to the new app directory to take advantage of the new features. Defining Routes You can use the folder hierarchy inside app to define routes. A route is a single path of nested folders, following the hierarchy from the root folder down to the final leaf folder. [][yH5BAEAAAA][image] For example, you can add a new /dashboard/settings route by nesting two new folders in the app directory. Note: + With this system, you'll use folders to define routes, and files to define UI (with new file conventions such as layout.js, page.js, and in the second part of the RFC loading.js). + This allows you to colocate your own project files (UI components, test files, stories, etc) inside the app directory. This is currently not possible in pages. Route Segments Each folder in the subtree represents a route segment. Each route segment is mapped to a corresponding segment in a URL path. [][yH5BAEAAAA][image] For example, the /dashboard/settings route is composed of 3 segments: * The / root segment * The dashboard segment * The settings segment Note: The name route segment was chosen to match the existing terminology around URL paths. Layouts & Creating UI New file convention: layout.js So far, we have used folders to define the routes of our application. But empty folders do not do anything by themselves. Let's discuss how you can define the UI that will render for these routes using new file conventions. A layout is UI that is shared between route segments in a subtree. Layouts do not affect URL paths and do not re-render (React state is preserved) when a user navigates between segments that share the same layout. A layout can be defined by default exporting a React component from a layout.js file. The component should accept a children prop which will be populated with the segments the layout is wrapping. There are 2 types of layouts: * Root layout: Applies to all routes * Regular layout: Applies to specific route segments You can nest two or more layouts together to form nested layouts. Root layout You can create a root layout that will apply to all routes of your application by adding a layout.js file inside the app folder. [][yH5BAEAAAA][image] Note: + The root layout replaces the need for a custom App (_app.js) and custom Document (_document.js) since it applies to all routes. + You'll be able to use the root layout to customize the initial document shell (e.g. and tags). + You'll be able to use data fetching methods inside the root layout (and other layouts). Regular layouts You can also create a layout that only applies to a part of your application by adding a layout.js file inside a specific folder. [][yH5BAEAAAA][image] For example, you can create a layout.js file inside the dashboard folder which will only apply to the route segments inside dashboard. Nesting layouts Layouts are nested by default. [][yH5BAEAAAA][image] For example, if we were to combine the two layouts above. The root layout (app/layout.js) would be applied to the dashboard layout, which would also apply to all route segments inside dashboard/*. [][yH5BAEAAAA][image] Pages New file convention: page.js A page is UI that is unique to a route segment and required for a route to be valid. You can create a page by adding a page.js file inside a folder. [][yH5BAEAAAA][image] For example, to create pages for the /dashboard/* routes, you can add a page.js file inside each folder. When a user visits /dashboard/ settings, Next.js will render the page.js file for the settings folder wrapped in any layouts that exist further up the subtree. [][yH5BAEAAAA][image] You can create a page.js file directly inside the dashboard folder to match the /dashboard route. The dashboard layout will also apply to this page: [][yH5BAEAAAA][image] This route is composed of 2 segments: * The / root segment * The dashboard segment Note: + For a route to be valid, it needs to have a page in its leaf segment. If it doesn't, the route will 404. + A page.js file should default export a React component. + The name needs to be page.js exactly. If you do not export a Page component, Next.js will throw an error. Layout and Page Behavior Recap: * Page components are the default export of page.js. * Layout components are the default export of layout.js. * Layout components must accept a children prop. When a layout component is rendered, the children prop will be populated with a child layout component (if it exists further down the subtree) or a page component. It may be easier to visualize it as a layout tree where the parent layout will pick the nearest child layout until it reaches a page. Basic Example: [][yH5BAEAAAA][image] // Root layout (app/layout.js) // - Applies to all routes export default function RootLayout({ children }) { return (
{children}