Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply Next.js best practices for RSC boundaries, async APIs, routing, metadata, and optimization.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
file-conventions.md
1# File Conventions23Next.js App Router uses file-based routing with special file conventions.45## Project Structure67Reference: https://nextjs.org/docs/app/getting-started/project-structure89```10app/11├── layout.tsx # Root layout (required)12├── page.tsx # Home page (/)13├── loading.tsx # Loading UI14├── error.tsx # Error UI15├── not-found.tsx # 404 UI16├── global-error.tsx # Global error UI17├── route.ts # API endpoint18├── template.tsx # Re-rendered layout19├── default.tsx # Parallel route fallback20├── blog/21│ ├── page.tsx # /blog22│ └── [slug]/23│ └── page.tsx # /blog/:slug24└── (group)/ # Route group (no URL impact)25└── page.tsx26```2728## Special Files2930| File | Purpose |31|------|---------|32| `page.tsx` | UI for a route segment |33| `layout.tsx` | Shared UI for segment and children |34| `loading.tsx` | Loading UI (Suspense boundary) |35| `error.tsx` | Error UI (Error boundary) |36| `not-found.tsx` | 404 UI |37| `route.ts` | API endpoint |38| `template.tsx` | Like layout but re-renders on navigation |39| `default.tsx` | Fallback for parallel routes |4041## Route Segments4243```44app/45├── blog/ # Static segment: /blog46├── [slug]/ # Dynamic segment: /:slug47├── [...slug]/ # Catch-all: /a/b/c48├── [[...slug]]/ # Optional catch-all: / or /a/b/c49└── (marketing)/ # Route group (ignored in URL)50```5152## Parallel Routes5354```55app/56├── @analytics/57│ └── page.tsx58├── @sidebar/59│ └── page.tsx60└── layout.tsx # Receives { analytics, sidebar } as props61```6263## Intercepting Routes6465```66app/67├── feed/68│ └── page.tsx69├── @modal/70│ └── (.)photo/[id]/ # Intercepts /photo/[id] from /feed71│ └── page.tsx72└── photo/[id]/73└── page.tsx74```7576Conventions:77- `(.)` - same level78- `(..)` - one level up79- `(..)(..)` - two levels up80- `(...)` - from root8182## Private Folders8384```85app/86├── _components/ # Private folder (not a route)87│ └── Button.tsx88└── page.tsx89```9091Prefix with `_` to exclude from routing.9293## Middleware / Proxy9495### Next.js 14-15: `middleware.ts`9697```ts98// middleware.ts (root of project)99import { NextResponse } from 'next/server';100import type { NextRequest } from 'next/server';101102export function middleware(request: NextRequest) {103// Auth, redirects, rewrites, etc.104return NextResponse.next();105}106107export const config = {108matcher: ['/dashboard/:path*', '/api/:path*'],109};110```111112### Next.js 16+: `proxy.ts`113114Renamed for clarity - same capabilities, different names:115116```ts117// proxy.ts (root of project)118import { NextResponse } from 'next/server';119import type { NextRequest } from 'next/server';120121export function proxy(request: NextRequest) {122// Same logic as middleware123return NextResponse.next();124}125126export const config = {127matcher: ['/dashboard/:path*', '/api/:path*'],128};129```130131| Version | File | Export | Config |132|---------|------|--------|--------|133| v14-15 | `middleware.ts` | `middleware()` | `config` |134| v16+ | `proxy.ts` | `proxy()` | `config` |135136**Migration**: Run `npx @next/codemod@latest upgrade` to auto-rename.137138## File Conventions Reference139140Reference: https://nextjs.org/docs/app/api-reference/file-conventions