Skip to content

Conversation

@codyde
Copy link
Contributor

@codyde codyde commented Sep 28, 2025

Summary

Implements automatic markdown serving for AI/LLM tools and development environments through HTTP Accept header content negotiation AND user-agent pattern matching. When clients request markdown content, they are automatically redirected to .md versions for better LLM consumption and processing.

Detection Methods

🌐 HTTP Accept Header Content Negotiation (Standards-Compliant)

Follows HTTP standards for content type negotiation:

  • Accept: text/markdown - Standard markdown MIME type
  • Accept: text/x-markdown - Alternative markdown MIME type

Examples:

curl -H "Accept: text/markdown" https://docs.sentry.io/platforms/react/
curl -H "Accept: text/x-markdown" https://docs.sentry.io/platforms/react/

🤖 User-Agent Pattern Detection (Backward Compatibility)

Detects AI tools and development environments that benefit from markdown:

AI/LLM Tools

  • Claude (/claude/i) - Claude Desktop/Code
  • ChatGPT (/chatgpt/i) - ChatGPT clients
  • OpenAI (/openai/i) - OpenAI tools and APIs
  • Anthropic (/anthropic/i) - Anthropic tools

Development Tools & IDEs

  • Cursor (/cursor/i) - Cursor IDE
  • GitHub Copilot (/copilot/i) - GitHub Copilot
  • VS Code (/vscode/i) - VS Code extensions
  • IntelliJ (/intellij/i) - IntelliJ plugins
  • Sublime (/sublime/i) - Sublime Text plugins

HTTP Libraries

  • Got (/got/i) - sindresorhus/got HTTP library for Node.js

🎛️ Manual Override

  • Query Parameter: ?format=md forces markdown for any client

How It Works

  1. Canonical Redirects First: Handles deprecated URLs before markdown detection
  2. Hybrid Detection: Checks Accept header, then user-agent, then manual override
  3. Automatic Redirect: Redirects matching clients to .md versions (302 redirect)
  4. Existing Infrastructure: Leverages Next.js rewrite rules and markdown exports

Example Usage

Accept Header (Standards-Compliant)

# OpenCode-style content negotiation
curl -H "Accept: text/markdown" https://docs.sentry.io/platforms/react/
# → 302 redirect to /platforms/react.md

User-Agent Detection (Legacy Tool Support)

# AI client gets redirected to markdown
curl -H "User-Agent: Claude/1.0" https://docs.sentry.io/platforms/react/
# → 302 redirect to /platforms/react.md

# Normal browser gets HTML
curl -H "User-Agent: Mozilla/5.0..." https://docs.sentry.io/platforms/react/
# → 200 HTML response

Manual Override

# Force markdown for any user agent
https://docs.sentry.io/platforms/react/?format=md
# → 302 redirect to /platforms/react.md

Enhanced Logging

Shows detection method and content type:

[Middleware] /platforms/react/ - 📄 MARKDOWN (Accept header) - User-Agent: OpenCode/1.0
[Middleware] /platforms/react/ - 📄 MARKDOWN (User-agent) - User-Agent: Claude/1.0
[Middleware] /platforms/react/ - 📄 MARKDOWN (Manual) - User-Agent: Mozilla/5.0
[Middleware] /platforms/react/ - 🌐 HTML - User-Agent: Mozilla/5.0

Key Improvements

Standards Compliance

  • HTTP Content Negotiation: Proper Accept header support following RFC standards
  • Future-Proof: Ready for tools that adopt standard content negotiation
  • OpenCode Compatible: Works with sophisticated HTTP clients like OpenCode

Robust Detection

  • Precise Static File Filtering: Uses specific file extension regex instead of broad .includes('.')
  • Multiple Trailing Slash Handling: Handles edge cases like /path///
  • Canonical Redirect Priority: Deprecated URLs redirect properly before markdown conversion

Enhanced Debugging

  • Detection Method Tracking: Shows whether markdown was triggered by Accept header, user-agent, or manual override
  • Content Type Indicators: Visual markers for markdown vs HTML serving
  • Comprehensive Logging: Full request tracing for debugging and analytics

Benefits

  • Better LLM Experience: AI tools get clean, structured markdown instead of HTML
  • Standards Compliance: Uses HTTP Accept headers as intended for content negotiation
  • Wider Tool Support: Works with both legacy tools (user-agent) and modern tools (Accept header)
  • Analytics Insights: Track which AI tools and methods are accessing your docs
  • Future-Proof: Easy to add new patterns and support emerging tools

@vercel
Copy link

vercel bot commented Sep 28, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
develop-docs Ready Ready Preview Comment Sep 29, 2025 6:08am
sentry-docs Ready Ready Preview Comment Sep 29, 2025 6:08am

cursor[bot]

This comment was marked as outdated.

@codecov
Copy link

codecov bot commented Sep 28, 2025

Bundle Report

Changes will increase total bundle size by 9.19kB (0.04%) ⬆️. This is within the configured threshold ✅

