Skip to content
qino

Since 0.25.0

negotiateMarkdown

Answer a request with page Markdown when it asks for Markdown, or with null so the page renders.

View Markdown
On this page

Answers a request that wants page Markdown, and returns null for one that wants HTML, so your route renders the page. It takes a Web Request, so it works in any framework that uses them, such as TanStack Start route handlers and middleware, or Next.js route handlers.

src/routes/blog.$slug.tsx
import { negotiateMarkdown } from "@qino/cms";

const response = await negotiateMarkdown(request, () =>
  postCollection.getPageMarkdown(slug),
);
return response ?? next();

Signature

@qino/cms
function negotiateMarkdown(
  request: Request,
  load: LoadPageMarkdown,
): Promise<Response | null>;

type LoadPageMarkdown = () => string | null | Promise<string | null>;

Parameters

load usually calls a primitive's getPageMarkdown, but any string works, so the homepage or a middleware can use it too. LoadPageMarkdown is exported from @qino/cms.

When it serves Markdown

A request wants Markdown when its path ends in .md, whatever its Accept header says. Otherwise it reads the Accept header:

  • Types and parameter names are case-insensitive.
  • A , or ; inside a quoted parameter value, such as note="a,b", is part of the value.
  • A range whose q-value doesn't follow RFC 9110, such as q=1.5 or q=oops, is ignored.
  • It never answers 406 Not Acceptable. A request that accepts neither type gets null, and so HTML.

On a prerendered page, a host rewrite picks the answer before negotiateMarkdown runs, and its regex can disagree with this table. See Known limits.

Returns

Promise<Response | null>:

  • null when the request wants HTML. load isn't called.
  • A 200 with the page Markdown when load resolves to a string.
  • A 404 when load resolves to null, with a short Markdown body that links to /llms.txt on the request's origin:
404 body
# Not found

This page doesn't exist. See [llms.txt](https://example.com/llms.txt) for the pages that do.

Both responses carry Content-Type: text/markdown; charset=utf-8 and Vary: Accept. A HEAD request gets the same status and headers, with no body.

It adds no Link header: add one to your HTML responses, with the URL from getMarkdownUrl.

Throws

Whatever load throws.

See also