Skip to content

Commit 86a9021

Browse files
authored
fix: useCookieLocale not being set server-side (#3491)
1 parent 17df3ed commit 86a9021

2 files changed

Lines changed: 8 additions & 27 deletions

File tree

src/runtime/composables/index.ts

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { useRequestHeaders, useCookie as useNuxtCookie } from '#imports'
1+
import { useNuxtApp, useCookie as useNuxtCookie } from '#imports'
22
import { ref } from 'vue'
3-
import { parseAcceptLanguage, wrapComposable, runtimeDetectBrowserLanguage } from '../internal'
4-
import { localeCodes, normalizedLocales } from '#build/i18n.options.mjs'
3+
import { runtimeDetectBrowserLanguage, wrapComposable } from '../internal'
4+
import { localeCodes } from '#build/i18n.options.mjs'
55
import { _useLocaleHead, _useSetI18nParams } from '../routing/head'
66
import { getRouteBaseName, localePath, localeRoute, switchLocalePath } from '../routing/routing'
7-
import { findBrowserLocale } from '../routing/utils'
87
import type { Ref } from 'vue'
98
import type { Locale } from 'vue-i18n'
109
import type { resolveRoute } from '../routing/routing'
@@ -193,22 +192,13 @@ export function useSwitchLocalePath(): SwitchLocalePathFunction {
193192
* @returns the browser locale, if not detected, return `null`.
194193
*/
195194
export function useBrowserLocale(): string | null {
196-
const headers = useRequestHeaders(['accept-language'])
197-
return (
198-
findBrowserLocale(
199-
normalizedLocales,
200-
import.meta.client ? (navigator.languages as string[]) : parseAcceptLanguage(headers['accept-language'])
201-
) || null
202-
)
195+
return useNuxtApp().$i18n.getBrowserLocale() || null
203196
}
204197

205198
/**
206199
* Returns the locale cookie based on `document.cookie` (client-side) or `cookie` header (server-side).
207200
*
208-
* @remark
209-
* If `detectBrowserLanguage.useCookie` is `false` this will always return an empty string.
210-
*
211-
* @returns a `Ref<string>` with the detected cookie or an empty string if none is detected.
201+
* @returns a `Ref<string>` with the detected cookie or an empty string if none is detected or if `detectBrowserLanguage.useCookie` is disabled.
212202
*/
213203
export function useCookieLocale(): Ref<string> {
214204
const locale: Ref<string> = ref('')
@@ -218,17 +208,7 @@ export function useCookieLocale(): Ref<string> {
218208
return locale
219209
}
220210

221-
const cookieKey = detect.cookieKey!
222-
223-
let code: string | null = null
224-
if (import.meta.client) {
225-
code = useNuxtCookie<string>(cookieKey).value
226-
} else if (import.meta.server) {
227-
const cookie = useRequestHeaders(['cookie'])
228-
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
229-
code = (cookie as any)[cookieKey]
230-
}
231-
211+
const code: string | null = useNuxtCookie<string>(detect.cookieKey!).value
232212
if (code && localeCodes.includes(code)) {
233213
locale.value = code
234214
}

src/runtime/internal.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function wrapComposable<F extends (common: CommonComposableOptions, ...ar
2727
* @param input - Accept-Language header value.
2828
* @return An array of locale codes. Priority determined by order in array.
2929
*/
30-
export function parseAcceptLanguage(input: string = ''): string[] {
30+
function parseAcceptLanguage(input: string = ''): string[] {
3131
// Example input: en-US,en;q=0.9,nb;q=0.8,no;q=0.7
3232
// Contains tags separated by comma.
3333
// Each tag consists of locale code (2-3 letter language code) and optionally country code
@@ -62,6 +62,7 @@ export function getI18nCookie() {
6262
return useNuxtCookie<string | undefined>(cookieKey, cookieOptions)
6363
}
6464

65+
// TODO: remove side-effects
6566
export function getLocaleCookie(
6667
cookieRef: CookieRef<string | undefined>,
6768
detect: false | DetectBrowserLanguageOptions,

0 commit comments

Comments
 (0)