How to cookie on nextjs?
Qurus opened this issue · comments
Hello I have followed tutorial but when I change the language on other page again reloading default language. How can I add cookies to keep switched language?
Thanks.
You can set the cookie when you switch the language, something like this:
// Switcher.tsx
import Cookies from 'js-cookie'
function handleChange(event: React.ChangeEvent<HTMLSelectElement>) {
const locale = event.target.value as LOCALES
setLocale(locale)
router.push(router.pathname, router.pathname, { locale })
// set the cookie
Cookies.set('NEXT_LOCALE', locale, {
expires: 31, //one month,
path: '/',
sameSite: 'lax'
})
}
Well this is working only on index page on other pages again redirecting to default language. :(
That is the limitation of of the Next.js framework. It only works on the index page.
You can read more about it in the docs