Skip to content

d #1

@brewblue

Description

@brewblue

Routing System

Nextjs is a fullstack app solution. There are two categories of routing

  • Restful API routing
  • Page routing

Next.js App Directory File Naming Patterns

Essential file naming conventions in Next.js 14+ app directory.

checklist


File Description
page.tsx Default React Server Component for route segments. Defines unique UI and data fetching. Automatically accessible at route's URL.
layout.tsx Shared UI wrapper for route segments. Persists across route changes. Receives children prop for nested components.
loading.tsx Instant loading state using React Suspense. Automatically wraps route segments. Shows while page components are streaming.
error.tsx React Error Boundary wrapper. Catches runtime errors in nested routes. Isolates errors to affected segments.
not-found.tsx Custom 404 error handling. Invoked by notFound() function. Handles non-existent routes and intentional not-found states.
route.tsx Route Handlers for API functionality. Supports HTTP methods (GET, POST, etc.). Direct access to Next.js's Edge Runtime.
middleware.ts Global route and request interception. JWT validation, authentication, and rewriting. Executes before matching routes.
route.ts API Route Handler defining endpoints with HTTP methods (GET, POST, etc.). Creates API routes at the segment's URL path. Must be placed in app directory.
default.tsx app/@modal/default.tsx , Fallback UI when parallel route isn't active
template.tsx app/blog/template.tsx, New instance per navigation, useful for animations

Deep dive in.

  • template, page and layout
app/
  ├─ layout.tsx (root)
  └─ products/
      ├─ layout.tsx 
      ├─ template.tsx
      ├─ page.tsx
      └─ [id]/
          └─ page.tsx

So these UI layers wrapped like onion, root layout waps the products page layout, the products page layout wraps the template, then template wraps the further page.tsx.

The final effect is like an onion

Image

  • Folder structure

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions