Thuta Learning
Next.js
IntermediateWeb DevelopmentIntermediate

Route Groups and Nested Layouts

Next.jsLesson 925 min

What you'll walk away with

  • Know that a (group) folder never appears in the URL and predict URLs from file paths
  • Add a layout.tsx per group to separate the public site from the app UI
  • Explain how layouts nest: root layout, then group layout, then page
  • Avoid duplicate-URL conflicts and the full page load caused by multiple root layouts

Explain it simply

Route groups in plain language

A library may shelve books in a 'children' section and a 'reference' section, yet each book keeps its own title. Route groups work the same way: folders in parentheses such as (marketing) and (dashboard) sort your code onto separate shelves without touching the URL at all.

The main reason to shelve routes this way is layouts. Put a layout.tsx inside each group and the marketing pages get a promotional header while the dashboard pages get a sidebar, with neither leaking into the other.

Problem → Why → Solution

The problem: one layout carrying two sites

Myanmar Notes has public pages (/, /about) and logged-in pages (/notes, /profile). Before learning about route groups, a developer used usePathname inside the single app/layout.tsx to choose between a sidebar and a header. That turned the whole root layout into "use client", and the list of URL prefixes has to be edited by hand every time a page is added.

app/layout.tsx
"use client"; // the whole root layout now ships to the browser
import { usePathname } from "next/navigation";

export default function RootLayout({ children }: { children: React.ReactNode }) {
  const pathname = usePathname();
  // must be updated by hand for every new app page
  const inApp = pathname.startsWith("/notes") || pathname.startsWith("/profile");
  return (
    <html lang="en">
      <body>
        {inApp ? <Sidebar /> : <MarketingHeader />}
        {children}
      </body>
    </html>
  );
}

Why this hurts

A layout should follow the shape of the file system, not a runtime if statement. As a client layout it can no longer export metadata, and every component it imports ends up in the browser bundle. With route groups, moving a page into the right folder is all it takes to get the right layout.

Core concept: parenthesised folders and stacked layouts

Wrap a folder name in parentheses and Next.js skips that folder when it builds the URL. The URL of app/(dashboard)/notes/page.tsx is /notes, not /(dashboard)/notes. A group changes nothing about routing but keeps every privilege of a folder: it can hold layout.tsx, loading.tsx and error.tsx, and those files apply only to the routes inside the group.

Layouts nest folder by folder. When /notes opens, Next.js places app/(dashboard)/layout.tsx inside app/layout.tsx (the root layout, the one with <html> and <body>), and places notes/page.tsx inside that as children. Each layout receives whatever sits below it as the children prop and decides where to show it.

text
Layouts wrapping the /notes page
--------------------------------
app/layout.tsx              <html><body> ... </body></html>
 +- (dashboard)/layout.tsx   <aside>Sidebar</aside><main> ... </main>
     +- notes/page.tsx       <h1>Notes</h1>

URL: /notes      (the "(dashboard)" folder never appears)

One important behaviour of layouts is that they do not re-render on navigation. Going from /notes to /profile keeps the dashboard layout in place and swaps only the page part, so text typed into a search box in the sidebar, or its scroll position, survives.

Groups are also useful for opting only some routes into a layout. Suppose app/ has three folders, account, cart and about, and only account and cart should get the shop layout. Move those two into (shop) and leave about outside: all three URLs stay the same, yet the layout now applies selectively. A loading.tsx placed in a group likewise gives loading UI only to the pages inside that group.

Watch two rules with groups. First, two pages in different groups that resolve to the same URL, such as (marketing)/about and (shop)/about, make Next.js report an error. Second, you may delete app/layout.tsx and give each group its own root layout with <html>, but moving between groups with different root layouts is then a full page load instead of a client-side navigation.

Groups are shelves, layouts are frames
  1. Drop (group) folders from the URL
  2. Match the remaining path to a page
  3. Collect layouts from root to page
  4. Outer layouts wrap the inner ones

To predict a URL from a file path, delete every parenthesised part and drop the final page.tsx.

General pattern

app/(group)/layout.tsx
// app/(group)/layout.tsx wraps every page inside (group)
export default function GroupLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return <Frame>{children}</Frame>;
}

// app/(group)/segment/page.tsx  ->  URL: /segment

Put one layout.tsx in the group folder and place children inside your own frame. Every page in the group gets that frame, while its URL stays free of the group name.

Worked example: splitting marketing from the dashboard

The folder tree below splits Myanmar Notes into two groups. The arrows on the right show which URL each file creates. Both groups share the top-level app/layout.tsx (not shown in this tree) and add a layout.tsx of their own.

text
app/
├─ (marketing)/
│  ├─ layout.tsx
│  ├─ page.tsx          → /
│  └─ about/page.tsx    → /about
└─ (dashboard)/
   ├─ layout.tsx
   ├─ notes/page.tsx    → /notes
   └─ profile/page.tsx  → /profile

Code breakdown

  1. Marketing group├─ (marketing)/

    The group for public pages. Because of the parentheses, the word marketing never shows up in a URL.

  2. Home page inside a group│ ├─ page.tsx → /

    The home page (/) can live inside a group too, which is how it picks up the marketing layout.

  3. Nested page in a group│ └─ about/page.tsx → /about

    about is an ordinary folder, so it does appear in the URL, while (marketing) is skipped.

  4. Dashboard layout ├─ layout.tsx

    The layout inside (dashboard) wraps only /notes and /profile, which makes it the right place for a sidebar.

  5. App pages ├─ notes/page.tsx → /notes

    Pages in the dashboard group get a short URL such as /notes and inherit the sidebar layout automatically.

