Next.js course Β· Module 2: Routing and Layouts

Route Grouping and Organization

15 min read Β·
In this lesson6

At the heart of the Quantum Metropolis, the urban space management system is based on the concept of thematic districts and functional zones. The city is divided into recognizable areas - a science district, residential zone, entertainment district, and many others. These zones are separated from each other but work together as elements of a single, cohesive urban organism.

Similarly, in Next.js 16, we can organize and group routes in our application to increase code clarity, improve management, and enable more flexible loading strategies and component isolation.

Route Groups

In App Router, we can create route groups that have no impact on the actual URL structure by placing a directory in parentheses, for example: (groupName). This is a powerful feature that allows for logical code organization without changing its behavior or public API.

Route Group Structure and Syntax

Route groups are defined by placing the directory name in parentheses:

app/
  β”œβ”€β”€ (marketing)/       # Group for marketing pages
  β”‚   β”œβ”€β”€ about/
  β”‚   β”‚   └── page.tsx   # /about
  β”‚   β”œβ”€β”€ blog/
  β”‚   β”‚   └── page.tsx   # /blog
  β”‚   └── layout.tsx     # Shared layout for marketing pages
  β”‚
  β”œβ”€β”€ (shop)/            # Group for marketing pages
  β”‚   β”œβ”€β”€ products/
  β”‚   β”‚   └── page.tsx   # /products
  β”‚   β”œβ”€β”€ cart/
  β”‚   β”‚   └── page.tsx   # /cart
  β”‚   └── layout.tsx     # Shared layout for shop pages
  β”‚
  └── layout.tsx         # Main application layout

In this example, the /about and /blog routes are part of the (marketing) group, while /products and /cart are part of the (shop) group. Note that route groups do not affect the actual URLs - the (marketing) directory is not part of the /about URL path.

Route Group Applications

1. File and Folder Organization

The simplest use of route groups is logical file organization, especially in larger applications:

app/
  β”œβ”€β”€ (main)/               # Main public pages
  β”‚   β”œβ”€β”€ page.tsx          # Home (/)
  β”‚   β”œβ”€β”€ about/            # /about
  β”‚   └── contact/          # /contact
  β”‚
  β”œβ”€β”€ (auth)/               # Authentication-related pages
  β”‚   β”œβ”€β”€ login/            # /login
  β”‚   β”œβ”€β”€ register/         # /register
  β”‚   └── forgot-password/  # /forgot-password
  β”‚
  β”œβ”€β”€ (dashboard)/          # Dashboard pages (protected)
  β”‚   β”œβ”€β”€ account/          # /account
  β”‚   β”œβ”€β”€ settings/         # /settings
  β”‚   └── billing/          # /billing
  β”‚
  └── layout.tsx            # Main layout

2. Sharing Layouts Between Specific Routes

One of the most powerful uses of route groups is the ability to define shared layouts for specific groups of pages without affecting the URL paths:

// app/(marketing)/layout.tsx - Layout for marketing pages
export default function MarketingLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <div className="marketing-layout">
      <nav className="marketing-nav">
        <a href="/about">About Us</a>
        <a href="/blog">Blog</a>
        <a href="/careers">Careers</a>
      </nav>
      <main>{children}</main>
      <footer className="marketing-footer">
        <p>Β© 2150 Quantum Voyages. All rights reserved.</p>
      </footer>
    </div>
  );
}
// app/(shop)/layout.tsx - Layout for shop pages
export default function ShopLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <div className="shop-layout">
      <header className="shop-header">
        <h1>Quantum Voyages Store</h1>
        <nav className="shop-nav">
          <a href="/products">Products</a>
          <a href="/categories">Categories</a>
          <a href="/cart">Cart</a>
        </nav>
      </header>
      <main>{children}</main>
      <aside className="shop-sidebar">
        <div className="popular-products">
          <h3>Popular Products</h3>
          {/* List of popular products */}
        </div>
      </aside>
    </div>
  );
}

3. Splitting the Application into Sections

Route groups are particularly useful in multi-section applications, such as an application with a public homepage, admin panel, and user dashboard:

