Fix SSE 500: move API rewrites to afterFiles so route handlers take priority

The flat-array form of rewrites() runs as beforeFiles, intercepting /api/v1/*
requests before Next.js can match route handlers. This caused the SSE stream
endpoints (outlines/stream, presentation/stream, jobs/stream) to be handled
by the buffering HTTP proxy instead of the custom streaming route handlers,
resulting in ECONNRESET → 500 on the browser.

afterFiles rewrites only apply when no matching app/api/ file exists, so
route handlers now win over the backend proxy for all /api/v1/* paths that
have explicit handlers.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Vadym Samoilenko 2026-03-01 21:14:44 +00:00
parent 58e738e79b
commit 65da74ab13
2 changed files with 24 additions and 16 deletions

View file

@ -6,22 +6,30 @@ const nextConfig = {
distDir: ".next-build",
// Proxy API and static asset requests to FastAPI backend
// Proxy API and static asset requests to FastAPI backend.
// afterFiles: Next.js checks route handlers first; only falls through to
// these rewrites if no matching app/api/ route handler exists.
// This ensures SSE route handlers (outlines/stream, presentation/stream, etc.)
// are served directly without being intercepted by the catch-all /api/v1/* rewrite.
async rewrites() {
return [
{
source: '/api/v1/:path*',
destination: `${API_URL}/api/v1/:path*`,
},
{
source: '/app_data/:path*',
destination: `${API_URL}/app_data/:path*`,
},
{
source: '/static/:path*',
destination: `${API_URL}/static/:path*`,
},
];
return {
beforeFiles: [],
afterFiles: [
{
source: '/api/v1/:path*',
destination: `${API_URL}/api/v1/:path*`,
},
{
source: '/app_data/:path*',
destination: `${API_URL}/app_data/:path*`,
},
{
source: '/static/:path*',
destination: `${API_URL}/static/:path*`,
},
],
fallback: [],
};
},
images: {

File diff suppressed because one or more lines are too long