A record’s version can be represented as an ETag. A client sends If-Match when it only wants to update the version it read, turning a silent overwrite into an explicit failed precondition.
Read, predict, run
The model accepts an opaque expected tag and the current tag. This lesson supports one exact tag, not the full HTTP list and wildcard grammar. The final API uses an explicit numeric version field; compare the two contracts:
JavaScript
export function run({ expected, current }) { if (expected == null) return 428; return expected === current ? 200 : 412;}
Before pressing Run, predict what run returns for {"expected": null, "current": "\"v2\""} and {"expected": "\"v1\"", "current": "\"v2\""}, from the
code above. Write your answers down first: after Run, the case table shows the
expected values beside the actual ones.
Use Preconditions for Conditional WritesNext.js · 5 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 Cases from "./Cases.jsx";
export default function Page() {
return (
<main>
<h1>Use Preconditions for Conditional Writes</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({ expected, current }) {
if (expected == null) return 428;
return expected === current ? 200 : 412;
}
export const examples = [
[{ expected: null, current: '"v2"' }, 428],
[{ expected: '"v1"', current: '"v2"' }, 412],
];
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
An ETag is not a session token or permission grant. Check authorization separately, and make the version comparison atomic with the write.
Compare an update that accepts a missing tag with this contract. Against a
record whose tag is "v2": no If-Match gets 428, so a client that forgot
the header learns about it instead of overwriting blindly; If-Match: "v2"
gets 200; If-Match: "v1" gets 412. A contract that treated a missing header
as "no condition" would silently overwrite in the first case.
Exercise
Extend the contract
Reject weak tags that start with W/ as 412 for this exact strong-comparison contract.
Decide first: should W/"v2" match W/"v2" for a write? Write your answer
with one sentence of justification before you open the code.
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 {"expected": "W/\"v2\"", "current": "W/\"v2\""} must return 412.
Your working spaceNext.js · 5 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 Cases from "./Cases.jsx";
export default function Page() {
return (
<main>
<h1>Use Preconditions for Conditional Writes</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({ expected, current }) {
if (expected == null) return 428;
return expected === current ? 200 : 412;
}
export const examples = [
[{ expected: null, current: '"v2"' }, 428],
[{ expected: '"v1"', current: '"v2"' }, 412],
];
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Hint 1: the idea
A weak tag (W/ prefix) says two representations are equivalent, not
identical. A write needs the strong guarantee, so a weak tag can't satisfy it,
even when the strings match.
Hint 2: a first step
Keep the missing-precondition response distinct from a non-matching supplied tag.
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({ expected, current }) { if (expected == null) return 428; return !expected.startsWith("W/") && expected === current ? 200 : 412;}
The W/ check comes after the missing-tag check, so a missing header is still
428 and a weak one is 412: the client sent a condition, it just can't be met.
startsWith is safe there because expected is known not to be null. Accepting
weak tags would let a write go through against a representation the server only
called equivalent.
Explain it without the code
Write which status a client gets with no header, a stale tag and a weak tag,
and what it should do next in each case.
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.