If you've decided to use Next.js on your next project, setting up your layouts should be a key initial consideration to allow for flexibility in design and avoid any refactoring down the road.
Next.js offers some very powerful layout capabilities with the ability to create highly optimized server side rendering layouts with maximimum flexibility.
In this article I'll be covering some techniques to create modularized layouts for a variety of use cases, including:
How to create the most common layouts used in web pages today.
How to maixmize Server Side Rendering (SSR).
When and how to use client components.
How to use nested layouts effectively.
What are Next.js layouts?
Next.js layouts provide a cascading and dynamic ability to create diverse page layouts to compose full HTML page templates.
The framework offers the ability to maximize SSR at the component level to optimize page load times and improve security.
Why are Next.js layouts important?
In most cases, developers are collaborating with a UX design team and design requirements may not always fit into a single layout flow. For example:
Dashboard with a grid of cards with analytics.
Custom landing pages like 404 and error pages.
Full scale product promotional pages.
Blog and documentation pages.
Secured Admin pages based on a user's role and authentication status.
However, the most important aspects of Next.js layouts also include:
Fetching sensitive data on the server side rather than on the client.
Render secured menus and navigation without the need for any client logic.
Render a bulk of page layout content on the server or even statically using Static Site Generation (SSG).
Greatly improve page load times with SSR and caching.
Reduce Content Layout Shifting (CLS) with image and client content loading.
Types of layouts
Layouts should be easily interchangable and modular
and support a variety of use cases, rather than trying to make one layout do all the work.
Standard layout
Main content takes up full width.
Shows header and footer.
Also known as single column or default layout in most cases.
This will set an insertion point for any <script> tags needed in your app. You can also use the import JavaScript ESM approach too.
TSX
1importReactfrom"react";
2
3constScripts=()=>{
4return<>
5{/* Any 3rd party or local JavaScript Libraries here */}
6{/* i.e. <script src="/js/app.js"></script> */}
7</>
8};
9
10exportdefaultScripts;
Here are the rest of layouts. Some of them use some providers which I'll explain in the next section since it's important to define providers correctly in React Server Components (RSC).
6exportconstFullscreenLayout=({ children }:PropsWithChildren)=>{
7return(
8<RootLayout>
9<divclassName="h-screen">{children}</div>
10<Scripts/>
11</RootLayout>
12);
13};
Layout components
Here's the code for the components, <Header>, <Footer> and <Sidebar> components. These are just generic components and only to show sections of the layout.
Most third party providers you might already use are not React Server Component RSC ready. This means that they don't add the use client to indicate to run on the client.
Since layouts are RSC by default, they need to be placed in a separate module file and declared as client component using use client.
In order to use one of the layouts for a particular page route, we can use Next.js Route Groups to segment and group layouts.
Using the parenthesis (path) notation in the route path, tells Next.js to group the nested pages and routes into a group with the same layout.
By removing the /app/layout.tsx in the root of the app route directory, Route Groups will use the layout.tsx inside the Route Group directory as its root.
(auth): Used for pages like login.
(default): Use for general web pages.
(secured): Used for authenticated admin page.
Page Route Groups Section
Bash
1└── project-root
2 └── app
3 ├── (auth)
4 │ ├── login
5 │ │ └── page.tsx
6 │ └── layout.tsx
7 ├── (default)
8 │ ├── home
9 │ │ └── page.tsx
10 │ ├── about
11 │ │ └── page.tsx
12 │ └── layout.tsx
13 └── (secured)
14 ├── admin
15 │ └── page.tsx
16 └── layout.tsx
These are just examples used in this tutorial but you can group pages and layouts in anyway you need.
The main idea is the pattern for applying layouts using the Route Group notation.
Using the (default) route group layout
Here's an example of how to use the DefaultLayout defined earlier.
TSX
1importReact,{PropsWithChildren}from'react';
2import{DefaultLayout}from'@/layouts/default';
3
4exportdefaultfunctionLayout({ children }:PropsWithChildren){
It's important to start thinking from a server side first perspective when creating layouts and components. This will maximize how much code is rendered on the server and improve performance.
Here's a couple of guidelines:
Isolate client component logic
One thing to note here is that all the pages so far are RSC. Since the metadata object can only be exported from server component, it's best to keep page.tsx files as server components if possible.
Since most client UI components will want to use React hooks, you'll want to extract the client code into separate components to isolate the client logic.
This way the client component can contain all of the hooks and interactivity and the page.tsx can render as much on the server as possible.
Try to put as much static content inside of server components.
These are just a few techniques but the benefit of leveraging layouts and other server components to do a bulk of the processing allows client components to focus on the presentation tier.
In Conclusion
So hopefully you'll have a better grasp on what's possible with the latest versions of the Next.js framework regarding layouts.
Next.js definitely makes it easy to create flexible and modularized layouts to meet any design requirements.
These are some of the layout strategies I use on my own projects and I've really streamlined things to make it as light weight and modular as possible.
Creating layouts should be easy and fast. Hope you enjoyed.