From 417aad4f384ec86ff2d5d2d6cea846f896279c50 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 2 Jun 2026 03:15:07 +0000 Subject: [PATCH] fix: migrate middleware.ts to proxy.ts for Next.js 16 compatibility MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Next.js 16 deprecated the `middleware` file convention in favour of `proxy`. The deprecated `middleware.ts` was causing MIDDLEWARE_INVOCATION_FAILED 500 errors on every request hitting repofuse.com. Changes: - Rename middleware.ts → proxy.ts (required by Next.js 16) - Return NextResponse.next() explicitly instead of bare `return` (undefined is not a valid response in the Edge runtime and triggers the 500) - Wrap auth.protect() in its own try/catch to redirect gracefully on Clerk auth failures rather than surfacing an unhandled exception - Add outer try/catch so any unexpected error redirects cleanly instead of propagating as a 500 https://claude.ai/code/session_018w2VTF3yhVhavRJ8rTVWaV --- middleware.ts | 26 -------------------------- proxy.ts | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 26 deletions(-) delete mode 100644 middleware.ts create mode 100644 proxy.ts diff --git a/middleware.ts b/middleware.ts deleted file mode 100644 index 93f1f6c..0000000 --- a/middleware.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server' -import { NextResponse } from 'next/server' -import { isClerkConfigured } from '@/lib/clerk-auth' - -const isDashboardRoute = createRouteMatcher(['/dashboard(.*)']) - -export default clerkMiddleware(async (auth, request) => { - if (!isDashboardRoute(request)) { - return - } - - if (isClerkConfigured()) { - await auth.protect() - return - } - - const userId = request.cookies.get('github_user_id')?.value - const accessToken = request.cookies.get('github_access_token')?.value - if (!userId || !accessToken) { - return NextResponse.redirect(new URL('/?error=auth_required', request.url)) - } -}) - -export const config = { - matcher: ['/dashboard/:path*'], -} diff --git a/proxy.ts b/proxy.ts new file mode 100644 index 0000000..e2da233 --- /dev/null +++ b/proxy.ts @@ -0,0 +1,37 @@ +import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server' +import { NextResponse, NextRequest } from 'next/server' +import { isClerkConfigured } from '@/lib/clerk-auth' + +const isDashboardRoute = createRouteMatcher(['/dashboard(.*)']) + +export default clerkMiddleware(async (auth, request: NextRequest) => { + try { + if (!isDashboardRoute(request)) { + return NextResponse.next() + } + + if (isClerkConfigured()) { + try { + await auth.protect() + } catch (error) { + console.error('[proxy] Clerk auth.protect() failed:', error) + return NextResponse.redirect(new URL('/?error=auth_failed', request.url)) + } + return NextResponse.next() + } + + const userId = request.cookies.get('github_user_id')?.value + const accessToken = request.cookies.get('github_access_token')?.value + if (!userId || !accessToken) { + return NextResponse.redirect(new URL('/?error=auth_required', request.url)) + } + return NextResponse.next() + } catch (error) { + console.error('[proxy] Invocation error:', error) + return NextResponse.redirect(new URL('/?error=middleware_error', request.url)) + } +}) + +export const config = { + matcher: ['/dashboard/:path*'], +}