app/
  β”œβ”€β”€ (public)/           # Public part of the website
  β”‚   β”œβ”€β”€ page.tsx        # Home (/)
  β”‚   β”œβ”€β”€ about/          # /about
  β”‚   β”œβ”€β”€ blog/           # /blog
  β”‚   β”‚   └── [slug]/     # /blog/:slug
  β”‚   └── layout.tsx      # Public layout
  β”‚
  β”œβ”€β”€ (admin)/            # Admin panel
  β”‚   β”œβ”€β”€ admin/          # /admin (note: the admin/ directory is needed to preserve the URL)
  β”‚   β”‚   β”œβ”€β”€ page.tsx    # /admin
  β”‚   β”‚   β”œβ”€β”€ users/      # /admin/users
  β”‚   β”‚   └── settings/   # /admin/settings
  β”‚   └── layout.tsx      # Admin layout
  β”‚
  β”œβ”€β”€ (dashboard)/        # User dashboard
  β”‚   β”œβ”€β”€ dashboard/      # /dashboard
  β”‚   β”‚   β”œβ”€β”€ page.tsx    # /dashboard
  β”‚   β”‚   β”œβ”€β”€ profile/    # /dashboard/profile
  β”‚   β”‚   └── settings/   # /dashboard/settings
  β”‚   └── layout.tsx      # Dashboard layout
  β”‚
  └── layout.tsx          # Main layout (shared across all sections)

In this example, the application is divided into three main sections, each with its own layout, but all sharing a common main layout.

Quantum Voyages Example

Let's implement a route group system for our space application Quantum Voyages:

app/
  β”œβ”€β”€ (marketing)/            # Marketing pages
  β”‚   β”œβ”€β”€ page.tsx            # Home (/)
  β”‚   β”œβ”€β”€ about/              # /about
  β”‚   β”œβ”€β”€ destinations/       # /destinations
  β”‚   β”‚   └── [id]/           # /destinations/:id
  β”‚   └── layout.tsx          # Marketing layout
  β”‚
  β”œβ”€β”€ (customer)/             # Pages for logged-in customers
  β”‚   β”œβ”€β”€ dashboard/          # /dashboard
  β”‚   β”œβ”€β”€ bookings/           # /bookings
  β”‚   β”‚   └── [id]/           # /bookings/:id
  β”‚   β”œβ”€β”€ account/            # /account
  β”‚   └── layout.tsx          # Customer layout
  β”‚
  β”œβ”€β”€ (admin)/                # Admin panel
  β”‚   β”œβ”€β”€ admin/              # /admin
  β”‚   β”‚   β”œβ”€β”€ voyages/        # /admin/voyages
  β”‚   β”‚   β”œβ”€β”€ customers/      # /admin/customers
  β”‚   β”‚   └── analytics/      # /admin/analytics
  β”‚   └── layout.tsx          # Admin layoutistracyjny
  β”‚
  └── layout.tsx              # Main layout

Code for the layouts:

// app/layout.tsx - Main layout
import { Inter } from 'next/font/google';
import './globals.css';

const inter = Inter({ subsets: ['latin'] });

export const metadata = {
  title: 'Quantum Voyages - Space Travels from the Quantum Metropolis',
  description: 'Discover space with the Quantum Metropolis guide',
};

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang="pl">
      <body className={inter.className}>
        {children}
      </body>
    </html>
  );
}
// app/(marketing)/layout.tsx - Marketing layout
import Header from '@/components/marketing/Header';
import Footer from '@/components/marketing/Footer';

export default function MarketingLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <>
      <Header />
      <main className="min-h-screen pt-16 pb-20">
        {children}
      </main>
      <Footer />
    </>
  );
}
// app/(customer)/layout.tsx - Layout for logged-in customers
import CustomerHeader from '@/components/customer/Header';
import CustomerSidebar from '@/components/customer/Sidebar';
import { checkAuth } from '@/lib/auth';
import { redirect } from 'next/navigation';

export default async function CustomerLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  // Check if the user is logged in
  const user = await checkAuth();
  if (!user) {
    // The layout is a Server Component: there is no window object here. proxy.ts can add the return address.
    redirect('/login');
  }

  return (
    <>
      <CustomerHeader user={user} />
      <div className="flex min-h-screen pt-16">
        <CustomerSidebar />
        <main className="flex-1 p-6 bg-gray-50">
          {children}
        </main>
      </div>
    </>
  );
}
// app/(admin)/layout.tsx - Admin layout
import AdminHeader from '@/components/admin/Header';
import AdminSidebar from '@/components/admin/Sidebar';
import { checkAdminAuth } from '@/lib/auth';
import { redirect } from 'next/navigation';

