{"record":{"id":"78b97bb270e24a32","repo":"medusajs/medusa","slug":"setlocale-is-not-available-in-the-server-environme","errorCode":null,"errorMessage":"setLocale is not available in the server environment. Please set the locale directly through the 'x-medusa-locale' header.","messagePattern":"setLocale is not available in the server environment\\. Please set the locale directly through the 'x-medusa-locale' header\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"packages/core/js-sdk/src/client.ts","lineNumber":173,"sourceCode":"      info: console.info,\n      debug: console.debug,\n    }\n\n    this.logger = {\n      ...logger,\n      debug: config.debug ? logger.debug : () => {},\n    }\n\n    if (hasStorage(\"localStorage\")) {\n      this.locale_ = window.localStorage.getItem(LOCALE_STORAGE_KEY) || \"\"\n    }\n\n    this.fetch_ = this.initClient()\n  }\n\n  setLocale(locale: string) {\n    if (!window) {\n      this.logger.warn(\n        \"setLocale is not available in the server environment. Please set the locale directly through the 'x-medusa-locale' header.\"\n      )\n      return\n    }\n\n    if (hasStorage(\"localStorage\")) {\n      window.localStorage.setItem(LOCALE_STORAGE_KEY, locale)\n    }\n\n    this.locale_ = locale\n  }\n\n  /**\n   * `fetch` closely follows (and uses under the hood) the native `fetch` API. There are, however, few key differences:\n   * - Non 2xx statuses throw a `FetchError` with the status code as the `status` property, rather than resolving the promise\n   * - You can pass `body` and `query` as objects, and they will be encoded and stringified.\n   * - The response gets parsed as JSON if the `accept` header is set to `application/json`, otherwise the raw Response object is returned\n   *","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/medusajs/medusa/blob/5e06e544a296b9033f20f71f11c559f81a0e5739/packages/core/js-sdk/src/client.ts#L155-L191","documentation":"The JS SDK's setLocale persists the locale in the browser's localStorage so subsequent requests send x-medusa-locale. It checks for the `window` global; when the SDK runs server-side (SSR, Next.js server components, scripts) there is no window and the call just logs this warning and returns without doing anything — no request is made and no error is thrown.","triggerScenarios":"Calling sdk.setLocale('de') in code that executes in a Node/server runtime: getServerSideProps, Next.js server components, route handlers, unit tests, or SSR rendering of a storefront.","commonSituations":"Calling setLocale at module top-level of a component that is also server-rendered; shared SDK initialization code imported by both server and client bundles; SSR frameworks hydrating a component that calls setLocale during render.","solutions":["Pass the locale per request instead: configure the SDK fetch client to send the 'x-medusa-locale' header on the server","Move setLocale calls into client-only code (useEffect, onClick handlers, or components guarded by typeof window !== 'undefined')","Create separate SDK instances for server code with publishable key + custom headers, and keep the localStorage-based one for the browser"],"exampleFix":"// before\nexport const sdk = medusaSdk(...)\nsdk.setLocale(locale) // runs in Next.js server component\n\n// after (server)\nconst sdk = new Client({ baseUrl, publishableKey, customHeaders: { \"x-medusa-locale\": locale } })\n// after (client-only)\nuseEffect(() => { sdk.setLocale(locale) }, [locale])","handlingStrategy":"type-guard","validationCode":"if (typeof window !== \"undefined\") {\n  sdk.setLocale(locale)\n} else {\n  // pass header per request instead\n  client.setFetch((url, init) => fetch(url, { ...init, headers: { ...init?.headers, \"x-medusa-locale\": locale } }))\n}","typeGuard":"const isBrowser = (): boolean => typeof window !== \"undefined\"","tryCatchPattern":null,"preventionTips":["Never call client-only SDK helpers during SSR/module init","Keep one browser SDK instance and one server instance with explicit headers","Send x-medusa-locale explicitly in server-side requests"],"tags":["js-sdk","ssr","locale","client-only"],"backgroundTag":"browser-api-on-server","analyzedSha":"5e06e544a296b9033f20f71f11c559f81a0e5739","analyzedAt":"2026-08-27T07:24:39.599Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}