You should see
The marketing and dashboard routes each use their own layout, and the group name doesn't appear in the URL.

The layout code for each group

Group layouts sit inside the root layout, so they must not repeat <html> and <body>. They are Server Components that only decide where children go. Split out only the interactive part, such as a sidebar search box, into its own Client Component (lesson 12).

app/(dashboard)/layout.tsx
import Link from "next/link";

export default function DashboardLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <div className="dashboard">
      <aside>
        <Link href="/notes">Notes</Link>
        <Link href="/profile">Profile</Link>
      </aside>
      <main>{children}</main>
    </div>
  );
}
SetupMoving between groups
One app/layout.tsx + group layoutsClient-side navigation; root layout stays
A separate root layout per groupFull page load (a new document)

In real projects

Route groups in real projects

Most SaaS products keep the marketing website and the app on one domain. Route groups separate the two cleanly inside a single codebase. In larger teams, each group is owned by one team, which maintains its own layout, loading UI and error UI.

  • An (auth) group holds /login and /signup and gives them a centred card layout without the site header.
  • The (dashboard) group's layout can become the place that checks for a logged-in user, though each page must still check its own data (lesson 20).
  • When an admin panel needs a completely different design from the app, teams give each group its own root layout and accept the full page load between them.
  • Because regrouping folders never changes a URL, the codebase can be refactored without breaking bookmarks, search engine links or links from other sites.

Common mistakes

Common mistakes with route groups

Expecting the group name in the URL

Writing <Link href="/dashboard/notes"> gives a 404, because the page lives at /notes. If you really want /dashboard/ in the URL, use an ordinary dashboard folder without parentheses. A quick search for href values that contain a group name catches this before users do.

Putting the same URL in two groups

(marketing)/about/page.tsx and (dashboard)/about/page.tsx both resolve to /about, so Next.js cannot choose and reports an error. Each URL must be owned by exactly one page file.

Repeating <html> in a group layout

While app/layout.tsx exists, a group layout is nested inside it; adding another <html><body> produces invalid HTML and can trigger hydration errors. Only when you truly want separate root layouts should you delete app/layout.tsx and give every group its own.

Best practices

Best practices

  • Name groups by purpose rather than by URL: (marketing), (dashboard) and (auth) explain themselves the moment you open the folder.
  • Default to one app/layout.tsx as the root layout plus group layouts; reach for multiple root layouts only when the designs truly have nothing in common.
  • Keep group layouts as Server Components and split out only the small stateful piece as a Client Component.

Mini challenge

Mini challenge: (public) and (workspace)

Split the project into (public) and (workspace) groups. Public pages get a top nav, workspace pages get a sidebar, and each group has its own background colour.

  • / and /pricing live in (public); /notes lives in (workspace)
  • Each group has its own layout.tsx
  • Group layouts contain no <html> or <body>
  • No URL contains the word public or workspace
app/(public)/layout.tsx + app/(workspace)/layout.tsx
// Target tree:
// app/layout.tsx              (keep: <html><body>{children}</body></html>)
// app/(public)/layout.tsx     TODO
// app/(public)/page.tsx       -> /
// app/(public)/pricing/page.tsx  -> /pricing
// app/(workspace)/layout.tsx  TODO
// app/(workspace)/notes/page.tsx -> /notes

// TODO: app/(public)/layout.tsx
// a <header> with links to / and /pricing, then children,
// on a light background

// TODO: app/(workspace)/layout.tsx
// an <aside> sidebar with a link to /notes, then children in <main>,
// on a different background

Expected: / and /pricing show the header on a light background, and /notes shows the sidebar on a different background. Moving between the groups is not a full reload, because they share one root layout.

Show hint

Both layouts take { children }: { children: React.ReactNode } and return a single wrapper <div>. Use a style or className on that wrapper for the background.

Show solution
app/(public)/layout.tsx + app/(workspace)/layout.tsx
// app/(public)/layout.tsx
import Link from "next/link";

export default function PublicLayout({ children }: { children: React.ReactNode }) {
  return (
    <div style={{ background: "#f8fafc", minHeight: "100vh" }}>
      <header>
        <Link href="/">Home</Link> <Link href="/pricing">Pricing</Link>
      </header>
      {children}
    </div>
  );
}

// ---------------------------------------------------------------
// app/(workspace)/layout.tsx  (a second, separate file)
import Link from "next/link";

export default function WorkspaceLayout({ children }: { children: React.ReactNode }) {
  return (
    <div style={{ background: "#eef2ff", display: "flex", minHeight: "100vh" }}>
      <aside>
        <Link href="/notes">My notes</Link>
      </aside>
      <main>{children}</main>
    </div>
  );
}

Quick quiz 1 / 3

Which URL does app/(dashboard)/notes/[id]/page.tsx serve?

Quick quiz 2 / 3

What happens if both app/(marketing)/about/page.tsx and app/(shop)/about/page.tsx exist?

Quick quiz 3 / 3

You delete app/layout.tsx and give both (app)/layout.tsx and (admin)/layout.tsx their own <html> root layout. What happens when a link goes from /notes to /admin/users?

What to remember

  • A parenthesised folder such as (name) groups code without appearing in the URL.
  • Each group can hold its own layout.tsx, loading.tsx and error.tsx, affecting only its routes.
  • Layouts wrap each other from the root down to the page and do not re-render on navigation.
  • Two groups must not produce the same URL, and different root layouts mean a full page load.

Next.js — Project Structure — Next.js

Next.js — Project Structure — Next.js

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Route Groups and Nested Layouts | Thuta Learning