useCookieLocale() always return ref with empty string in server context (SSR).
This is useCookieLocale() source code:
export function useCookieLocale(): Ref<string> {
// Support for importing from `#imports` is generated by auto `imports` nuxt module, so `ref` is imported from `vue`
const locale: Ref<string> = ref('')
const detect = runtimeDetectBrowserLanguage()
if (detect && detect.useCookie) {
const cookieKey = detect.cookieKey!
let code: string | null = null
if (import.meta.client) {
code = useNuxtCookie<string>(cookieKey).value
} else if (import.meta.server) {
const cookie = useRequestHeaders(['cookie'])
// eslint-disable-next-line @typescript-eslint/no-explicit-any
code = (cookie as any)[cookieKey]
}
if (code && localeCodes.includes(code)) {
locale.value = code
}
}
return locale
}
In server part there is a useRequestHeaders(['cookie']) call which always return type
{ cookie?: string | undefined }
Next we have this line:
code = (cookie as any)[cookieKey]
It is incorrect, because, cookie variable is object which only has one property which is cookie, but in this line we are trying to get cookieKey key from it.
Environment
Reproduction
https://stackblitz.com/edit/bobbiegoede-nuxt-i18n-starter-hmdag5?file=middleware%2Fssr-context-cookie.ts
Describe the bug
useCookieLocale()
always return ref with empty string in server context (SSR).This is
useCookieLocale()
source code:In server part there is a
useRequestHeaders(['cookie'])
call which always return typeNext we have this line:
It is incorrect, because,
cookie
variable is object which only has one property which iscookie
, but in this line we are trying to getcookieKey
key from it.For example in my app this code:
Prints this to console:
Additional context
Since Nuxt
useCookie()
is SSR friendly, i think thatuseCookieLocale
could be rewritten as such:Logs
No response