SkillPilotAI

App Router and file conventions

In the Next.js App Router, folders become URLs and a few special file names (page, layout, loading, error, not-found, template) decide the UI, the loading state and error handling of each route. Knowing how these files wrap each other is a common interview topic.

59.1 Folder = URL

  • No route config: folders inside app become URL segments.
  • about/page.tsx creates /about; dashboard/settings/page.tsx creates /dashboard/settings.
  • A folder without a page file (components) never becomes a route.
Output
app/  layout.tsx  page.tsx          -> /  about/    page.tsx        -> /about  dashboard/    layout.tsx    page.tsx        -> /dashboard    settings/      page.tsx      -> /dashboard/settings  components/    Navbar.tsx      -> no route

59.2 page file: the UI of a route

  • A normal React component with one rule: it must be the default export.
  • Opening /about shows this content.
React (TSX)
export default function AboutPage() {  return (    <main>      <h1>About us</h1>      <p>We sell books across India.</p>    </main>  );}
Output
# About usWe sell books across India.

59.3 Why layouts? (problem: header copied on every page)

  • Copying the same header into 20 page files repeats code.
  • On every navigation the copied header mounts again, so its state (search box text) resets.
  • A layout wraps pages and stays the same during navigation.

59.4 Root layout

  • Every Next.js app must have app/layout.tsx; it wraps the whole website.
  • children is the current page and changes with the URL.
  • html and body are written only in the root layout; the header stays common on every page.
React (TSX)
export default function RootLayout({  children,}: {  children: React.ReactNode;}) {  return (    <html lang="en">      <body>        <header>BookKart</header>        {children}      </body>    </html>  );}
Output
BookKart# About usWe sell books across India.

59.5 Nested layout

  • app/dashboard/layout.tsx wraps only the dashboard pages (sidebar in an aside).
  • /dashboard/settings renders: root layout > dashboard layout > settings page.
  • The sidebar does not mount again when the page changes.
React (TSX)
export default function DashboardLayout({  children,}: {  children: React.ReactNode;}) {  return (    <section>      <aside>Orders | Settings</aside>      <div>{children}</div>    </section>  );}
Output
BookKartOrders | Settings# Settings
How one folder's special files wrap
  1. 1.layout: outermost, persists across navigation

  2. 2.template: remounts on every navigation (if present)

  3. 3.error: Error boundary

  4. 4.loading: Suspense boundary

  5. 5.page: innermost, your UI

loading and error cover only the page; the layout always stays on screen

59.6 loading file

  • An async server component can await data (getOrders may take 2 seconds) with no feedback for the user.
  • In Next.js 16 npm run build even fails when slow data has no loading UI around it.
  • Fix: app/dashboard/loading.tsx. Next wraps the page in Suspense with this UI; the layout shows Loading orders... immediately and the real page streams in when data arrives.
React (TSX)
import { getOrders } from "@/lib/orders"; export default async function OrdersPage() {  const orders = await getOrders();  return (    <ul>      {orders.map((o) => (        <li key={o.id}>{o.title}</li>      ))}    </ul>  );}
React (TSX)
export default function Loading() {  return <p>Loading orders...</p>;}
Output
BookKartOrders | SettingsLoading orders... BookKartOrders | SettingsAtomic HabitsWings of Fire
Loading and streaming
  1. 1.User clicks Dashboard

  2. 2.Layout (header + sidebar) shows immediately

  3. 3.loading.tsx shows Loading orders... in place of the page

  4. 4.getOrders resolves on the server

  5. 5.Next.js streams the real page into that spot

59.7 error file

  • Catches errors in the page (getOrders fails) and saves the whole app from crashing.
  • Must start with "use client" because an error boundary works in the browser.
  • Receives a retry function: Try again fetches the data and re-renders the page; the sidebar layout stays on screen.
React (TSX)
"use client";export default function OrdersError({  retry,}: {  retry: () => void;}) {  return (    <div>      <p>Could not load orders.</p>      <button onClick={() => retry()}>        Try again      </button>    </div>  );}
Output
BookKartOrders | SettingsCould not load orders.[button] Try again

59.8 not-found file

  • app/not-found.tsx shows for any unknown URL, with a 404 status.
  • Inside a page, call notFound() (for example a product missing in the database) to show the same kind of UI.
React (TSX)
import Link from "next/link"; export default function NotFound() {  return (    <div>      <h2>Page not found</h2>      <Link href="/">Back to home</Link>    </div>  );}
Output
BookKart# Page not foundBack to home

59.9 Checkpoint: which error file catches it?

  • An error inside dashboard/layout.tsx is caught by app/error.tsx, not dashboard/error.tsx.
  • The dashboard error boundary sits inside the dashboard layout, so it cannot catch the layout's own error.
  • Errors in the root layout need a separate global-error file.
Output
app/  error.tsx  dashboard/    error.tsx    layout.tsx   <- error here    page.tsx
Output
> app/error.tsx catches it

59.10 Comparison: special files

FileRoleNote
pageUI of the route, makes it publicDefault export required
layoutShared UI, persists across navigationRoot layout has html and body
templateLike layout, but remounts every navigationState resets, effects re-run
loadingSuspense boundary around the pageNeeded for slow data in Next.js 16 builds
errorError boundary around the pageMust be "use client"
not-foundUI for unknown URLs / notFound()404 status
global-errorCatches errors in the root layoutSeparate from error

59.11 Common mistakes

  • Logging page views with useEffect in a layout: the layout does not remount, so the effect runs only once. Use a template, which creates a new instance on every navigation.
  • Thinking every folder inside app is a page: a route is public only with a page file. Folders starting with _ are private.
  • Forgetting "use client" at the top of error.tsx.
React (TSX)
// Wrong: app/dashboard/layout.tsx (effect runs only once)"use client";import { useEffect } from "react";import { logPageView } from "@/lib/analytics"; export default function Layout({  children,}: { children: React.ReactNode }) {  useEffect(() => {    logPageView();  }, []);  return <div>{children}</div>;}
React (TSX)
// Right: app/dashboard/template.tsx (effect runs on every navigation)"use client";import { useEffect } from "react";import { logPageView } from "@/lib/analytics"; export default function Template({  children,}: { children: React.ReactNode }) {  useEffect(() => {    logPageView();  }, []);  return <div>{children}</div>;}

59.12 Best practices

  • html and body only in the root layout; keep common UI in a layout, not copied into pages.
  • Put loading in folders with slow data and error in important sections.
  • Always "use client" at the top of error files.
  • Use template only when you need a reset or an effect on every navigation; otherwise layout.

59.13 Interview tips

  • Layout vs template: both wrap children; layout stays mounted (state preserved), template remounts on every navigation (state resets).
  • Wrap order in one folder: layout > template > error > loading > page.
  • loading = Suspense boundary, error = Error boundary; both cover only the page.
  • A route becomes public only when its folder has a page file.