11import type { NextRequest } from 'next/server' ;
22import { 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 */
6061function 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