fix(proxy): graceful error handling when Supabase is unreachable
Some checks failed
Workflow / ⚫️ Test (push) Has been cancelled
Workflow / ʦ TypeScript (push) Has been cancelled

Wrap getUser() calls in proxy.ts with try/catch so the proxy doesn't
crash when the Supabase client can't connect. Without this, the proxy
fails silently and Next.js returns 404 for all locale-dependent routes
(/auth/sign-in, /join, etc.) because the locale rewrite never happens.
This commit is contained in:
Zaid Marzguioui
2026-04-01 11:18:44 +02:00
parent 1687735de0
commit b2c9503749
2 changed files with 23 additions and 4 deletions

View File

@@ -80,7 +80,10 @@ async function adminMiddleware(request: NextRequest, response: NextResponse) {
return;
}
const { data, error } = await getUser(request, response);
const { data, error } = await getUser(request, response).catch(() => ({
data: null as any,
error: new Error('Supabase unreachable'),
}));
// If user is not logged in, redirect to sign in page.
// This should never happen, but just in case.
@@ -121,7 +124,14 @@ async function getPatterns() {
{
pattern: new URLPattern({ pathname: '/auth/*?' }),
handler: async (req: NextRequest, res: NextResponse) => {
const { data } = await getUser(req, res);
let data;
try {
({ data } = await getUser(req, res));
} catch {
// Supabase unreachable — treat as logged out, let the page render
return;
}
// the user is logged out, so we don't need to do anything
if (!data?.claims) {
@@ -148,7 +158,16 @@ async function getPatterns() {
{
pattern: new URLPattern({ pathname: '/home/*?' }),
handler: async (req: NextRequest, res: NextResponse) => {
const { data } = await getUser(req, res);
let data;
try {
({ data } = await getUser(req, res));
} catch {
// Supabase unreachable — redirect to sign in
const signIn = pathsConfig.auth.signIn;
return NextResponse.redirect(new URL(signIn, req.nextUrl.origin).href);
}
const { origin, pathname: next } = req.nextUrl;
// If user is not logged in, redirect to sign in page.