Skip to content

Menu

Next.js App Router Cheat Sheet: Files, Caching, Route Handlers

Checked against Next.js Next.js 16.4.6 (latest on npm); notes for 15.5, which this site runs (15.5.29) on 28 November 2026. Each section links to the official docs it was checked against.

This sheet is for developers building with the Next.js App Router who want the file names and caching APIs on one page. It follows the nextjs.org docs for Next.js 16 (16.4.6 was the latest release on 28 Nov 2026) and flags where Next.js 15 differs, since plenty of apps, including this one on 15.5.29, have not moved yet. Rows are from the docs and were not run in a fresh app.

When to use what

Leave components as Server Components unless they need state, event handlers, effects or browser APIs. Put 'use client' on the smallest interactive piece, such as a search box, not on a whole layout, because everything that file imports joins the client bundle. You can still pass Server Components into a client wrapper as children.

Caching is the part that changed most. In Next.js 15 without Cache Components, fetch is not cached by default; you opt in with cache: 'force-cache' or next: { revalidate } and tune routes with export const dynamic and revalidate. In Next.js 16 you can turn on cacheComponents, and then nothing is cached unless you mark it with 'use cache' plus a cacheLife profile. Uncached reads must sit inside <Suspense> so the rest of the page can still be prerendered. New apps from create-next-app have it on already. I would turn it on for new projects and migrate older ones route by route, because the mental model is simpler once you are there.

After a mutation, use updateTag() in a Server Action when the user must see their own change right away, and revalidateTag(tag, 'max') when stale-while-revalidate is fine. Use a route.ts handler for webhooks and endpoints other clients call, and Server Actions for your own forms. Our caching strategies post, Server Actions guide and Next.js 15 migration guide go further.

A folder becomes a public route only when it has a page or route file. In Next.js 16 every parallel route slot needs a default.js.

app/layout.tsx

Root layout that wraps every route; must render <html> and <body>

From the docs, not run by us
page.tsx

UI for a route segment and the file that makes the URL public

From the docs, not run by us
loading.tsx

Suspense fallback shown while the segment loads

From the docs, not run by us
error.tsx / global-error.tsx

Error boundary for a segment, and one for the root layout

From the docs, not run by us
not-found.tsx

UI rendered when notFound() is called or no route matches

From the docs, not run by us
route.ts

HTTP endpoint; cannot sit in the same segment as a page

From the docs, not run by us
template.tsx

Like a layout but re-mounted on every navigation

From the docs, not run by us
default.tsx

Fallback for a parallel route slot that has no match

From the docs, not run by us
proxy.ts

Request interception at the project root; replaces middleware.ts in Next.js 16

From the docs, not run by us
app/blog/[slug]/page.tsx

One dynamic segment: /blog/my-first-post

From the docs, not run by us
app/shop/[...slug]/page.tsx

Catch-all: /shop/a and /shop/a/b, but not /shop

From the docs, not run by us
app/docs/[[...slug]]/page.tsx

Optional catch-all that also matches /docs

From the docs, not run by us
app/(marketing)/page.tsx

Route group: organizes files and layouts without adding to the URL

From the docs, not run by us
app/blog/_components/

Private folder that the router ignores

From the docs, not run by us
app/@sidebar/page.tsx

Parallel route slot passed to the parent layout as a prop

From the docs, not run by us
app/feed/(..)photo/[id]/page.tsx

Intercepting route: show a parent-level route, for example as a modal

From the docs, not run by us
'use client'

First line of a file: marks a client boundary; its imports join the client bundle

From the docs, not run by us
<Modal><Cart /></Modal>

Pass a Server Component as children of a Client Component

From the docs, not run by us
import 'server-only'

Build error if a module with secrets is imported into client code

From the docs, not run by us
NEXT_PUBLIC_API_URL

Only NEXT_PUBLIC_ variables reach the browser; others become empty strings there

Try it: Env File GeneratorFrom the docs, not run by us
const { id } = await params

Next.js 15 and 16: params and searchParams are promises

From the docs, not run by us
const store = await cookies()

cookies(), headers() and draftMode() are async; sync access is removed in 16

From the docs, not run by us

The previous model (no Cache Components) is documented at https://nextjs.org/docs/app/guides/caching-without-cache-components.

cacheComponents: true, partialPrefetching: true

next.config.ts options that turn on the Next.js 16 caching model

From the docs, not run by us
'use cache'; cacheLife('hours')

Cache a function, component or page with a named lifetime profile

From the docs, not run by us
cacheTag('posts')

Tag cached output so you can invalidate it later

