Next.js Server or Client? The Decision
Every piece of a page belongs on one side. Ask what the piece does, and the side picks itself.
Ask the one question
The script lists five pieces of the Bookshelf app. Each piece names its side.
const pieces = ["Book list", "Search box", "Mark-read button", "Book page", "Read count"];
console.log("Pieces to place:", pieces.length);The rule, in one line
The rule fits in one line. Clicks and typing move a piece to the client, and the rest stays on the server.
console.log("Clicks or typing: client. Everything else: server.");Example
const picks = [["Book list", "server"], ["Search box", "client"], ["Mark-read button", "client"], ["Book page", "server"], ["Read count", "server"]];
console.log("Five pieces checked:");
picks.forEach(([piece, side]) => console.log(piece + ": " + side));Key points
- Ask first: does this piece need a click, typing, or a browser tool?
- A piece that reads data belongs on the server. It has no click to answer.
- A piece that answers a click belongs on the client. The browser holds the click.
- A piece that holds changing text belongs on the client. State lives in the browser.
- The book list reads data, so it is a server piece. The search box holds text, so it is a client leaf.
- The read count is a number from the data, so it is a server piece.
In this example, five pieces of the Bookshelf app each get a side. The book list is server, because it only reads data. The search box is client, because it holds typed text. The mark-read button is client, because it answers a click. The book page is server, because it only shows one book. The read count is server, because it is a number from the data. Five pieces, three server and two client, and each side follows from what the piece does.
Note
When in doubt, keep the piece on the server. Move it only when a click or typing forces the move.
Try it Yourself
Edit the code, then press Run.
Running is simulated · TypeScript Playground
Exercise
Which piece belongs on the client?