export default async function AdminLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  // Check if the user is an admin
  const isAdmin = await checkAdminAuth();
  if (!isAdmin) {
    redirect('/admin-login');
  }

  return (
    <div className="admin-layout">
      <AdminHeader />
      <div className="flex min-h-screen pt-16">
        <AdminSidebar />
        <main className="flex-1 p-6 bg-gray-100">
          {children}
        </main>
      </div>
    </div>
  );
}

Groups Only for Organizing Files

A route group doesn't need its own layout.tsx. You can use it only to keep files in order, and the URLs stay the same:

app/
  β”œβ”€β”€ (onboarding)/welcome/
  β”‚                └── page.tsx   # /welcome
  └── (payments)/cart/
                  └── page.tsx    # /cart

Separating Layouts and URL Paths

Route groups are particularly useful when we want to share a layout between specific routes that aren't necessarily close to each other in the URL hierarchy:

app/
  β”œβ”€β”€ (auth)/
  β”‚   β”œβ”€β”€ login/         # /login
  β”‚   β”‚   └── page.tsx
  β”‚   β”œβ”€β”€ register/      # /register
  β”‚   β”‚   └── page.tsx
  β”‚   └── layout.tsx     # Shared layout only for /login and /register
  β”‚
  β”œβ”€β”€ account/           # /account (does not use the (auth) layout)
  β”‚   └── page.tsx
  β”‚
  └── layout.tsx         # Main layout for all routes

Private Folders (_folderName)

Next.js 16 also supports the private folders convention, which are marked with an underscore (_) at the beginning of the name. Such folders are not included in URL paths but can contain code shared by multiple routes.

Private Folder Structure and Syntax

app/
  β”œβ”€β”€ _components/           # Private folder for shared components
  β”‚   β”œβ”€β”€ Button.tsx         # Button component
  β”‚   β”œβ”€β”€ Card.tsx           # Card component
  β”‚   └── SearchForm.tsx     # Search form component
  β”‚
  β”œβ”€β”€ _lib/                  # Private folder for shared functions
  β”‚   β”œβ”€β”€ api.ts             # API functions
  β”‚   └── utils.ts           # Utility functions
  β”‚
  β”œβ”€β”€ destinations/          # Public route
  β”‚   └── page.tsx           # /destinations
  β”‚
  └── page.tsx               # Home (/)

In this example, the _components and _lib directories are private and do not generate any URL paths. They are used to store shared components and functions that can be imported by other components.

Difference Between Private Folders and Route Groups

Private folders and route groups look similar but have different use cases:

  • Private folders (_folderName) are used to organize code that should not be directly accessible via URL.
  • Route groups ((folderName)) are used to group routes without affecting URL paths, but they can influence layout behavior.

Example of Using Private Folders in the Quantum Voyages Application

app/
  β”œβ”€β”€ _components/                # Shared UI components
  β”‚   β”œβ”€β”€ ui/                     # Basic UI components
  β”‚   β”‚   β”œβ”€β”€ Button.tsx
  β”‚   β”‚   β”œβ”€β”€ Card.tsx
  β”‚   β”‚   └── Input.tsx
  β”‚   β”‚
  β”‚   β”œβ”€β”€ marketing/              # Components for marketing pages
  β”‚   β”‚   β”œβ”€β”€ HeroSection.tsx
  β”‚   β”‚   └── TestimonialCard.tsx
  β”‚   β”‚
  β”‚   └── bookings/               # Components for the booking system
  β”‚       β”œβ”€β”€ BookingForm.tsx
  β”‚       └── PlanetSelector.tsx
  β”‚
  β”œβ”€β”€ _lib/                       # Shared functions and utilities
  β”‚   β”œβ”€β”€ api/                    # Functions for API communication
  β”‚   β”‚   β”œβ”€β”€ destinations.ts
  β”‚   β”‚   └── bookings.ts
  β”‚   β”‚
  β”‚   β”œβ”€β”€ hooks/                  # Custom React hooks
  β”‚   β”‚   β”œβ”€β”€ useBooking.ts
  β”‚   β”‚   └── useAuth.ts
  β”‚   β”‚
  β”‚   └── utils/                  # Helper functions
  β”‚       β”œβ”€β”€ dates.ts
  β”‚       └── validation.ts
  β”‚
  β”œβ”€β”€ _data/                      # Static data (you can also use /public)
  β”‚   β”œβ”€β”€ destinations.json
  β”‚   └── testimonials.json
  β”‚
  β”œβ”€β”€ (marketing)/                # Group for marketing pages
  β”‚   β”œβ”€β”€ page.tsx                # Home (/)
  β”‚   β”œβ”€β”€ about/                  # /about
  β”‚   └── destinations/           # /destinations
  β”‚
  └── (customer)/                 # Group for customer pages
      β”œβ”€β”€ dashboard/              # /dashboard
      └── bookings/               # /bookings

