I’m having trouble with my middleware in 2 applications.
both look like that
import type { NextRequest } from 'next/server';
import createMiddleware from 'next-intl/middleware';
import { routing } from './i18n/routing';
const intlMiddleware = createMiddleware(routing);
export function middleware(request: NextRequest) {
const response = intlMiddleware(request);
const initialTheme = request.cookies.get('theme')?.value;
let theme = 'light';
if (initialTheme === 'dark') {
theme = 'dark';
}
response.headers.set('x-theme', theme);
return response;
}
export const config = {
matcher: ['/((?!api|trpc|netlify|_next|_vercel|.*\\..*).*)'],
};
On dev env it works perfectly, but on netlify, I can see the request cookies, but it doesnt set the response cookies.
then if reload the page, the theme goes to default, and same with the language if I navigate to another page.
Repos: GitHub - LucasMendoncaWF/portfolio
GitHub - LucasMendoncaWF/mp5
I’m setting the cookie this way on client side:
document.cookie = theme=${newTheme}; path=/; max-age=31536000; SameSite=Lax; Secure;
already tried with domain, without Secure, Without samesite and it never worked