Tracking parameters and temporary client state should not create a new canonical page identity. Decide which public query values actually define the content and encode them deliberately.
Read, predict, run
The model constructs a canonical URL under a fixed trusted origin and accepts only one tag parameter. It does not trust a Host header or user-supplied origin. In Next.js, metadataBase and alternates.canonical express the actual metadata:
JavaScript
export function run({ tag }) { const url = new URL("/reviews", "https://example.test"); if (typeof tag === "string" && tag) url.searchParams.set("tag", tag); return url.toString();}
Before pressing Run, predict what run returns for {"tag": "a & b"}, from the
code above. Write your answers down first: after Run, the case table shows the
expected values beside the actual ones.
Canonical URLs Need an AllowlistNext.js · 6 filesimport "./globals.css";
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<header>
<strong>Review Queue · Practice lab</strong>
</header>
{children}
</body>
</html>
);
}
import Link from "next/link";
import Cases from "./Cases.jsx";
export default function Page() {
return (
<main>
<h1>Canonical URLs Need an Allowlist</h1>
<p>
app/reviews/page.jsx builds its canonical URL with run from
lib/contract.js. <Link href="/reviews?tag=react&utm_source=mail">Open the reviews page</Link>
</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;
}
import Link from "next/link";
import { run } from "@/lib/contract.js";
// The canonical URL comes from run, never from the request's Host header or
// from the raw query string.
export async function generateMetadata({ searchParams }) {
const { tag } = await searchParams;
return {
title: "Reviews",
alternates: { canonical: run({ tag }) },
};
}
export default async function Reviews({ searchParams }) {
const { tag } = await searchParams;
return (
<main>
<h1>Reviews</h1>
<p>Showing {typeof tag === "string" && tag ? `tag “${tag}”` : "every tag"}.</p>
{/* Real Next.js puts this in <link rel="canonical">; the simulator
applies the title only, so the page prints it too. */}
<p>Canonical URL: {run({ tag })}</p>
<p>
<Link href="/reviews?tag=react&utm_source=mail">React, from a newsletter</Link>{" "}
· <Link href="/reviews?tag=nonsense">An unknown tag</Link> ·{" "}
<Link href="/">Back</Link>
</p>
</main>
);
}
export function run({ tag }) {
const url = new URL("/reviews", "https://example.test");
if (typeof tag === "string" && tag) url.searchParams.set("tag", tag);
return url.toString();
}
export const examples = [
[{ tag: "a & b" }, "https://example.test/reviews?tag=a+%26+b"],
];
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Check your prediction
Input {"tag": "a & b"} → "https://example.test/reviews?tag=a+%26+b".
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
Reflecting an untrusted host can point search engines at another site. Removing every query parameter is also wrong when a parameter selects a genuinely different public resource.
Compare building the canonical URL from the request's Host header with a fixed
origin. A request sent with Host: evil.example gets
https://evil.example/reviews as its canonical, and a crawler may believe it.
The fixed origin always says https://example.test/reviews.
Wire it into metadata
In the App Router, a page declares its canonical URL in generateMetadata, and
Next.js renders it as <link rel="canonical"> in the page's head. The lab's
app/reviews/page.jsx takes it from run:
JavaScript
export async function generateMetadata({ searchParams }) { const { tag } = await searchParams; return { title: "Reviews", alternates: { canonical: run({ tag }) }, };}
Follow Open the reviews page, which arrives with
?tag=react&utm_source=mail, then An unknown tag. Predict the canonical
URL for each before you look.
Canonical URLs in generateMetadataNext.js · 6 filesimport "./globals.css";
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<header>
<strong>Review Queue · Practice lab</strong>
</header>
{children}
</body>
</html>
);
}
import Link from "next/link";
import Cases from "./Cases.jsx";
export default function Page() {
return (
<main>
<h1>Canonical URLs Need an Allowlist</h1>
<p>
app/reviews/page.jsx builds its canonical URL with run from
lib/contract.js. <Link href="/reviews?tag=react&utm_source=mail">Open the reviews page</Link>
</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;
}
import Link from "next/link";
import { run } from "@/lib/contract.js";
// The canonical URL comes from run, never from the request's Host header or
// from the raw query string.
export async function generateMetadata({ searchParams }) {
const { tag } = await searchParams;
return {
title: "Reviews",
alternates: { canonical: run({ tag }) },
};
}
export default async function Reviews({ searchParams }) {
const { tag } = await searchParams;
return (
<main>
<h1>Reviews</h1>
<p>Showing {typeof tag === "string" && tag ? `tag “${tag}”` : "every tag"}.</p>
{/* Real Next.js puts this in <link rel="canonical">; the simulator
applies the title only, so the page prints it too. */}
<p>Canonical URL: {run({ tag })}</p>
<p>
<Link href="/reviews?tag=react&utm_source=mail">React, from a newsletter</Link>{" "}
· <Link href="/reviews?tag=nonsense">An unknown tag</Link> ·{" "}
<Link href="/">Back</Link>
</p>
</main>
);
}
export function run({ tag }) {
const url = new URL("/reviews", "https://example.test");
if (typeof tag === "string" && tag) url.searchParams.set("tag", tag);
return url.toString();
}
export const examples = [
[{ tag: "a & b" }, "https://example.test/reviews?tag=a+%26+b"],
];
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Check your prediction
The newsletter link gives https://example.test/reviews?tag=react: the
tracking parameter never reaches the canonical, because run copies only
tag. The unknown tag gives https://example.test/reviews?tag=nonsense, so
every invented tag still becomes its own "page" for a search engine. The
exercise closes that gap with an allowlist.
The simulator applies the metadata title but doesn't render the canonical
link, so the page prints the URL as well. In real Next.js, check it with
View source or curl.
Exercise
Extend the contract
Accept only the public tags react and next; omit other tag values from the canonical URL.
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 {"tag": "private"} must return "https://example.test/reviews".
Your working spaceNext.js · 6 filesimport "./globals.css";
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<header>
<strong>Review Queue · Practice lab</strong>
</header>
{children}
</body>
</html>
);
}
import Link from "next/link";
import Cases from "./Cases.jsx";
export default function Page() {
return (
<main>
<h1>Canonical URLs Need an Allowlist</h1>
<p>
app/reviews/page.jsx builds its canonical URL with run from
lib/contract.js. <Link href="/reviews?tag=react&utm_source=mail">Open the reviews page</Link>
</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;
}
import Link from "next/link";
import { run } from "@/lib/contract.js";
// The canonical URL comes from run, never from the request's Host header or
// from the raw query string.
export async function generateMetadata({ searchParams }) {
const { tag } = await searchParams;
return {
title: "Reviews",
alternates: { canonical: run({ tag }) },
};
}
export default async function Reviews({ searchParams }) {
const { tag } = await searchParams;
return (
<main>
<h1>Reviews</h1>
<p>Showing {typeof tag === "string" && tag ? `tag “${tag}”` : "every tag"}.</p>
{/* Real Next.js puts this in <link rel="canonical">; the simulator
applies the title only, so the page prints it too. */}
<p>Canonical URL: {run({ tag })}</p>
<p>
<Link href="/reviews?tag=react&utm_source=mail">React, from a newsletter</Link>{" "}
· <Link href="/reviews?tag=nonsense">An unknown tag</Link> ·{" "}
<Link href="/">Back</Link>
</p>
</main>
);
}
export function run({ tag }) {
const url = new URL("/reviews", "https://example.test");
if (typeof tag === "string" && tag) url.searchParams.set("tag", tag);
return url.toString();
}
export const examples = [
[{ tag: "a & b" }, "https://example.test/reviews?tag=a+%26+b"],
];
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Hint 1: the idea
The origin and the query are two separate allowlists. A tag that isn't a public
topic doesn't define different content, like a tracking parameter, so it
doesn't belong in the canonical URL.
Hint 2: a first step
The fixed origin and the accepted content choices are separate allowlists.
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({ tag }) { const url = new URL("/reviews", "https://example.test"); if (["react", "next"].includes(tag)) url.searchParams.set("tag", tag); return url.toString();}
["react", "next"].includes(tag) is false for anything else, including
undefined and arrays, so only known public tags reach the URL; URL handles
the encoding. Rejecting the request instead would break shared links that carry
an old tag; leaving it out of the canonical keeps the page working and the
search index clean.
Explain it without the code
Write the canonical URL for /reviews?tag=react&utm_source=mail and for
/reviews?tag=REACT, and say whether the second deserves a tag.
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.