feat: serve markdown to agents via Accept content negotiation#467
feat: serve markdown to agents via Accept content negotiation#467sriramveeraghanta wants to merge 2 commits into
Conversation
VitePress already emits a .md twin for every page (buildEnd copies the sources into dist/) and Vercel serves them as text/markdown, but the `Accept: text/markdown` negotiation never actually worked: vercel.json `rewrites` run *after* the filesystem, so the rewrite to `.md` was always shadowed by the existing `.html` file and never fired. Add Routing/Edge Middleware, which runs *before* the filesystem, to transparently rewrite page requests carrying `Accept: text/markdown` to their `.md` twin at the same URL. HTML stays the default for browsers, and Vercel's CDN already keys on the Accept header so the two variants cache separately (no Vary needed). - add middleware.ts (matcher skips assets/fonts/extensioned paths; root -> index.md) - add @vercel/functions dependency for the rewrite/next helpers - remove the dead `rewrites` block from vercel.json - update now-stale comments in .vitepress/config.ts
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Warning Review limit reached
More reviews will be available in 47 minutes and 37 seconds. Learn how PR review limits work. Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file). ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits. 🚦 How do rate limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan refill rate. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, the refill rate gradually slows as usage increases. The highest same-day bursts are limited more strictly. Please see our Fair Usage Limits Policy for further information. 📝 WalkthroughWalkthroughMarkdown content negotiation is migrated from a ChangesMarkdown Content Negotiation via Middleware
Sequence Diagram(s)sequenceDiagram
participant Agent as Agent/Client
participant Middleware as middleware.ts
participant Edge as Vercel Edge
participant Static as dist/*.md
Agent->>Middleware: GET /docs/page (Accept: text/markdown)
Middleware->>Middleware: Strip trailing slash → /docs/page.md
Middleware->>Edge: rewrite(/docs/page.md)
Edge->>Static: Fetch static .md file
Static-->>Agent: 200 text/markdown
Agent->>Middleware: GET /docs/page (Accept: text/html)
Middleware->>Edge: next() — no rewrite
Edge-->>Agent: 200 text/html
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@middleware.ts`:
- Around line 27-30: The Accept header negotiation at line 28 uses a simple
substring check with includes() that doesn't properly handle case variants,
quality values (q-values), or proper media range parsing. Replace the substring
check with proper media type parsing logic that parses the Accept header into
individual media ranges with their quality values, checks if text/markdown is
present and not explicitly unacceptable (where q=0 means reject), and performs
case-insensitive matching. This ensures the Accept negotiation follows HTTP
content negotiation standards and correctly handles cases like
"text/markdown;q=0" which should be treated as unacceptable.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 78da4a5d-67f9-4786-b725-672adefa483d
⛔ Files ignored due to path filters (1)
pnpm-lock.yamlis excluded by!**/pnpm-lock.yaml
📒 Files selected for processing (4)
docs/.vitepress/config.tsmiddleware.tspackage.jsonvercel.json
💤 Files with no reviewable changes (1)
- vercel.json
Replace the includes("text/markdown") substring check with a small parser
that handles q-values (q=0 = reject), matches the media type
case-insensitively, and ignores wildcards (*/*, text/*) so browsers keep
getting HTML.
Goal
Return a markdown representation of a page when an agent sends
Accept: text/markdown, while HTML stays the default for browsers — Cloudflare's "Markdown for Agents" pattern, implemented Vercel-natively (docs.plane.so is on Vercel, not Cloudflare-proxied).What was actually broken
The repo was already 95% there —
buildEnd()copies a.mdtwin of every page intodist/, and Vercel serves those withContent-Type: text/markdown(verified live). But theAccept: text/markdownnegotiation never fired:The fix has to run before the filesystem. The only Vercel mechanisms that do are redirects and Routing/Edge Middleware — middleware was chosen for transparent same-URL negotiation.
Changes
middleware.ts(new)Accept: text/markdown, transparently rewrites/foo→/foo.md(same URL, no redirect). Matcher skips assets/fonts/extensioned paths; root/→/index.md.package.json@vercel/functions(rewrite/nexthelpers)vercel.jsonrewritesblockdocs/.vitepress/config.tsmiddleware.tsCache safety: Vercel's CDN already includes the
Acceptheader in its cache key by default, so the HTML and markdown variants of a URL cache as separate entries — noVaryneeded, no risk of agents' markdown being served to browsers.Verified locally
pnpm buildpasses — 117.mdfiles +llms.txtstill emitrewrite/nextsignatures match usage (read from.d.ts); esbuild bundles cleanlypnpm check:formatcleanNeeds deploy to confirm
Middleware doesn't run under
vitepress preview, so end-to-end behavior must be checked on a Vercel preview/prod deploy:Tradeoff
The middleware runs as a small fluid-compute function on every page request (before the cache), returning
next()instantly for browsers. That's the cost of transparent same-URL negotiation vs. a pure-static redirect.Summary by CodeRabbit
text/markdownformat, enabling better integration with AI agents and tools.