Route Groups, Private Folders and Project Organisation

Next.js Course: Beginner to Advanced with Examples · Routing and Navigation · 4 min read · Updated

Folders for you, not for URLs

  1. A folder in round brackets is a group. It helps you organise, and it is hidden from the address.
  2. A page inside the group still defines its own address.
  3. A folder that starts with an underscore is private: the router ignores it, so it is safe for helper code.
  4. The visitor sees only /pricing. Neither the group nor the private folder shows up.

As a project grows, the app folder fills up. You might have marketing pages, shop pages, account pages and shared components all mixed together. Next.js gives you two small tools to keep order without changing a single URL: route groups and private folders. Think of them as labelled boxes in a cupboard. The boxes help you find things, but the people who use the room never see the labels.

Route groups: folders in parentheses

A folder whose name is wrapped in round brackets is a route group. The name is only for you. It is not part of the address. So app/(marketing)/pricing/page.tsx answers /pricing, not /marketing/pricing.

import Card from "../../_components/Card";

export default function PricingPage() {
  return <Card title="Pricing">Free while you learn.</Card>;
}

We built this and ran it. The address /pricing worked, and the (marketing) folder was invisible. The build list shows the same thing: the route is just /pricing.

Why use route groups?

  • Tidy folders. Put all marketing pages in (marketing), all account pages in (account), and so on.
  • Different layouts for different parts of the site. Each group can have its own layout file. The marketing pages can have a big header and footer, while the account pages have a sidebar. You will build this in the layouts lesson.
  • Splitting a big site between teams. Each team works in its own group.

Private folders: names that begin with an underscore

A folder whose name begins with an underscore, such as _components, is private. It is skipped by the router completely, even if it contains a page file. It is a safe place for helper code that belongs to one part of the app.

export default function Card({ title, children }: { title: string; children: React.ReactNode }) {
  return (
    <section className="rounded border p-4">
      <h2 className="font-semibold">{title}</h2>
      {children}
    </section>
  );
}

The card above lives in app/_components/Card.tsx. The pricing page imports it with a relative path. No address /_components exists. If you would like an address that really starts with an underscore, write %5F instead of the underscore in the folder name, but you will almost never need it.

Colocation: keep files near where they are used

In Next.js a folder is only public when it holds a page or route file. So you are free to put components, styles, tests and helper functions inside route folders. Many teams follow this habit: code used by one route stays next to the route, and code used by many routes goes into shared folders at the top of the project.

A tidy structure for a medium project

app/
  (marketing)/
    layout.tsx
    page.tsx            ->  /
    pricing/page.tsx    ->  /pricing
  (account)/
    layout.tsx
    login/page.tsx      ->  /login
    profile/page.tsx    ->  /profile
  _components/          (private, not a route)
components/             (shared across the whole app)
lib/                    (helpers, database code)
public/                 (images and icons)

This is only one good option. You can also use a src folder, or put shared code under app. What matters is that the team agrees and that new files go in an obvious place.

Folder names that people read

  • Use lowercase letters and dashes in route folders, for example about-us. URLs are easier to read and share, and capital letters can confuse servers.
  • Choose names that match what the visitor expects to see in the address bar.
  • Keep addresses short. A deep path such as /a/b/c/d/e is hard to remember.

Your task

  1. Create a (marketing) group with an about page and a pricing page. Check that the addresses do not include the word marketing.
  2. Create app/_components with a Card component and use it on the pricing page.
  3. Try adding the same address in two groups on purpose. Read the error message, then fix it.

How to check it worked

  • /pricing opens even though the folder is (marketing)/pricing.
  • The route list from npm run build shows /pricing and not /marketing/pricing.
  • You can explain the difference between a group and a private folder.

Frequently asked questions

Does the group name show in the URL?

No. A folder in round brackets is only for organising your files. The address does not include it.

When should I use a private folder?

When a folder holds helper code that belongs to one part of the app and must never become a route. Because only page and route files make addresses, a private folder is also a clear signal to other developers.

Can two route groups hold the same address?

No. Two files that would produce the same address cause a build error. Keep each address in one group.