NestJS REST API Best Practices That Scale
Module boundaries, DTO validation, guards, exception filters and configuration: the NestJS structure that stays clean as your API grows.
The App Router changed how Next.js apps are structured. After building several production sites with it, here is what actually matters day to day.
Every component in the app folder is a server component unless you add "use client" at the top. Server components run only on the server, so they can read from a database, call private APIs and ship zero JavaScript to the browser.
Use a client component only when you need state, effects, event handlers or browser APIs. Keep those leaves small, and keep the data fetching in the server parent.
// app/projects/page.tsx (server component)
export default async function Projects() {
const projects = await getProjects();
return <ProjectGrid projects={projects} />; // ProjectGrid can be a client component
}
A layout.tsx wraps its children and does not re-render when you navigate between sibling pages. Put your header, footer and providers there. Nested layouts let a dashboard have its own shell without touching the public site.
Add loading.tsx for instant skeletons while a segment streams, and error.tsx to catch failures in that segment. Be careful with loading.tsx on pages that call notFound(): streaming starts before the status code is known, which can turn a missing page into a soft 404.
Data fetched on the server can be cached and revalidated by time or on demand.
export const revalidate = 60 rebuilds a page at most once a minute.revalidatePath or revalidateTag after you change content, for example from an admin dashboard.This gives you static speed with fresh content, which is ideal for blogs and portfolios.
Export generateMetadata from a page to set the title, description, canonical URL and Open Graph image from the data you already loaded. Add JSON-LD for articles and you have the technical SEO basics covered.
NEXT_PUBLIC_ prefix are server only.Think server first, push interactivity to small client leaves, and use revalidation instead of fetching on every request. Your pages get faster and your bundles get smaller.
Module boundaries, DTO validation, guards, exception filters and configuration: the NestJS structure that stays clean as your API grows.
Stop guessing. Measure first, then apply the React optimizations that move the needle: memoization, code splitting, list rendering and more.
Typing props, hooks, events and API data with TypeScript, plus utility types and patterns that remove whole classes of bugs.