Pages usually share a common layout, like a shared header, sidebar, and footer. The +Layout setting lets you define such shared layout.
You can also define multiple layouts: some pages can share one layout, while other pages share a different one. For example, admin and marketing pages typically have different layouts.
The component defined by the +Layout setting wraps the component defined by +Page.
<Layout> ⟸ component defined by +Layout <Page /> ⟸ component defined by +Page</Layout>
To define a global layout applying to all your pages:
// /pages/+Layout.tsxexport { Layout }import React from 'react'// `children` includes the +Page component (and all inner +Layout components)function Layout({ children }: { children: React.ReactNode }) { return <> <Navigation/> <Content>{children}</Content> </>}function Navigation() { /* ... */ }function Content() { /* ... */ }
Don't forget to use children (or <slot> if you use Vue).
# Global outer layout that applies to all pagespages/+Layout.js# Inner layout nested into the global outer layout, for marketing pagespages/(marketing)/+Layout.js# Inner layout nested into the global outer layout, for admin pagespages/admin-panel/+Layout.js
Here pages/+Layout.js applies to all pages, including the marketing and admin pages.
The Layout setting is cumulative: pages/(marketing)/+Layout.js doesn't override pages/+Layout.js. Instead, the <Layout> components nest within each other:
pages/+Layout.js # Global layout (shared among all pages)pages/product/@id/+Layout.js # Outer content ("Macbook" ...)pages/product/@id/pricing/+Page.js # Inner content ("Pricing" ...)pages/product/@id/reviews/+Page.js # Inner content ("Reviews" ...)
If your nested layout isn't associated with a URL (if the pricing and reviews tabs share the same URL /product/42) then you can use a stateful component instead of <Layout>.
To avoid the page scrolling to the top, make sure to use:
See the Nested section above. For smooth nested layout navigation, we recommend using Client Routing. (Using Server Routing leads to full page reloads which usually isn't acceptable for same-page navigations.)