I’ve started a NextJs project and when I follow the docs I realize that in production the cookie NEXT_LOCALTE generated by the Intl middleware is not set as secure?
How can I improve that? I’ve created an approach but I don’t know if that the best solution.
Original
import createMiddleware from "next-intl/middleware";
import { locales } from "./i18n.config";
export default createMiddleware({
defaultLocale: locales[0],
locales,
localeDetection: true,
});
export const config = {
matcher: [
"/((?!api|_next|_vercel|.*\..*).*)",
],
};
Approach to avoid the issue:
import { NextRequest, NextResponse } from 'next/server';
import createIntlMiddleware from 'next-intl/middleware';
import { locales } from './i18n.config';
const intlMiddleware = createIntlMiddleware({
defaultLocale: locales[0],
locales,
localeDetection: true,
});
export function middleware(req: NextRequest): NextResponse {
const res = intlMiddleware(req) as NextResponse;
const nextLocaleCookie = res.cookies.get("NEXT_LOCALE");
if (nextLocaleCookie) {
res.cookies.set({ ...nextLocaleCookie, secure: true })
}
return res;
}
export const config = {
matcher: [
'/((?!api|_next|_vercel|.*\..*).*)',
],
};