Next.js Cheat Sheet
Create and run
npx create-next-app@latest my-app
npm run dev # http://localhost:3000
npm run build # production build
npm start # serve the build
Routing files
app/page.js # /
app/about/page.js # /about
app/blog/[slug]/page.js # dynamic segment
app/(group)/x/page.js # route group, not in URL
app/api/x/route.js # API endpoint
layout.js loading.js error.js not-found.js
Components
'use client' // make a Client Component
import Link from 'next/link' // <Link href="/x">
import Image from 'next/image' // <Image src alt width height />
import { useRouter } from 'next/navigation' // router.push('/x')
import { notFound, redirect } from 'next/navigation'
Data
await fetch(url, { cache: 'force-cache' }) // cache
await fetch(url, { next: { revalidate: 60 } }) // ISR
export const dynamic = 'force-dynamic' // always SSR
export async function generateStaticParams() {} // pre-render params
revalidatePath('/notes'); revalidateTag('notes') // on-demand refresh
Server Actions and route handlers
async function save(formData) { 'use server' } // <form action={save}>
export async function GET(req) { return Response.json({}) }
export async function POST(req) { const b = await req.json() }
Config and env
export const metadata = { title: 'Page' } // SEO
NEXT_PUBLIC_API_URL=https://... // browser-visible env
images: { remotePatterns: [{ hostname: 'x.com' }] } // next.config.mjs
Full Next.js documentation