App Router file conventions
Special file names inside app/ control routing, layout, and loading/error boundaries automatically — no manual route config.
app/
layout.tsxshared shell — wraps every page below it
page.tsxthe / route
loading.tsxSuspense fallback while page.tsx loads
error.tsxerror boundary (must be a Client Component)
not-found.tsxrendered on notFound() or unmatched route
dashboard/
page.tsx/dashboard
[id]/
page.tsx/123 — dynamic segment, params.id
Server vs. Client Components
Every component is a Server Component by default. Add 'use client' only when you need interactivity or browser-only APIs.
| Need | Use |
|---|---|
| Fetch data, read env vars, query a database | Server Component (default) |
| useState, useEffect, event handlers | Client Component ('use client') |
| Browser APIs (localStorage, window) | Client Component |
| Rendering a large static tree | Server Component — ships zero JS for it |
Tip
Push
'use client' as far down the tree as possible. A small interactive island inside a Server Component shell ships far less JavaScript than marking the whole page client-side.Core hooks
| Hook | Use for |
|---|---|
useState | Local component state. |
useEffect | Syncing with an external system (subscriptions, DOM APIs) — not for fetching data. |
useEffectEvent | Non-reactive logic extracted out of an effect (React 19.2+). |
useMemo / useCallback | Memoize an expensive value or a stable function reference. |
useRef | Mutable value that doesn't trigger a re-render; DOM node references. |
useContext | Read a value from a Context provider without prop drilling. |
useTransition | Mark a state update as non-urgent so the UI stays responsive. |
useActionState | Wire a form to a Server Action and read its pending/result state. |
Data fetching
Fetch directly inside async Server Components — no client-side loading state needed for the initial render.
app/posts/page.tsx
export default async function PostsPage() {
const posts = await getPosts()
return (
<ul>
{posts.map((post) => (
<li key={post.id}>{post.title}</li>
))}
</ul>
)
}Need to fetch inside a Client Component instead? Use SWR rather than fetching in useEffect — it handles caching, revalidation, and race conditions for you.
Client Component with SWR
'use client'
import useSWR from 'swr'
const fetcher = (url: string) => fetch(url).then((r) => r.json())
export function Profile() {
const { data, isLoading } = useSWR('/api/user', fetcher)
if (isLoading) return <Spinner />
return <p>{data.name}</p>
}Rendering strategies
| Strategy | When it runs | Good for |
|---|---|---|
| Static (default) | Build time | Marketing pages, docs — content that rarely changes. |
| Dynamic | Every request | Personalized or request-dependent content (cookies, headers). |
| Incremental (ISR) | Build time + background revalidation | Content that changes occasionally — product pages, blog posts. |
'use cache' | Cached per compiler-generated key | Explicit, granular caching of a page, component, or function. |
Common patterns
- Mutate data with a Server Action (
'use server') instead of a client-side POST to an API route when the caller is a form in your own app. - Use
revalidateTag()orrevalidatePath()after a mutation so cached reads reflect the change. - Co-locate a route's components, tests, and styles in the same folder — only
page.tsx,layout.tsx, etc. are routable. - Use
next/linkfor internal navigation, never a plainatag — it prefetches and avoids full page reloads. - Read
paramsandsearchParamswithawait— they're asynchronous as of Next.js 15+.