Example code using private folders:

// app/_components/ui/Button.tsx
import { cva, type VariantProps } from 'class-variance-authority';
import { cn } from '@/app/_lib/utils/cn';

const buttonVariants = cva(
  'inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2',
  {
    variants: {
      variant: {
        default: 'bg-primary text-primary-foreground hover:bg-primary/90',
        destructive: 'bg-destructive text-destructive-foreground hover:bg-destructive/90',
        outline: 'border border-input bg-background hover:bg-accent hover:text-accent-foreground',
        secondary: 'bg-secondary text-secondary-foreground hover:bg-secondary/80',
        ghost: 'hover:bg-accent hover:text-accent-foreground',
        link: 'text-primary underline-offset-4 hover:underline',
      },
      size: {
        default: 'h-10 px-4 py-2',
        sm: 'h-9 rounded-md px-3',
        lg: 'h-11 rounded-md px-8',
        icon: 'h-10 w-10',
      },
    },
    defaultVariants: {
      variant: 'default',
      size: 'default',
    },
  }
);

export interface ButtonProps
  extends React.ButtonHTMLAttributes<HTMLButtonElement>,
    VariantProps<typeof buttonVariants> {
  asChild?: boolean;
}

export function Button({
  className,
  variant,
  size,
  asChild = false,
  ...props
}: ButtonProps) {
  return (
    <button
      className={cn(buttonVariants({ variant, size, className }))}
      {...props}
    />
  );
}
// app/(marketing)/destinations/page.tsx
import { getDestinations } from '@/app/_lib/api/destinations';
import DestinationCard from '@/app/_components/marketing/DestinationCard';
import { HeroSection } from '@/app/_components/marketing/HeroSection';

export default async function DestinationsPage() {
  const destinations = await getDestinations();

  return (
    <div className="container mx-auto px-4 py-12">
      <HeroSection
        title="Discover Space with Quantum Voyages"
        subtitle="Browse our space destinations and plan your adventure"
        image="/images/space-banner.jpg"
      />

      <section className="my-12">
        <h2 className="text-3xl font-bold mb-8">Popular Destinations</h2>

        <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8">
          {destinations.map((destination) => (
            <DestinationCard
              key={destination.id}
              destination={destination}
            />
          ))}
        </div>
      </section>
    </div>
  );
}

Route Files (route.js)

Next.js 16 enables creating API endpoints using route.js (or route.ts) files. These files can also benefit from organizational techniques such as route groups and private folders.

API Endpoint Organization

app/
  β”œβ”€β”€ api/                      # Main API section
  β”‚   β”œβ”€β”€ (public)/             # Public endpoints
  β”‚   β”‚   β”œβ”€β”€ destinations/
  β”‚   β”‚   β”‚   └── route.ts      # GET /api/destinations
  β”‚   β”‚   └── newsletter/
  β”‚   β”‚       └── route.ts      # POST /api/newsletter
  β”‚   β”‚
  β”‚   β”œβ”€β”€ (protected)/          # Protected endpoints (require authorization)
  β”‚   β”‚   β”œβ”€β”€ bookings/
  β”‚   β”‚   β”‚   β”œβ”€β”€ route.ts      # GET, POST /api/bookings
  β”‚   β”‚   β”‚   └── [id]/
  β”‚   β”‚   β”‚       └── route.ts  # GET, PUT, DELETE /api/bookings/:id
  β”‚   β”‚   └── user/
  β”‚   β”‚       └── route.ts      # GET /api/user
  β”‚   β”‚
  β”‚   └── _middleware/          # Middleware for API (private folder)
  β”‚       β”œβ”€β”€ auth.ts           # Authentication middleware
  β”‚       └── rateLimit.ts      # Rate limiting middleware
  β”‚
  └── page.tsx                  # Home (/)

