another step
This commit is contained in:
+17
-2
@@ -2,7 +2,7 @@ import createMiddleware from 'next-intl/middleware';
|
||||
import { type NextRequest, NextResponse } from 'next/server';
|
||||
import { routing } from './src/i18n/routing';
|
||||
import { COOKIE_NAMES } from './src/lib/cookies';
|
||||
import { PUBLIC_PATHS, ROUTES } from './src/constants';
|
||||
import { HEADER_NAMES, PUBLIC_PATHS, ROUTES } from './src/constants';
|
||||
|
||||
const intlMiddleware = createMiddleware(routing);
|
||||
|
||||
@@ -39,7 +39,22 @@ export default function middleware(request: NextRequest) {
|
||||
}
|
||||
}
|
||||
|
||||
return i18nResponse;
|
||||
// Detect locale from the normalized URL (next-intl always puts it at position 1)
|
||||
const locale = routing.locales.find(
|
||||
(l) => pathname === `/${l}` || pathname.startsWith(`/${l}/`),
|
||||
) ?? routing.defaultLocale;
|
||||
|
||||
const requestHeaders = new Headers(request.headers);
|
||||
requestHeaders.set(HEADER_NAMES.LOCALE, locale);
|
||||
|
||||
const response = NextResponse.next({ request: { headers: requestHeaders } });
|
||||
|
||||
// Preserve response headers set by next-intl (e.g. Link: alternate hreflang)
|
||||
i18nResponse.headers.forEach((value, key) => {
|
||||
response.headers.set(key, value);
|
||||
});
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
export const config = {
|
||||
|
||||
Reference in New Issue
Block a user