DEV Community

prateekshaweb
prateekshaweb

Posted on • Originally published at prateeksha.com

Next.js App Router for Beginners: Pages, Layouts, and Navigation

Hook

Routing can make or break your app’s developer experience and performance. Next.js’s new app router (the /app directory) simplifies routing, nested layouts, and data fetching so your pages load faster and your codebase stays tidy. This guide gets you productive with pages, layouts, and navigation without drowning in theory.

Why this matters

Modern web apps require fast navigation, clear URL structure, and reusable UI scaffolding. The older pages router forced a lot of shared UI into a single global wrapper, which becomes unwieldy as apps grow. The app router solves that with nested layouts, server components, and file-based routes that read like your site map.

What the app router gives you

At a high level, the app router brings:

  • File-and-folder routing via the app/ directory that maps directly to URLs.
  • Nested layout support so headers, sidebars, and footers can be scoped.
  • Server-first components and async data fetching for better performance.
  • Cleaner handling of loading, error, and not-found states per route.

These features let you compose UI in layers (root layout → section layout → page) rather than repeating wrappers on every page.

Quick implementation roadmap

Follow this simple path to get a minimal app router setup:

  1. Create a Next app and choose the /app option: npx create-next-app@latest my-nextjs-app.
  2. Confirm the project contains app/layout.js and app/page.js — these are your root layout and home page.
  3. Add routes by creating folders with page.js (for example, app/about/page.js becomes /about).
  4. Add nested layouts by placing layout.js inside a folder (for example, app/blog/layout.js wraps /blog and its children).
  5. Create dynamic routes using bracket syntax: app/blog/[slug]/page.js and access params.slug inside the component.

This flow keeps your file structure and URLs in sync, making it easy to reason about navigation and UI scope.

Practical tips and best practices

  • Keep layouts focused on persistent UI only: headers, footers, navigation, and providers. Avoid storing ephemeral page state in layouts because they persist across navigations.
  • Use Link from next/link for internal navigation to enable client-side transitions; use the useRouter hook (next/navigation) for programmatic redirects. Remember: any file using useRouter must be a client component (add 'use client' at the top).
  • Organize components into a shared components/ folder for items reused across layouts and pages.
  • For dynamic pages, fetch data in the page component (it can be async) and fall back to a not-found UI if params don’t match a resource.
  • Leverage layout-level loading.js and error.js files to show per-section loading spinners or error messages while nested content resolves.

Short example (file names, not code blocks)

  • app/layout.js → root layout with global header and NavBar
  • app/page.js → homepage content
  • app/blog/layout.js → blog-specific layout (sidebar, feed)
  • app/blog/page.js → blog index
  • app/blog/[slug]/page.js → dynamic blog post

This structure keeps UI concerns separated and makes it straightforward to add or remove entire sections without touching the global layout.

Navigation patterns

Use Link for anchor-like navigation and useRouter for actions (after form submit, auth flow, etc.). For highlighting active links, use usePathname from next/navigation inside a client NavBar and apply an "active" class based on the path.

Prefetching: Link prefetches pages in the viewport by default, which improves perceived speed. If you have many links, you can disable prefetch with prefetch={false} for less critical routes.

Quick checklist before you ship

  • [ ] Are layouts minimized to persistent UI?
  • [ ] Do internal links use Link (no full-page reloads)?
  • [ ] Are client hooks used only inside client components?
  • [ ] Are dynamic routes returning sensible not-found or error UIs?
  • [ ] Is navigation responsive and accessible?

Where to learn more and examples

If you want a worked example or a longer walkthrough, check the original tutorial at https://prateeksha.com/blog/nextjs-app-router-for-beginners-pages-layouts-navigation. For more resources, previews, and related posts see https://prateeksha.com/blog and the company site https://prateeksha.com.

Conclusion

The Next.js app router is a practical improvement: it maps routes to folders, scopes layouts naturally, and encourages server-first thinking for performance. Start small—move one section at a time, use nested layouts to reduce duplication, and prefer Link/useRouter for smooth navigation. With these patterns, you’ll build faster, cleaner apps that scale without painful rewrites.

Top comments (0)