Skip to content

Commit 42d4878

Browse files
committed
feat: add text/plain Accept header detection and Sentry structured logging
- Add text/plain to Accept header detection (markdown is plain text) - Convert console.log to Sentry.logger.info with structured properties - Include URL path in log message for better readability - Log urlPath, acceptHeader, userAgent, contentType, detectionMethod Benefits: - Better HTTP semantics: text/plain Accept header gets markdown - Structured analytics in Sentry dashboard - Query by contentType, detectionMethod, urlPath, etc. - Production-ready logging format
1 parent 6d33b3f commit 42d4878

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/middleware.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type {NextRequest} from 'next/server';
22
import {NextResponse} from 'next/server';
3+
import * as Sentry from '@sentry/nextjs';
34

45
// This env var is set in next.config.js based on the `NEXT_PUBLIC_DEVELOPER_DOCS` env var at build time
56
// a workaround edge middleware not having access to env vars
@@ -59,7 +60,9 @@ function isAIOrDevTool(userAgent: string): boolean {
5960
*/
6061
function wantsMarkdownViaAccept(acceptHeader: string): boolean {
6162
return (
62-
acceptHeader.includes('text/markdown') || acceptHeader.includes('text/x-markdown')
63+
acceptHeader.includes('text/markdown') ||
64+
acceptHeader.includes('text/x-markdown') ||
65+
acceptHeader.includes('text/plain')
6366
);
6467
}
6568

@@ -108,9 +111,13 @@ const handleAIClientRedirect = (request: NextRequest) => {
108111
) {
109112
const contentType = willServeMarkdown ? '📄 MARKDOWN' : '🌐 HTML';
110113
const methodInfo = willServeMarkdown ? ` (${detectionMethod})` : '';
111-
console.log(
112-
`[Middleware] ${url.pathname} - ${contentType}${methodInfo} - User-Agent: ${userAgent}`
113-
);
114+
Sentry.logger.info(`Middleware request processed: ${url.pathname}`, {
115+
urlPath: url.pathname,
116+
acceptHeader: request.headers.get('accept') || '',
117+
userAgent: request.headers.get('user-agent') || '',
118+
contentType: willServeMarkdown ? 'markdown' : 'html',
119+
detectionMethod: willServeMarkdown ? detectionMethod : null,
120+
});
114121
}
115122

116123
// Skip if already requesting a markdown file
@@ -132,9 +139,11 @@ const handleAIClientRedirect = (request: NextRequest) => {
132139
// Check for markdown request (Accept header, user-agent, or manual)
133140
if (clientWantsMarkdown || forceMarkdown) {
134141
// Log the redirect for debugging
135-
console.log(
136-
`[Middleware] Redirecting to markdown: ${forceMarkdown ? 'Manual format=md' : detectionMethod}`
137-
);
142+
Sentry.logger.info('Markdown redirect triggered', {
143+
urlPath: url.pathname,
144+
detectionMethod: forceMarkdown ? 'Manual format=md' : detectionMethod,
145+
targetUrl: url.pathname.replace(/\/+$/, '') + '.md',
146+
});
138147

139148
// Create new URL with .md extension
140149
const newUrl = url.clone();

0 commit comments

Comments
 (0)