Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as Sentry from '@sentry/nextjs';
import type {NextRequest} from 'next/server';
import {NextResponse} from 'next/server';

Expand Down Expand Up @@ -59,7 +60,9 @@ function isAIOrDevTool(userAgent: string): boolean {
*/
function wantsMarkdownViaAccept(acceptHeader: string): boolean {
return (
acceptHeader.includes('text/markdown') || acceptHeader.includes('text/x-markdown')
acceptHeader.includes('text/markdown') ||
acceptHeader.includes('text/x-markdown') ||
acceptHeader.includes('text/plain')
);
}

Expand Down Expand Up @@ -106,11 +109,13 @@ const handleAIClientRedirect = (request: NextRequest) => {
!url.pathname.includes('.') &&
!url.pathname.startsWith('/api/')
) {
const contentType = willServeMarkdown ? 'πŸ“„ MARKDOWN' : '🌐 HTML';
const methodInfo = willServeMarkdown ? ` (${detectionMethod})` : '';
console.log(
`[Middleware] ${url.pathname} - ${contentType}${methodInfo} - User-Agent: ${userAgent}`
);
Sentry.logger.info(`Middleware request processed: ${url.pathname}`, {
urlPath: url.pathname,
acceptHeader: request.headers.get('accept') || '',
userAgent: request.headers.get('user-agent') || '',
contentType: willServeMarkdown ? 'markdown' : 'html',
detectionMethod: willServeMarkdown ? detectionMethod : null,
});
}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Markdown Redirect Logs Incorrect for Root Path

The targetUrl logged for markdown redirects is inaccurate for the root path (/). The log calculates '.md', but the actual redirect goes to /index.md, which makes the logs misleading for debugging.

Fix in CursorΒ Fix in Web


// Create new URL with .md extension
const newUrl = url.clone();
Expand Down
Loading