Logo
Services
Industries
Technologies
About
Case Study
Blog

Let's Build Your Tech Vision

120+ expert engineers & designers in AI, Web, Mobile.

General Inquiries

hr@mediusware.com+880 1897-661850-51

Discuss Services

sales@mediusware.com+880 1750-020408

Mediusware 2015 - 2026. All Rights Reserved

Quick Links

Our StoriesLeadershipBlogsContactCareerCSR Terms & ConditionsPrivacy Policy

Services

Software DevelopmentWeb DevelopmentMobile App DevelopmentE-commerce DevelopmentQA & DevOpsDigital MarketingArtificial IntelligenceBranding & Visualization

Technology

React.jsVue.jsAngular jsLaravelDjangoFlutterRustReact Native

Industries

RetailE-learningTravelHealthcareLogisticsEntertainmentFintechReal Estate
Blog/
  1. What is a Root Layout?
  2. Implementation of Multiple Root Layout
  3. Testing the Layouts
  4. Final Thoughts
ChatGPT
ChatGPT
Google AI Studio
Google AI Studio
Claude
Claude
Grok
Grok
Perplexity
Perplexity

Transform Your Next.js Experience: A Guide to Multiple Root Layouts in Next.js

  • Learn to implement multiple root layouts in Next.js for efficient app structure.

  • Use the new App Router in Next.js 14 to define and manage different layouts.

  • Create specific layouts for different sections of your app (e.g., dashboard, authentication).

  • Maintain a consistent user experience while optimizing your app’s scalability and modularity.

Transform Your Next.js Experience: A Guide to Multiple Root Layouts in Next.js image
Navigate
  1. What is a Root Layout?
  2. Implementation of Multiple Root Layout
  3. Testing the Layouts
  4. Final Thoughts

When building complex applications in Next.js, it's common to have different layouts for different sections of your app. With the introduction of the new App Router in Next.js 14, creating and managing multiple root layouts has become more flexible and intuitive. This guide will show you how to set up multiple root layouts in your Next.js application using the App Router.

What is a Root Layout?

A root layout in Next.js is a special layout that wraps around your page components, providing consistent UI elements like headers, footers, sidebars, or other shared elements. The App Router makes it easy to define these layouts in a file-based structure, ensuring that your layout logic is organized and easy to manage

Implementation of Multiple Root Layout

Step 1: Setting Up the Project

If you haven't already set up a Next.js project, you can do so by running the following command:

npx create-next-app@latest my-next-app
cd my-next-app

This will create a new Next.js project with the default configuration.

 

Step 2: Understanding the App Router Structure

Next.js 13 introduces the app/ directory as the new standard for building applications. In this directory, each route can have its own layout. You can create a folder structure to manage multiple root layouts based on the routes.

 

Here's an example of a typical structure when using the App Router with multiple layouts:

Fig: Folder Structure

 

  • dashboard/layout.js – Layout specific to the dashboard section.
  • auth/layout.js – Layout specific to the authentication section.
  • layout.js – The global root layout that wraps all other layouts.

 

Step 3: Creating Global Layout

The global root layout file provides a base layout that wraps around all pages in the application. To create this file, navigate to the app directory and add a layout.js file with the following code:

// app/layout.js
export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <body>
        <header>
          <h1>Global Header</h1>
        </header>
        {children}
        <footer>
          <p>Global Footer</p>
        </footer>
      </body>
    </html>
  );
}

This layout will be used across your entire application unless overridden by a specific layout in a sub-directory.

 

Step 4: Creating a Specific Layout for the Dashboard

Now, let's create a specific layout for the dashboard section of your app. Inside the app/dashboard/ directory, create a layout.js file with the following content:

// app/dashboard/layout.js
export default function DashboardLayout({ children }) {
  return (
    <div>
      <aside>
        <nav>
          <ul>
            <li>Dashboard Link 1</li>
            <li>Dashboard Link 2</li>
          </ul>
        </nav>
      </aside>
      <main>{children}</main>
    </div>
  );
}

This DashboardLayout will wrap any page under the /dashboard route, providing a specific layout for the dashboard section of your application.

 

Step 5: Creating a Layout for Authentication

Similarly, you might want a different layout for your authentication pages (login, signup). Create a layout.js file under the app/auth/ directory:

// app/auth/layout.js
export default function AuthLayout({ children }) {
  return (
    <div className="auth-layout">
      <header>
        <h2>Authentication Header</h2>
      </header>
      <main>{children}</main>
      <footer>
        <p>Auth Footer</p>
      </footer>
    </div>
  );
}

This layout will be used specifically for pages under the /auth route.

 

Step 6: Adding Page Components

Now that you've defined your layouts, you can create page components that will use these layouts automatically. For example, create a page.js file for the dashboard and authentication routes:

// app/dashboard/page.js
export default function DashboardPage() {
  return <h2>Welcome to the Dashboard</h2>;
}

// app/auth/page.js
export default function AuthPage() {
  return <h2>Login or Signup</h2>;
}

These pages will automatically use their respective layouts (DashboardLayout and AuthLayout) defined in their folders.

Testing the Layouts

Now, when you navigate to the /dashboard and /auth routes in your application, you will see that each section has its unique layout applied, while the global layout remains present for other pages, ensuring a consistent user experience. Below are screenshots of the output for both routes.

Fig: Dashboard Route

Fig: Dashboard Route

 

Fig: Auth Route

 

Final Thoughts

Using multiple root layouts in Next.js with the App Router is a powerful way to structure your application. It allows you to create distinct sections of your app, each with its own design and structure, while still maintaining a global layout. This approach makes your codebase more modular, easier to manage, and more scalable as your app grows.

 

Frequently Asked Questions

Root layouts in Next.js provide a consistent structure for your application, allowing you to define shared UI elements (like headers and footers) that wrap around your page components. This is particularly useful for maintaining a cohesive look across different sections of your app.

Author
By Mediusware Editorial Team

Content Team at Mediusware

We are the Mediusware Editorial Team, passionate about crafting insightful content on technology, software development, and industry trends. Our mission is to inform, inspire, and engage our audience with well-researched articles and thought leadership pieces. With a deep understanding of the tech landscape, we aim to be a trusted source of knowledge for professionals and enthusiasts alike.

Get the best of our content straight to your inbox!

By submitting, you agree to our privacy policy.

What is a Root Layout?
Implementation of Multiple Root Layout
Testing the Layouts
Final Thoughts
Navigate
What is a Root Layout?Implementation of Multiple Root LayoutTesting the LayoutsFinal Thoughts

Get the best of our content straight to your inbox!

By submitting, you agree to our privacy policy.

Featureblogs

01QA Outsourcing: Boost Software Quality and Speed02Is Golang Still the Right Choice in 2025?03Large Language Models in 2025: How They’re Changing the Way We Work and Build04How to Hire the Right Flutter Developer05Nearshore Staff Augmentation: Unlocking Global Talent

Authorblogs

01Application Software: Types, Benefits, and Future Trends02What Is Wireframing? A Practical Guide03ChatGPT vs. Gemini vs. Claude vs. Copilot vs. Perplexity: What Matters for Real Work042025 Global Recognition Award: Mediusware’s Leadership in Innovation and Impact05React Vulnerability: Critical Security Flaw in React Server Components