Example implementation of a protected API endpoint:

// app/api/(protected)/bookings/route.ts
import { NextResponse } from 'next/server';
import { verifyAuth } from '@/app/api/_middleware/auth';
import { rateLimit } from '@/app/api/_middleware/rateLimit';

export async function GET(request: Request) {
  // Verify authentication
  const authResult = await verifyAuth(request);
  if (!authResult.isAuthenticated) {
    return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
  }

  // Rate limiting
  const rateLimitResult = await rateLimit(request);
  if (!rateLimitResult.success) {
    return NextResponse.json({ error: 'Too many requests' }, { status: 429 });
  }

  // Get bookings for the logged-in user
  const userId = authResult.user.id;
  const bookings = await getBookingsForUser(userId);

  return NextResponse.json({ bookings });
}

export async function POST(request: Request) {
  // Verify authentication
  const authResult = await verifyAuth(request);
  if (!authResult.isAuthenticated) {
    return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
  }

  // Parse data from the request
  const data = await request.json();

  // Data validation
  const validationResult = validateBookingData(data);
  if (!validationResult.success) {
    return NextResponse.json({ error: validationResult.errors }, { status: 400 });
  }

  // Create booking
  const userId = authResult.user.id;
  const booking = await createBooking(userId, data);

  return NextResponse.json({ booking }, { status: 201 });
}

Best Practices for Route Organization in Large Applications

In the Quantum Metropolis, detailed planning is key to the efficient functioning of the city. Similarly, in larger Next.js 16 applications, it's important to follow specific rules and patterns for route organization:

1. Use a Clear and Consistent Structure

Maintain a consistent directory structure and naming throughout the application:

app/
  β”œβ”€β”€ (sections)/                 # Groups for main sections
  β”‚   β”œβ”€β”€ (marketing)/            # Public section
  β”‚   β”œβ”€β”€ (app)/                  # Main application
  β”‚   └── (admin)/                # Admin panel
  β”‚
  β”œβ”€β”€ _lib/                       # Shared code
  β”‚   β”œβ”€β”€ components/             # Shared components
  β”‚   β”œβ”€β”€ hooks/                  # React hooks
  β”‚   └── utils/                  # Utility functions
  β”‚
  β”œβ”€β”€ api/                        # API endpoints
  β”‚   β”œβ”€β”€ (public)/
  β”‚   └── (protected)/
  β”‚
  └── layout.tsx                  # Main layout

2. Share Components Wisely

Use private folders to store shared components and place them at the appropriate hierarchy level:

  • Components used throughout the application: app/_components/
  • Section-specific components: app/(section)/_components/

3. Apply Separation of Concerns

  • page.js - Page rendering and data fetching
  • layout.js - UI layout and navigation logic
  • loading.js - Loading states
  • error.js - Error handling
  • not-found.js - Missing resource handling

4. Use Import Aliases in tsconfig.json/jsconfig.json

Configure import aliases to simplify paths:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["./*"],
      "@components/*": ["./app/_components/*"],
      "@lib/*": ["./app/_lib/*"],
      "@hooks/*": ["./app/_lib/hooks/*"],
      "@utils/*": ["./app/_lib/utils/*"]
    }
  }
}

This allows you to import components in a more readable way:

// Instead of:
import { Button } from '../../../_components/ui/Button';

// You can use:
import { Button } from '@components/ui/Button';

5. Implementation for the Quantum Voyages Application

Example of a comprehensive structure for a large Quantum Voyages application:

