DEV Community

Cover image for Mastering Route Groups in NextJS 14
Shawon Saha
Shawon Saha

Posted on

Mastering Route Groups in NextJS 14

In the ever-evolving world of web development, organization and structure are key to building scalable and maintainable applications. With the release of NextJS 14, developers are introduced to a powerful feature called Route Groups, which offers a elegant solution for organizing routes and managing layouts within a NextJS project.

What are Route Groups?

Route Groups are a convention in NextJS 14 that allows you to group related routes together without affecting the URL path structure. This feature is particularly useful when you need to organize routes by site section, intent, or team, while maintaining a clean and logical URL hierarchy.

Creating Route Groups

To create a Route Group, you simply need to wrap the folder name with parentheses, like this: (folderName). For example, you could have a route group called (marketing) or (shop). Inside these folders, you can place your route files and nested layouts.

Benefits of Route Groups

  1. Organized Routes: Route Groups provide a way to keep related routes together, making it easier to navigate and maintain your codebase as your application grows.

  2. Nested Layouts: With Route Groups, you can create multiple nested layouts within the same route segment level. This includes the ability to have multiple root layouts, each with its own layout.js file and HTML/body structure.

  3. Layout Segregation: Route Groups allow you to opt specific routes into a shared layout by moving them into a dedicated group folder. Routes outside of this group will not share the same layout, providing greater flexibility in your application's UI and experience.

  4. Multiple Root Layouts: By removing the top-level layout.js file and adding individual layout.js files within each Route Group, you can create multiple root layouts. This is particularly useful when partitioning your application into sections with completely different UIs or experiences.

Practical Examples

  1. Organizing Routes without Affecting the URL Path: Let's say you have a website with sections for marketing and an online shop. You can create Route Groups like (marketing) and (shop) to keep related routes together, while the URL paths remain unaffected.

  2. Opting Specific Segments into a Layout: If you have routes like account and cart that should share a common layout, you can create a (shop) Route Group and move these routes inside. The checkout route, which doesn't share the same layout, can remain outside the group.

  3. Creating Multiple Root Layouts: To create multiple root layouts, remove the top-level layout.js file and add individual layout.js files within each Route Group (e.g., (marketing) and (shop)). This allows you to define separate HTML and body structures for different sections of your application.

Best Practices and Considerations

  • Route naming within Route Groups has no special significance other than organization. It does not affect the URL path.
  • Avoid resolving routes from different Route Groups to the same URL path, as this will cause an error.
  • If using multiple root layouts, your home page.js file should be defined within one of the Route Groups (e.g., app/(marketing)/page.js).
  • Navigating across multiple root layouts will trigger a full page load, as opposed to a client-side navigation.

By leveraging Route Groups in NextJS 14, you can achieve a higher level of organization and structure within your application, while maintaining clean URL paths and flexible layout management. Whether you're building a complex web application or a simple website, Route Groups provide a powerful tool to streamline your development process and enhance the maintainability of your codebase.

Top comments (2)

Collapse
 
ngdangtu profile image
Đăng Tú

I wonder if we can nest route groups, like this (parent-group) > (child-group) > page-a > page.tsx?

Collapse
 
shawon profile image
Shawon Saha • Edited

Yes, you can absolutely nest Route Groups in Next.js 14! Here's how it works:

app/
├── (parent-group)/
│   ├── layout.js
│   ├── (child-group-1)/
│   │   ├── layout.js
│   │   └── page-a/
│   │       └── page.tsx    // URL: /page-a
│   └── (child-group-2)/
│       ├── layout.js
│       └── page-b/
│           └── page.tsx    // URL: /page-b
Enter fullscreen mode Exit fullscreen mode

Key Points About Nested Route Groups

  • Independent Layouts: Each group can have its own layout.js file.
  • URL Structure: The URL structure is not affected by the route groups; they are purely organizational.
  • Cascading Layouts: Layouts cascade down through the nested groups.

Example Layouts

Consider the following layouts:

// app/(parent-group)/layout.tsx
export default function ParentLayout({ children }) {
  return (
    <div className="parent-wrapper">
      {children}
    </div>
  );
}

// app/(parent-group)/(child-group-1)/layout.tsx
export default function ChildLayout({ children }) {
  return (
    <div className="child-wrapper">
      {children}
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

When visiting /page-a, the layout hierarchy would be:

  1. ParentLayout
  2. ChildLayout
  3. Page Content

Use Cases for Nested Route Groups

Nested Route Groups are particularly useful for organizing routes that share common layouts but have specific layout requirements. Here are some common organizational strategies:

  • Organization by Feature:
app/
├── (auth)/
│   ├── (forms)/
│   │   ├── login/
│   │   └── register/
│   └── (recovery)/
│       ├── forgot-password/
│       └── reset-password/
Enter fullscreen mode Exit fullscreen mode
  • Organization by Access Level:
app/
├── (protected)/
│   ├── (admin)/
│   │   ├── users/
│   │   └── settings/
│   └── (user)/
│       ├── profile/
│       └── dashboard/
Enter fullscreen mode Exit fullscreen mode

Best Practices

While you can nest Route Groups as deeply as needed, it's best to keep the nesting reasonable to maintain code clarity. Too many nested groups might complicate navigation within your codebase.