Detailed changes
Bundle name Size Change
sentry-docs-edge-server-array-push 353.45kB 1.25kB (0.35%) ⬆️
sentry-docs-client-array-push 10.13MB -6 bytes (-0.0%) ⬇️
sentry-docs-server-cjs 12.9MB 7.95kB (0.06%) ⬆️

Affected Assets, Files, and Routes:

view changes for bundle: sentry-docs-client-array-push

Assets Changed:

Asset Name Size Change Total Size Change (%)
static/chunks/pages/_app-*.js -3 bytes 882.71kB -0.0%
static/chunks/8321-*.js -3 bytes 425.87kB -0.0%
static/urqQBtzXxzlie1_6TDfvg/_buildManifest.js (New) 684 bytes 684 bytes 100.0% 🚀
static/urqQBtzXxzlie1_6TDfvg/_ssgManifest.js (New) 77 bytes 77 bytes 100.0% 🚀
static/0JCe0pMUfu609x3obILs5/_buildManifest.js (Deleted) -684 bytes 0 bytes -100.0% 🗑️
static/0JCe0pMUfu609x3obILs5/_ssgManifest.js (Deleted) -77 bytes 0 bytes -100.0% 🗑️
view changes for bundle: sentry-docs-edge-server-array-push

Assets Changed:

Asset Name Size Change Total Size Change (%)
src/middleware.js 1.25kB 208.69kB 0.6%
view changes for bundle: sentry-docs-server-cjs

Assets Changed:

Asset Name Size Change Total Size Change (%)
1729.js -3 bytes 1.78MB -0.0%
../instrumentation.js -3 bytes 1.1MB -0.0%
9523.js -3 bytes 1.08MB -0.0%
../app/[[...path]]/page.js.nft.json 2.65kB 837.07kB 0.32%
../app/platform-redirect/page.js.nft.json 2.65kB 836.98kB 0.32%
../app/sitemap.xml/route.js.nft.json 2.65kB 834.21kB 0.32%

cursor[bot]

This comment was marked as outdated.

codyde and others added 8 commits September 28, 2025 18:34
- Detect AI/LLM tools (Claude, Cursor, Copilot, etc.) via user-agent
- Automatically redirect to .md versions for better LLM consumption
- Add manual override with ?format=md query parameter
- Preserve existing redirect functionality

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
- Log all user agents for non-static requests
- Log when redirects to markdown occur
- Helps identify what user agents AI tools are sending
- Add /got/i pattern to detect sindresorhus/got library
- Common HTTP client used by Node.js tools and automation
- Ensures markdown serving for tools using got for requests
- Show 📄 MARKDOWN or 🌐 HTML indicators in logs
- Determine content type before logging for better debugging
- Makes it clear which requests get markdown vs HTML content
- Replace url.pathname.includes('.') with specific file extension regex
- Fixes AI client detection for .NET, Node.js, Next.js documentation
- Now only excludes actual static files (.js, .png, .css, etc.)
- Resolves Sentry bug report about .NET docs not getting markdown redirects

Fixes: Critical bug where paths like /platforms/dotnet/ were incorrectly
excluded from AI client detection due to containing dots in the path.
- Replace /\/$/ with /\/+$/ to remove ALL trailing slashes
- Prevents malformed URLs like /path//.md when multiple slashes present
- Defensive programming for edge cases bypassing Next.js normalization
- Reported by Cursor - simple one-character regex fix

Examples:
- /platforms/react/// -> /platforms/react.md ✅
- /platforms/react/ -> /platforms/react.md ✅
- /platforms/react -> /platforms/react.md ✅
- Process canonical URL redirects first before AI client detection
- Ensures deprecated URLs redirect to current paths before markdown conversion
- Prevents AI clients from getting outdated content via old URLs
- Fixes Cursor bug report about bypassing canonical URL redirects

Example flow:
1. /old-path/ → /new-path/ (canonical redirect)
2. AI client requests /new-path/ → /new-path.md (markdown)

Instead of:
1. AI client requests /old-path/ → /old-path.md (404/outdated)
cursor[bot]

This comment was marked as outdated.

codyde and others added 2 commits September 28, 2025 22:03
- Implement hybrid strategy: Accept header + user-agent detection
- Support standards-compliant text/markdown and text/x-markdown Accept headers
- Maintain backward compatibility with existing user-agent detection
- Enhanced logging shows detection method (Accept header, User-agent, Manual)
- Inspired by OpenCode's content negotiation approach

Examples:
- Accept: text/markdown → markdown (standards-compliant)
- User-Agent: Claude/1.0 → markdown (fallback for existing tools)
- ?format=md → markdown (manual override)

This makes docs accessible to more tools while following HTTP standards.
@codyde codyde merged commit 0d3cde1 into master Sep 29, 2025
15 checks passed
@codyde codyde deleted the feat/ai-client-markdown-redirect-clean branch September 29, 2025 07:41
@github-actions github-actions bot locked and limited conversation to collaborators Oct 15, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants