RFC 9110 HTTP Content Negotiation Lab
Hyper Content Negotiation Lab
Returning full styled HTML pages to automated readers wastes tokens on navigation chrome and forces brittle
scraping. Test how one canonical URI serves text/html, text/markdown,
application/ld+json, and application/json using standard Accept
negotiation with Vary: Accept and alternate Link headers.
Standard RFC 9110 Accept negotiation (Vary: Accept) on a single canonical URL
shrinks automated retrieval payloads significantly without breaking browser HTML or duplicating URL trees.
text/markdown and application/ld+json measure 50%–85% fewer bytes and
estimated tokens than full text/html while preserving Vary: Accept and alternate
Link discovery headers.
Fine-grained custom headers (such as Accept-Prompt) collapse shared CDN caches under
Vary and expose user preferences on the first request.
Quick Experiments
Run live requests against the Worker endpoints and inspect exact wire headers.
Interactive Negotiation Workbench
1. Canonical Resource or Remote HTTPS URL
Select a built-in /docs/:slug resource or fetch any public https:// page through
the SSRF-guarded /negotiate endpoint.
2. HTTP Accept & Optional Accept-Prompt Header
Send a real Accept header via fetch() to inspect RFC 9110 q-weight matching, or
attach an experimental Accept-Prompt header to test cache-key fragmentation.
Representation Payload Comparison
Comparing UTF-8 bytes, gzip bytes, and estimated tokens (~chars/4) against the HTML baseline.
Cache-Key Cardinality & Fingerprinting Check
Vary: Accept (4 bounded keys)Coarse RFC 9110 Accept negotiation keeps shared CDN caches bounded to 4 canonical variants (html, markdown, jsonld, json).
Negotiated Representation Body text/markdown 0 B · 0 tokens
Loading...
Live HTTP Response Headers & RFC 9110 Match HTTP 200 Vary: Accept
Loading headers...
Ready-to-Run curl Commands CLI 4 media types
Live Network Wire Telemetry Log 0 requests Observed fetch() headers
No requests recorded yet.
Canonical Negotiation Flow
Accept precedence and emits
Vary: Accept so shared caches store separate entries per media representation without exploding
cache keys on custom prompt headers.
Implementation & Wire Contract
One Cloudflare Worker serves styled HTML, compact Markdown, Schema.org JSON-LD, and JSON from
/docs/:slug and /negotiate using the RFC 9110 parser in
src/negotiate.ts, emitting Vary: Accept and alternate Link headers.
// Scraping styled HTML or maintaining parallel format-specific URL trees
const html = await fetch("https://site.example/docs/architecture").then((r) => r.text());
const doc = new DOMParser().parseFromString(html, "text/html");
// Brittle selectors pull navigation, footers, and chrome alongside prose
const body = doc.querySelector("main article")?.textContent ?? "";
// Or the origin duplicates routes per format:
// GET /docs/architecture -> HTML
// GET /api/v1/architecture.md -> Markdown
// GET /api/v1/architecture.json -> JSON
// One canonical URI (/docs/:slug) responds according to Accept q-values
const mdRes = await fetch("/docs/hyper-content-negotiation", {
headers: { Accept: "text/markdown, text/html;q=0.8" },
});
// Content-Type: text/markdown; charset=utf-8
// Vary: Accept
// Link: </docs/...?format=markdown>; rel="alternate"; type="text/markdown", ...
const markdown = await mdRes.text();
const ldRes = await fetch("/docs/hyper-content-negotiation", {
headers: { Accept: "application/ld+json" },
});
const graph = await ldRes.json();
Complete Page & Worker Wiring (public/index.html + src/negotiate.ts + src/worker.ts)
Matches the live Worker routes in src/worker.ts, the SUPPORTED_MEDIA_TYPES
precedence table in src/negotiate.ts, and the browser fetch() calls in
public/app.js.
<!-- 1. Page HTML Setup (public/index.html) -->
<form id="accept-form">
<input id="accept-input" name="accept" type="text" value="text/markdown, text/html;q=0.8" required>
<input id="accept-prompt-input" name="acceptPrompt" type="text" placeholder="Optional Accept-Prompt">
<button type="submit">Send Request</button>
</form>
<pre id="wire-body-view"><code id="wire-body-code">Loading...</code></pre>
<script type="module" src="/app.js"></script>
// 2. Media-Type Precedence Table (src/negotiate.ts)
export const FORMAT_CONTENT_TYPES = {
html: "text/html; charset=utf-8",
markdown: "text/markdown; charset=utf-8",
jsonld: "application/ld+json; charset=utf-8",
json: "application/json; charset=utf-8",
};
// parseAcceptHeader() ranks candidates by (b.q - a.q) || (b.specificity - a.specificity) || (a.serverPriority - b.serverPriority)
// 3. Edge Route & Coarse Cache Policy (src/worker.ts -> handleDocsRoute)
const negotiation = parseAcceptHeader(request.headers.get("Accept"), url.searchParams.get("format"));
const rendered = renderRepresentation(doc, negotiation.format, canonicalUrl);
const cacheAnalysis = analyzeCacheKeyImpact(request.headers.get("Accept-Prompt"));
const headers = new Headers({
"Content-Type": rendered.contentType,
Vary: "Accept", // Never vary on free-form Accept-Prompt strings
"X-Negotiated-Format": rendered.format,
"X-Negotiation-Acceptable": String(negotiation.acceptable),
"X-Matched-Accept-Range": negotiation.matchedRange,
"X-Cache-Key-Strategy": "coarse-accept (4 variants)",
Link: buildAlternateLinkHeader(canonicalUrl),
});
if (cacheAnalysis.promptProvided) {
headers.set("X-Accept-Prompt-Status", cacheAnalysis.promptPolicy);
}
Fragile HTML scraping selectors and parallel /api/*.md endpoint trees that split canonical URLs and duplicate indexing.
Client media-type priority via Accept (q-weights and specificity), bounded CDN cache keys via Vary: Accept, and machine-readable alternates via Link.
Browser address bars send a fixed Accept: text/html,... header, and free-form headers like Accept-Prompt cannot safely enter Vary without collapsing shared CDN cache hit rates.