Menu

Route Groups, Private Folders, and Special UI Files

Lesson 5: Route Groups, Private Folders, and Special UI Files

How do you organise a growing app?

Convention Syntax Effect on URL Use it for
Route group (marketing) None Grouping routes, giving them different layouts
Private folder _components Folder is never a route Keeping components and helpers next to routes
Dynamic segment [slug] Variable segment Pages from data

Example: two layouts with route groups

app/
├─ (marketing)/
│  ├─ layout.tsx        # Big header, footer
│  ├─ page.tsx          # /
│  └─ about/page.tsx    # /about
├─ (app)/
│  ├─ layout.tsx        # Sidebar, user menu
│  └─ dashboard/page.tsx # /dashboard
└─ layout.tsx          # Root: <html>, <body>

Both groups share the root layout, but marketing pages and app pages get different shells. The group names never appear in the URL.

Special UI files

File Shown when Notes
loading.tsx The route segment is loading Wraps the page in a Suspense boundary automatically
error.tsx An error is thrown while rendering the segment Must be a Client Component; receives error and reset
not-found.tsx notFound() is called, or no route matches Can be placed per segment
global-error.tsx The root layout itself fails Must render its own <html> and <body>
// app/courses/loading.tsx
export default function Loading() {
  return <p>Loading courses…</p>;
}
// app/courses/error.tsx
'use client';

export default function Error({
  error,
  reset,
}: {
  error: Error & { digest?: string };
  reset: () => void;
}) {
  return (
    <div>
      <h2>Could not load courses.</h2>
      <button onClick={() => reset()}>Try again</button>
    </div>
  );
}

How it works

For each segment, Next.js nests the special files in this order:

<Layout>
  <ErrorBoundary fallback={error.tsx}>
    <Suspense fallback={loading.tsx}>
      <ErrorBoundary fallback={not-found.tsx}>
        <Page />
  • An error in a page is caught by the nearest error.tsx above it, so the rest of the layout keeps working.
  • error.tsx does not catch errors in the layout of the same segment. Place it one level up for that.
  • In production, error messages from Server Components are hidden from the browser; only a digest ID is sent.

Common mistakes

Mistake Effect Fix
Missing 'use client' in error.tsx Build error Error boundaries must be Client Components
Two route groups resolving to the same URL Build error Each URL must come from one page
One loading.tsx at the root only Whole page shows a spinner Add loading.tsx or Suspense close to slow data
Showing raw error messages to users May leak internal details Show friendly text; log details on the server

Key takeaways

  • Route groups organise code and layouts without changing URLs.
  • loading, error, and not-found files give each segment its own fallback UI.
  • Place fallbacks close to the part of the page that can be slow or fail.

Try it: Split LearnHub into (marketing) and (app) route groups with different layouts, then add loading.tsx and not-found.tsx to the courses section.


Nextjs Tutorial

J

Jebasta





Get in Touch For Details