Markdown Negotiation Snippet
Returns a working proxy.ts example that detects the Accept header and rewrites to a markdown route, plus a basic markdown route handler. Drop into your Next.js project.
Configuration
proxy.ts
typescript · 28 lines · 705 B
// proxy.ts (Next.js 16+ App Router)
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
function acceptsMarkdown(request: NextRequest): boolean {
const accept = request.headers.get("accept") ?? "";
return accept.toLowerCase().includes("text/markdown");
}
export function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
if (pathname !== "/") {
return NextResponse.next();
}
if (acceptsMarkdown(request)) {
const rewriteUrl = request.nextUrl.clone();
rewriteUrl.pathname = "/markdown-home";
return NextResponse.rewrite(rewriteUrl);
}
return NextResponse.next();
}
export const config = {
matcher: ["/"],
};
Markdown route handler
typescript · 23 lines · 416 B
// app/markdown-home/route.ts
import { NextResponse } from "next/server";
const BODY = `# Your page
> Short tagline.
## Sections
- [Docs](https://example.com/docs)
- [API](https://example.com/api)
`;
export async function GET() {
return new NextResponse(BODY, {
status: 200,
headers: {
"content-type": "text/markdown; charset=utf-8",
"cache-control": "public, max-age=300",
},
});
}
Verify with curl
shell · 1 line · 51 B
curl -H "Accept: text/markdown" https://yourdomain/
Apply this with your coding agent
Get a ready-made prompt that wraps the output above with implementation steps. Paste it into your AI assistant and let it ship the change.
Why this matters
Browsers receive HTML by default. Agents that send Accept: text/markdown get a clean, token-efficient representation of the same page. This is the simplest content negotiation layer that agents can rely on.
On Next.js 16, this lives in proxy.ts at the project root. The previous middleware file convention has been renamed to proxy.
Verify on a real URL
Run a full agent readiness scan to see how your live site responds end to end.