A record page and its title should agree about which record exists. Build metadata from public fields and keep missing-record behaviour explicit; metadata is not an authorization boundary.
Read, predict, run
The function builds the title and description from a public record. This lab tests the pure metadata builder. In a real route, call the same public lookup from generateMetadata and the page. Wrap that lookup in React's cache() (the previous lesson) so the two calls in one request read the database once:
JavaScript
export function run(record) { if (!record) return { title: "Not found" }; return { title: record.title + " · Study Library", description: "Study " + record.title, };}
Before pressing Run, predict what run returns for null and {"title": "Routing"}, from the
code above. Write your answers down first: after Run, the case table shows the
expected values beside the actual ones.
Metadata Uses the Same Public ContractNext.js · 5 filesimport "./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";
export default function Page() {
return (
<main>
<h1>Metadata Uses the Same Public Contract</h1>
<p>
Read the input and predict the result before running. Change
lib/contract.js for the exercise.
</p>
<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>
);
}
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(record) {
if (!record) return { title: "Not found" };
return {
title: record.title + " · Study Library",
description: "Study " + record.title,
};
}
export const examples = [
[null, { title: "Not found" }],
[
{ title: "Routing" },
{ title: "Routing · Study Library", description: "Study Routing" },
],
];
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
The case table computes these results on the server side of the simulation.
It compares values; it does not prove a production deployment or database works.
The failure to avoid
Generating a title from an internal memo leaks it outside the body too. A generic page fallback should not conceal a failed database request as a missing record.
Trace null through run. Where does that null come from in a real route,
and what should the page itself do with the same lookup result?
Exercise
Extend the contract
Cap the generated description at 80 characters while leaving the full page title intact.
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 {"title": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"} must return {"title": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx · Study Library", "description": "Study xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}.
Your working spaceNext.js · 5 filesimport "./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";
export default function Page() {
return (
<main>
<h1>Metadata Uses the Same Public Contract</h1>
<p>
Read the input and predict the result before running. Change
lib/contract.js for the exercise.
</p>
<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>
);
}
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(record) {
if (!record) return { title: "Not found" };
return {
title: record.title + " · Study Library",
description: "Study " + record.title,
};
}
export const examples = [
[null, { title: "Not found" }],
[
{ title: "Routing" },
{ title: "Routing · Study Library", description: "Study Routing" },
],
];
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Hint 1: the idea
A display limit belongs to one output, the description shown in search
results. It isn't a rule about the record or about the page title.
Hint 2: a first step
Only the description has this display limit; do not mutate the record.
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(record) { if (!record) return { title: "Not found" }; return { title: record.title + " · Study Library", description: ("Study " + record.title).slice(0, 80), };}
slice(0, 80) runs on the finished string, so the word Study counts towards
the 80 and the description is never longer, whatever the title. The title is
left whole because it's the page's name. record.title itself is never changed,
so the page body built from the same record still shows the full title.
Explain it without the code
Write why metadata counts as public output even when the page body is
protected, and which record fields it may use.
Going further
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.
Completion is saved in this browser only. It does not measure mastery.