From the docs, not run by us
revalidateTag('posts', 'max')

Mark tagged entries stale and refresh them in the background (Next.js 16 signature)

From the docs, not run by us
updateTag(`user-${id}`)

Server Actions only: expire a tag and read fresh data in the same request

From the docs, not run by us
revalidatePath('/profile')

Invalidate cached data for one route path

From the docs, not run by us
await connection()

Defer a component to request time before using random values or the current time

From the docs, not run by us
fetch(url, { cache: 'force-cache' })

Previous model: opt one request into caching; fetch is uncached by default

From the docs, not run by us
fetch(url, { next: { revalidate: 3600, tags: ['user'] } })

Previous model: cache for an hour and tag for on-demand invalidation

From the docs, not run by us
export const dynamic = 'force-dynamic'

Previous model: render the route on every request

From the docs, not run by us
export const revalidate = 600

Previous model: route-wide revalidation in seconds; must be a literal

From the docs, not run by us

Metadata rules are from https://nextjs.org/docs/app/getting-started/metadata-and-og-images; static params from https://nextjs.org/docs/app/api-reference/functions/generate-static-params.

export async function GET(request: Request) {}

Route handler; GET, POST, PUT, PATCH, DELETE, HEAD and OPTIONS are supported

From the docs, not run by us
ctx: RouteContext<'/users/[id]'>

Typed context for a handler; read await ctx.params

From the docs, not run by us
export const metadata: Metadata = { title, description }

Static metadata from a layout or page; Server Components only

Try it: Meta Tag GeneratorFrom the docs, not run by us
export async function generateMetadata({ params })

Metadata that depends on data; wrap shared queries in React cache to fetch once

Try it: seo-meta-analyzerFrom the docs, not run by us
app/opengraph-image.tsx

Generate a social image with ImageResponse from next/og

From the docs, not run by us
export async function generateStaticParams() { return [{ slug: 'a' }] }

Prerender listed dynamic paths at build time

From the docs, not run by us
export const dynamicParams = false

Return 404 for paths not listed; only without Cache Components

From the docs, not run by us
return []

Previous model: prerender every path on first visit; with Cache Components an empty list is a build error

From the docs, not run by us

Worked example: Cache a blog list and refresh it from a Server Action (Next.js 16)

With cacheComponents: true, this page puts the post list in the static shell, tags it, and refreshes it when an editor publishes:

// app/blog/page.tsx
import { cacheLife, cacheTag } from 'next/cache'

async function getPosts() {
  'use cache'
  cacheLife('hours')
  cacheTag('posts')
  return db.post.findMany({ orderBy: { createdAt: 'desc' } })
}

export default async function BlogPage() {
  const posts = await getPosts()
  return <ul>{posts.map((p) => <li key={p.id}>{p.title}</li>)}</ul>
}
// app/blog/actions.ts
'use server'
import { updateTag } from 'next/cache'

export async function publish(formData: FormData) {
  await db.post.create({ data: { title: String(formData.get('title')) } })
  updateTag('posts')
}

At build time getPosts runs once and its result goes into the prerendered HTML, so visitors get the list without a database round trip. The hours profile lets Next.js refresh it in the background over time. When publish runs, updateTag('posts') expires that entry and the editor's next render reads fresh data in the same request, so they see their post immediately. If you only need other visitors to catch up eventually, revalidateTag('posts', 'max') is the cheaper choice. On Next.js 15 without Cache Components, the equivalent is unstable_cache with tags: ['posts'] and revalidateTag('posts'). This is from the docs; we did not build it in a fresh app.

Common mistakes

  • Putting 'use client' at the top of a layout to make one button interactive.

    Move the button into its own file with the directive. The layout stays a Server Component and ships less JavaScript.

  • Reading params.slug directly after upgrading to Next.js 15 or 16.

    params is a promise. Use const { slug } = await params, or run the upgrade codemod with npx @next/codemod@canary upgrade latest.

  • Calling revalidateTag('posts') with one argument on Next.js 16.

    The single-argument form is deprecated. Pass a profile such as 'max', or use updateTag inside a Server Action for read-your-writes.

  • Adding a route.ts next to a page.tsx in the same folder.

    They conflict. Put the handler under a separate segment such as app/api/items/route.ts.

  • Returning an empty array from generateStaticParams after turning on Cache Components.

    That is now a build error. Return at least one real param, or follow the ISR with Cache Components guide for unknown params.

  • Keeping middleware.ts after upgrading to 16 and expecting the Node runtime.

    Rename it to proxy.ts and the export to proxy. The old file still works for Edge but is deprecated.