{"success":true,"name":"in-next-js-decide-where-each-component-runs-server-by-defaul","description":"Writing Next.js components without choosing server vs client rendering on purpose","category":"ai-brimind","path":"ai-brimind/in-next-js-decide-where-each-component-runs-server-by-defaul/SKILL.md","file":"SKILL.md","content":"---\nname: in-next-js-decide-where-each-component-runs-server-by-defaul\ndescription: \"Writing Next.js components without choosing server vs client rendering on purpose\"\n---\n\n# In Next.js, Decide Where Each Component Runs: Server by Default, Client Only for Interactivity\n\n## When to use\nProject is Next.js (App Router), and components are being written by copying whatever pattern is nearby — 'use client' slapped on top to silence an error, or data fetched via useEffect in components that never needed to be client-side at all.\n\n## Example request\n'use client' added to make a build error disappear → the error was pointing at a real decision (this component uses browser-only features), and the directive quietly moved the whole subtree to the browser bundle; silencing it without understanding is choosing without knowing. A page fetches its list with useEffect + useState + loading spinner → in the App Router, a server component could fetch during render and ship finished HTML — faster, less code, no spinner for the first paint; the useEffect-fetch pattern is often imported muscle memory from older React. One onClick somewhere makes the developer mark the ENTIRE page 'use client' → the boundary is placed too high; extracting the interactive button into a small client component keeps the heavy page on the server. A genuinely interactive widget (form, drag-drop, live filter) → belongs client-side by nature; the discipline is not 'server always', it is 'each piece where it earns its keep'.\n\n## Instructions\nAct as a Next.js developer who knows the App Router asks one architectural question about every component — 'does this run on the server or in the user's browser?' — and answers it with a default you should respect: everything is a server component until something forces it client-side, because server components ship HTML instead of JavaScript, keep secrets and DB access naturally out of the browser, and cost the user's device nothing. For each component you write or touch, do this: (1) Ask what the component actually DOES: renders data → server, and fetch that data directly in the component (await your DB call or fetch during render) instead of the useEffect/useState/loading-spinner dance, which is a client-side pattern solving a problem the server version does not have. Uses state, effects, event handlers, or browser APIs (onClick, useState, localStorage, window) → that specific piece is client territory. (2) Place the 'use client' boundary as LOW in the tree as possible: not on the page because one button needs onClick, but on the button — extract the interactive island (the form, the like button, the dropdown) into its own small client component and keep the surrounding page server-rendered. Every 'use client' marks the top of a subtree that entirely joins the browser bundle, so a high boundary silently drags everything under it client-side. (3) Never add 'use client' to silence an error without translating the error first: 'useState in a server component' means 'you put interactivity in server territory' — the fix is deciding (move the state down into a client island, or accept the component is truly interactive), not directive-sprinkling until the build passes. (4) Respect what the boundary means for data and secrets: server components may touch the DB and read private env; client components receive data via props (serializable only) and see only what you deliberately send — which connects to your response-shaping discipline: crossing the boundary IS an API surface, choose the fields. (5) Verify the split honestly: check which components carry 'use client' and ask for each 'what interactivity earns this?' — and glance at what the page ships (view-source shows real HTML for server-rendered content). Only call the page architected once every 'use client' has a named justification and data fetching happens server-side wherever no interactivity demanded otherwise — a Next.js app where everything is a client component is paying for a server framework to build a plain browser app, the exact bill it was designed to tear up.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #229\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}