Skip to content

Send a Public Record, Not a Database Row

The Course
•
18 min study

The decision

A Server Component's code never reaches the browser, so it can read a whole database row, private fields included. A Client Component is different: every prop it receives is serialized and sent to the browser in the page's data, whether or not the component displays it. Rendering only item.title does not keep item.internalMemo on the server.

See it happen

In the lab, the page loads a row from lib/library.js and passes the whole thing to ItemCard, a Client Component that shows only the title:

JSX
export default async function Page() {
  const row = await loadRow(1);
  return (
    <main>
      <h1>Send a Public Record, Not a Database Row</h1>
      <ItemCard item={row} />
      <Cases />
    </main>
  );
}

ItemCard has a button that prints the props it received, exactly as they arrived in the browser. Before pressing it, predict what it prints.

Send a Public Record, Not a Database Row Next.js · 7 files
import "./globals.css"; export default function RootLayout({ children }) { return ( <html lang="en"> <body> <header> <strong>Study Library · Practice lab</strong> </header> {children} </body> </html> ); } import Cases from "./Cases.jsx"; import ItemCard from "./ItemCard.jsx"; import { loadRow } from "@/lib/library.js"; export default async function Page() { const row = await loadRow(1); return ( <main> <h1>Send a Public Record, Not a Database Row</h1> <ItemCard item={row} /> <Cases /> </main> ); } import { run, examples } from "@/lib/contract.js"; const text = (value) => JSON.stringify(value) ?? "undefined"; export default async function Cases() { const results = await Promise.all( examples.map(async ([input, expected]) => { // Record the input before running, to show whether run changed it. const before = text(input); const actual = await run(input); return { input: before, expected, actual, kept: text(input) === before, pass: text(actual) === text(expected), }; }), ); return ( <section> <h2>Executable cases</h2> <p> Checks: {results.filter((row) => row.pass).length}/{results.length}{" "} passed </p> <table> <thead> <tr> <th>Input</th> <th>Expected</th> <th>Actual</th> <th>Input kept</th> </tr> </thead> <tbody> {results.map((row, i) => ( <tr key={i}> <td> <code>{row.input}</code> </td> <td> <code>{text(row.expected)}</code> </td> <td className={row.pass ? "pass" : "fail"}> <code>{text(row.actual)}</code> </td> <td className={row.kept ? "pass" : "fail"}> {row.kept ? "yes" : "no: run changed it"} </td> </tr> ))} </tbody> </table> </section> ); } "use client"; import { useState } from "react"; // A client component: it renders only the title, but it receives whatever the // server passes as props. export default function ItemCard({ item }) { const [open, setOpen] = useState(false); return ( <section> <h2>{item.title}</h2> <button onClick={() => setOpen(!open)}> {open ? "Hide" : "Show"} what reached the browser </button> {open && <pre aria-label="Props in the browser">{JSON.stringify(item)}</pre>} </section> ); } body { font: 16px/1.5 system-ui; margin: 0; color: #182b3c; background: #f4f8fc; } header, main { max-width: 900px; margin: auto; padding: 20px; } header { border-bottom: 1px solid #cbd5e1; } a { color: #075985; } button, input, textarea, select { font: inherit; padding: 8px; margin: 4px; } button { cursor: pointer; } table { border-collapse: collapse; width: 100%; } th, td { padding: 10px; border: 1px solid #cbd5e1; text-align: left; vertical-align: top; } pre, code { white-space: pre-wrap; overflow-wrap: anywhere; } .pass { color: #166534; } .fail, [role="alert"] { color: #a01818; } section { margin: 16px 0; padding: 12px; background: white; border-radius: 8px; } nav { display: flex; gap: 16px; } export function run(row) { return { id: row.id, title: row.title }; } export const examples = [ [ { id: 1, title: "Routing", internalMemo: "practice only" }, { id: 1, title: "Routing" }, ], ]; // A pretend database row. internalMemo is for staff only. export async function loadRow(id) { return { id, title: "Routing", done: false, internalMemo: "Reviewer note: practice data only", }; }
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Add and remove files here; write their code in the editor.
Check your prediction

The whole row: {"id":1,"title":"Routing","done":false,"internalMemo":"Reviewer note: practice data only"}. On a real Next.js site the same text is in the page's source and in the data the browser downloads when it navigates, so anyone can read it with devtools.

Send a public record

lib/contract.js builds a new object from an allowlist of public fields:

JavaScript
export function run(row) {
  return { id: row.id, title: row.title };
}

In app/page.jsx, import it with import { run } from "@/lib/contract.js"; and pass item={run(row)}. Run again and press the button: the browser now receives {"id":1,"title":"Routing"}. The case table below the card checks the same function on its own.

The failure to avoid

Removing password from a copied object is a denylist: a later apiKey field can leak. Public output should grow only when the contract intentionally grows.

Suppose a later migration adds apiKey to every row. Trace a row through run, and through a denylist that removes only internalMemo: which one sends the key to the browser?

Exercise

Extend the contract

Include a public done flag as a strict boolean, while keeping every unknown field out of the result.

Edit lib/contract.js, then rerun the original cases. If the requested rule changes an earlier result, update that expected value deliberately. Keep the other cases. Add the acceptance cases to examples so the preview checks them too.

  • Input {"id": 2, "title": "Forms", "done": true, "token": "fixture"} must return {"id": 2, "title": "Forms", "done": true}.
Your working space Next.js · 7 files
import "./globals.css"; export default function RootLayout({ children }) { return ( <html lang="en"> <body> <header> <strong>Study Library · Practice lab</strong> </header> {children} </body> </html> ); } import Cases from "./Cases.jsx"; import ItemCard from "./ItemCard.jsx"; import { loadRow } from "@/lib/library.js"; export default async function Page() { const row = await loadRow(1); return ( <main> <h1>Send a Public Record, Not a Database Row</h1> <ItemCard item={row} /> <Cases /> </main> ); } import { run, examples } from "@/lib/contract.js"; const text = (value) => JSON.stringify(value) ?? "undefined"; export default async function Cases() { const results = await Promise.all( examples.map(async ([input, expected]) => { // Record the input before running, to show whether run changed it. const before = text(input); const actual = await run(input); return { input: before, expected, actual, kept: text(input) === before, pass: text(actual) === text(expected), }; }), ); return ( <section> <h2>Executable cases</h2> <p> Checks: {results.filter((row) => row.pass).length}/{results.length}{" "} passed </p> <table> <thead> <tr> <th>Input</th> <th>Expected</th> <th>Actual</th> <th>Input kept</th> </tr> </thead> <tbody> {results.map((row, i) => ( <tr key={i}> <td> <code>{row.input}</code> </td> <td> <code>{text(row.expected)}</code> </td> <td className={row.pass ? "pass" : "fail"}> <code>{text(row.actual)}</code> </td> <td className={row.kept ? "pass" : "fail"}> {row.kept ? "yes" : "no: run changed it"} </td> </tr> ))} </tbody> </table> </section> ); } "use client"; import { useState } from "react"; // A client component: it renders only the title, but it receives whatever the // server passes as props. export default function ItemCard({ item }) { const [open, setOpen] = useState(false); return ( <section> <h2>{item.title}</h2> <button onClick={() => setOpen(!open)}> {open ? "Hide" : "Show"} what reached the browser </button> {open && <pre aria-label="Props in the browser">{JSON.stringify(item)}</pre>} </section> ); } body { font: 16px/1.5 system-ui; margin: 0; color: #182b3c; background: #f4f8fc; } header, main { max-width: 900px; margin: auto; padding: 20px; } header { border-bottom: 1px solid #cbd5e1; } a { color: #075985; } button, input, textarea, select { font: inherit; padding: 8px; margin: 4px; } button { cursor: pointer; } table { border-collapse: collapse; width: 100%; } th, td { padding: 10px; border: 1px solid #cbd5e1; text-align: left; vertical-align: top; } pre, code { white-space: pre-wrap; overflow-wrap: anywhere; } .pass { color: #166534; } .fail, [role="alert"] { color: #a01818; } section { margin: 16px 0; padding: 12px; background: white; border-radius: 8px; } nav { display: flex; gap: 16px; } export function run(row) { return { id: row.id, title: row.title }; } export const examples = [ [ { id: 1, title: "Routing", internalMemo: "practice only" }, { id: 1, title: "Routing" }, ], ]; // A pretend database row. internalMemo is for staff only. export async function loadRow(id) { return { id, title: "Routing", done: false, internalMemo: "Reviewer note: practice data only", }; }
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Add and remove files here; write their code in the editor.
Hint 1: the idea

The public record grows only when you name a field. And a public field needs a definite type, so the client never has to guess what a missing value means.

Hint 2: a first step

List the added output field explicitly. Decide what a missing flag means.

Show one solution

Replace the exported run function and any constants shown here; keep the examples export underneath it. Compare the result with your own approach.

JavaScript
export function run(row) {
  return { id: row.id, title: row.title, done: row.done === true };
}

row.done === true turns anything that isn't exactly true (a missing field, "yes", 1) into false, so the client always gets a boolean. Writing done: row.done would pass the acceptance case but send nothing at all for old rows without the field, because JSON drops undefined. token stays out because nobody named it.

Explain it without the code

List the fields of a Library item you'd let a Client Component receive, and one you'd keep on the server. Write what decides it.

Going further

Two tools catch this mistake earlier on a real project. Importing the server-only package at the top of lib/library.js makes the build fail if a Client Component ever imports the database module. React's experimental taint APIs (enabled with experimental.taint in next.config.js) can mark a whole row object so that passing it to a Client Component throws. Neither replaces the allowlist: they stop the row, the projection decides what goes instead.

Read the Next.js guide for the framework boundary around this rule. The preview runs code-playpad's Next.js simulation. Real caching, hydration, authentication and persistence require the real-server checks described on Day 7.

Was this article helpful?

Your response is saved on this device.