chore(): js-sdk set locale in server environment (#14313)

**What**
Prevent guess work in server environment and warn user when they try to set the locale on the sdk
This commit is contained in:
Adrien de Peretti
2025-12-16 11:25:03 +01:00
committed by GitHub
parent accb778039
commit 191e647eec
2 changed files with 18 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
---
"@medusajs/js-sdk": patch
---
chore(): js-sdk set locale in server environment

View File

@@ -147,9 +147,17 @@ export class Client {
}
setLocale(locale: string) {
if (!window) {
this.logger.warn(
"setLocale is not available in the server environment. Please set the locale directly through the 'x-medusa-locale' header."
)
return
}
if (hasStorage("localStorage")) {
window.localStorage.setItem(LOCALE_STORAGE_KEY, locale)
}
this.locale_ = locale
}
@@ -249,6 +257,11 @@ export class Client {
return async (input: FetchInput, init?: FetchArgs) => {
// We always want to fetch the up-to-date JWT token before firing off a request.
const headers = new Headers(defaultHeaders)
if (this.locale) {
headers.set("x-medusa-locale", this.locale)
}
const customHeaders = {
"x-medusa-locale": this.locale,
...this.config.globalHeaders,