Course map
Stage 1: App Router Foundations
Stage 2: The Rendering Model
Stage 3: Data, Caching and Mutations
Stage 4: Full-Stack Next.js
Stage 5: Production and Specialization

Part 03 / Topic 2 of 7

Route groups for a second reason: scoping a layout

Part 2 section 6 used a route group, (info), purely to tidy the file tree — about and contact sat together on disk without "info" ever appearing in their URLs. That is one reason to reach for (group) syntax. This part reaches for the same syntax for a genuinely different reason: a route group also lets a layout.tsx placed inside it apply only to that group, without the group's name ever touching a URL.

To do this, Apna Bazaar's storefront pages (page.tsx, products/, search/) moved into a second group, (shop), sitting right next to (info):

$ find app/store -maxdepth 1 -not -path '*/node_modules*'
app/store
app/store/(info)
app/store/(shop)
app/store/_components
app/store/layout.tsx
app/store/lib

Moving a page into a group changes nothing about its URL — only its file location, and therefore the relative import path up to lib/products.ts gained one more ../. app/store/(shop)/layout.tsx wraps only what is inside (shop):

export default function ShopLayout({ children }: { children: React.ReactNode }) {
  return (
    <div data-scope="shop">
      <p style={{ fontFamily: "system-ui", padding: "0 2rem", color: "#555", fontSize: "0.85rem" }}>
        🛍️ Browsing Apna Bazaar
      </p>
      {children}
    </div>
  );
}

and its sibling, app/store/(info)/layout.tsx, wraps only about and contact with a different banner. Both groups sit under the same parent layout from section 1, so both get the shared nav — but only one of the two gets each banner. Proven for real, by marking each wrapper with a data-scope attribute and curling five different pages:

$ curl -s http://localhost:PORT/store | grep -o 'data-scope="[a-z]*"'
data-scope="shop"
$ curl -s http://localhost:PORT/store/products | grep -o 'data-scope="[a-z]*"'
data-scope="shop"
$ curl -s http://localhost:PORT/store/products/p1 | grep -o 'data-scope="[a-z]*"'
data-scope="shop"
$ curl -s http://localhost:PORT/store/about | grep -o 'data-scope="[a-z]*"'
data-scope="info"
$ curl -s http://localhost:PORT/store/contact | grep -o 'data-scope="[a-z]*"'
data-scope="info"

And the build's route table (section 1) proves the other half of the claim: moving every storefront page into (shop) did not change a single URL — /store, /store/products, /store/products/[id] and /store/search/[...terms] all read exactly as they did at the end of Part 2.

Full files: examples/03-shop-group-layout.tsx, examples/04-info-group-layout.tsx.

Done here? The practice set for Part 3 — including the solutions — is on the part page.

Search the guide