app/
  β”œβ”€β”€ (public)/                    # Public part of the website
  β”‚   β”œβ”€β”€ _components/             # Components specific to the public section
  β”‚   β”œβ”€β”€ page.tsx                 # Home (/)
  β”‚   β”œβ”€β”€ about/                   # /about
  β”‚   β”œβ”€β”€ destinations/            # /destinations
  β”‚   β”‚   β”œβ”€β”€ page.tsx             # Destination list
  β”‚   β”‚   └── [id]/                # /destinations/:id
  β”‚   β”‚       └── page.tsx         # Destination details
  β”‚   β”œβ”€β”€ contact/                 # /contact
  β”‚   └── layout.tsx               # Public layout
  β”‚
  β”œβ”€β”€ (auth)/                      # Authentication section
  β”‚   β”œβ”€β”€ _components/             # Authentication components
  β”‚   β”œβ”€β”€ _lib/                    # Authentication logic
  β”‚   β”œβ”€β”€ login/                   # /login
  β”‚   β”œβ”€β”€ register/                # /register
  β”‚   β”œβ”€β”€ forgot-password/         # /forgot-password
  β”‚   └── layout.tsx               # Authentication layout
  β”‚
  β”œβ”€β”€ (app)/                       # Main application (for logged-in users)
  β”‚   β”œβ”€β”€ _components/             # Application components
  β”‚   β”œβ”€β”€ dashboard/               # /dashboard
  β”‚   β”œβ”€β”€ bookings/                # /bookings
  β”‚   β”‚   β”œβ”€β”€ page.tsx             # Booking list
  β”‚   β”‚   β”œβ”€β”€ new/                 # /bookings/new
  β”‚   β”‚   └── [id]/                # /bookings/:id
  β”‚   β”‚       β”œβ”€β”€ page.tsx         # Booking details
  β”‚   β”‚       └── edit/            # /bookings/:id/edit
  β”‚   β”œβ”€β”€ account/                 # /account
  β”‚   β”‚   β”œβ”€β”€ page.tsx             # Account overview
  β”‚   β”‚   β”œβ”€β”€ profile/             # /account/profile
  β”‚   β”‚   └── preferences/         # /account/preferences
  β”‚   └── layout.tsx               # Application layout
  β”‚
  β”œβ”€β”€ (admin)/                     # Admin panel
  β”‚   β”œβ”€β”€ _components/             # Admin components
  β”‚   β”œβ”€β”€ admin/                   # /admin (folder needed for the URL)
  β”‚   β”‚   β”œβ”€β”€ page.tsx             # Admin dashboard
  β”‚   β”‚   β”œβ”€β”€ users/               # /admin/users
  β”‚   β”‚   β”œβ”€β”€ destinations/        # /admin/destinations
  β”‚   β”‚   └── bookings/            # /admin/bookings
  β”‚   └── layout.tsx               # Admin layouta
  β”‚
  β”œβ”€β”€ _components/                 # Shared components
  β”‚   β”œβ”€β”€ ui/                      # Basic UI components
  β”‚   └── forms/                   # Form components
  β”‚
  β”œβ”€β”€ _lib/                        # Shared functions
  β”‚   β”œβ”€β”€ api/                     # API functions
  β”‚   β”œβ”€β”€ hooks/                   # Custom hooks
  β”‚   └── utils/                   # Utility functions
  β”‚
  β”œβ”€β”€ api/                         # API endpoints
  β”‚   β”œβ”€β”€ (public)/                # Public API
  β”‚   └── (protected)/             # Protected API
  β”‚
  └── layout.tsx                   # Main layout

Summary

Route grouping and organization in Next.js 16 offers many possibilities for structuring applications in a logical and clear way, similar to how urban zoning in the Quantum Metropolis organizes urban space for maximum efficiency and functionality.

Key concepts:

  1. Route groups ((groupName)) - allow logical grouping of routes without affecting URL paths, which is particularly useful for dividing different application sections and implementing specific layouts.

  2. Private folders (_folderName) - enable organization of code, shared components, and functions that are not directly accessible via URL.

  3. Hierarchical layouts - the ability to define specific layouts for different route groups, ensuring a consistent user experience within each section.

  4. API organization - applying the same organizational principles to API endpoints.

With these techniques, you can create applications that are:

  • More transparent and easier to maintain
  • Flexible in terms of navigation and user experience
  • Well-organized for development teams
  • Scalable for growing applications

In the next chapter, we'll discover how to handle more complex navigation scenarios, such as nested routes and catch-all parameters, which enable creating advanced and flexible navigation systems in Next.js 16 applications.

Docs entries used in this lesson

Spotted a mistake in this lesson?

Useful articles