Files
flatrender/src/middleware.ts
T

37 lines
1.1 KiB
TypeScript
Raw Normal View History

import { type NextRequest } from "next/server";
import createIntlMiddleware from "next-intl/middleware";
import { routing } from "@/i18n/routing";
import { updateSession } from "@/lib/supabase/middleware";
const handleI18n = createIntlMiddleware(routing);
export async function middleware(request: NextRequest) {
// 1. Run i18n locale detection / redirect
const i18nResponse = handleI18n(request);
// If next-intl redirected (e.g. to add /en/ prefix), honour it immediately
if (i18nResponse.status !== 200 || i18nResponse.headers.has("location")) {
return i18nResponse;
}
// 2. Run Supabase session refresh on the (possibly rewritten) request
const supabaseResponse = await updateSession(request);
// Carry over any locale cookies/headers set by next-intl
i18nResponse.headers.forEach((value, key) => {
if (!supabaseResponse.headers.has(key)) {
supabaseResponse.headers.set(key, value);
}
});
return supabaseResponse;
}
export const config = {
// Match all routes except api, _next, static assets
matcher: [
"/((?!api|_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)",
],
};