Skip to content
Merged
Changes from 1 commit
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
23 changes: 16 additions & 7 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {NextRequest} from 'next/server';
import {NextResponse} from 'next/server';
import * as Sentry from '@sentry/nextjs';

// This env var is set in next.config.js based on the `NEXT_PUBLIC_DEVELOPER_DOCS` env var at build time
// a workaround edge middleware not having access to env vars
Expand Down Expand Up @@ -59,7 +60,9 @@
*/
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,15 @@
!url.pathname.includes('.') &&
!url.pathname.startsWith('/api/')
) {
const contentType = willServeMarkdown ? 'πŸ“„ MARKDOWN' : '🌐 HTML';

Check failure on line 112 in src/middleware.ts

View workflow job for this annotation

GitHub Actions / Lint

'contentType' is declared but its value is never read.
const methodInfo = willServeMarkdown ? ` (${detectionMethod})` : '';

Check failure on line 113 in src/middleware.ts

View workflow job for this annotation

GitHub Actions / Lint

'methodInfo' is declared but its value is never read.
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 +139,11 @@
// 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