{"version":3,"file":"serverSideTranslations.cjs","names":["createConfig","createClient","path","unique"],"sources":["../../src/pagesRouter/serverSideTranslations.ts"],"sourcesContent":["import fs from 'fs'\nimport path from 'path'\nimport { createRequire } from 'module'\n\nimport { createConfig } from './config/createConfig'\nimport { applyServerSideConfig } from './config/serverSideConfig'\nimport createClient from './createClient/node'\n\nimport { UserConfig, SSRConfig } from './types'\nimport { getFallbackForLng, unique } from './utils'\nimport { Module, Namespace } from 'i18next'\n\nlet DEFAULT_CONFIG_PATH = './next-i18next.config.js'\n\n/**\n * One line expression like `const { I18NEXT_DEFAULT_CONFIG_PATH: DEFAULT_CONFIG_PATH = './next-i18next.config.js' } = process.env;`\n * is breaking the build, so keep it like this.\n *\n * @see https://github.com/i18next/next-i18next/pull/2084#issuecomment-1420511358\n */\nif (process.env.I18NEXT_DEFAULT_CONFIG_PATH) {\n  DEFAULT_CONFIG_PATH = process.env.I18NEXT_DEFAULT_CONFIG_PATH\n}\n\ntype ArrayElementOrSelf<T> = T extends ReadonlyArray<infer U> ? U[] : T[]\n\nexport const serverSideTranslations = async (\n  initialLocale: string,\n  namespacesRequired:\n    | ArrayElementOrSelf<Namespace>\n    | string\n    | string[]\n    | undefined = undefined,\n  configOverride: UserConfig | null = null,\n  extraLocales: string[] | false = false\n): Promise<SSRConfig> => {\n  if (typeof initialLocale !== 'string') {\n    throw new Error(\n      'Initial locale argument was not passed into serverSideTranslations'\n    )\n  }\n\n  let userConfig = configOverride\n  const configPath = path.resolve(DEFAULT_CONFIG_PATH)\n\n  if (!userConfig && fs.existsSync(configPath)) {\n    // Use createRequire to prevent Turbopack/webpack from tracing this dynamic require.\n    // Pass the absolute configPath itself as the base — works identically under CJS\n    // and ESM output and avoids referencing __filename / import.meta.url.\n    const nodeRequire = createRequire(configPath)\n    userConfig = nodeRequire(configPath)\n  }\n\n  if (userConfig === null) {\n    throw new Error(\n      `next-i18next was unable to find a user config at ${configPath}`\n    )\n  }\n\n  const config = createConfig(\n    {\n      ...userConfig,\n      lng: initialLocale,\n    },\n    { applyServerSideConfig }\n  )\n\n  const {\n    localeExtension,\n    localePath,\n    fallbackLng,\n    reloadOnPrerender,\n  } = config\n\n  const { i18n, initPromise } = createClient({\n    ...config,\n    lng: initialLocale,\n  })\n\n  await initPromise\n\n  const hasCustomBackend = userConfig?.use?.filter(Boolean).some(\n    (b: Module) => b.type === 'backend'\n  )\n  if (hasCustomBackend && namespacesRequired) {\n    await i18n.loadNamespaces(Array.isArray(namespacesRequired) ? (namespacesRequired as string[]) : (namespacesRequired as string))\n  }\n\n  const initialI18nStore: Record<string, any> = {\n    [initialLocale]: {},\n  }\n\n  getFallbackForLng(initialLocale, fallbackLng ?? false)\n    .concat(extraLocales || [])\n    .forEach((lng: string) => {\n      initialI18nStore[lng] = {}\n    })\n\n  if (!Array.isArray(namespacesRequired)) {\n    if (typeof localePath === 'function') {\n      throw new Error(\n        'Must provide namespacesRequired to serverSideTranslations when using a function as localePath'\n      )\n    }\n\n    const getLocaleNamespaces = (path: string) =>\n      fs.existsSync(path)\n        ? fs\n          .readdirSync(path)\n          .map(file => file.replace(`.${localeExtension}`, ''))\n        : []\n\n    const namespacesByLocale = Object.keys(initialI18nStore)\n      .map(locale =>\n        getLocaleNamespaces(\n          path.resolve(process.cwd(), `${localePath}/${locale}`)\n        )\n      )\n      .flat()\n\n    namespacesRequired = unique(namespacesByLocale)\n  }\n\n  // Dev-only hot-reload: every backend (resources-to-backend, http, locize,\n  // chained) refetches unconditionally with no dedup, so doing this in\n  // production would hammer the source on every prerender call. Scope to\n  // exactly the locales × namespaces this call will ship.\n  if (reloadOnPrerender && process.env.NODE_ENV !== 'production') {\n    await i18n.reloadResources(\n      Object.keys(initialI18nStore),\n      namespacesRequired as string[]\n    )\n  }\n\n  namespacesRequired.forEach(ns => {\n    for (const locale in initialI18nStore) {\n      initialI18nStore[locale][ns] =\n        (i18n.services.resourceStore.data[locale] || {})[ns] || {}\n    }\n  })\n\n  return {\n    _nextI18Next: {\n      initialI18nStore,\n      initialLocale,\n      ns: namespacesRequired,\n      userConfig: config.serializeConfig ? userConfig : null,\n    },\n  }\n}\n"],"mappings":";;;;;;;;;;;;AAYA,IAAI,sBAAsB;;;;;;;AAQ1B,IAAI,QAAQ,IAAI,4BACd,uBAAsB,QAAQ,IAAI;AAKpC,MAAa,yBAAyB,OACpC,eACA,qBAIgB,KAAA,GAChB,iBAAoC,MACpC,eAAiC,UACV;AACvB,KAAI,OAAO,kBAAkB,SAC3B,OAAM,IAAI,MACR,qEACD;CAGH,IAAI,aAAa;CACjB,MAAM,aAAa,KAAA,QAAK,QAAQ,oBAAoB;AAEpD,KAAI,CAAC,cAAc,GAAA,QAAG,WAAW,WAAW,CAK1C,eAAA,GAAA,SAAA,eADkC,WAAW,CACpB,WAAW;AAGtC,KAAI,eAAe,KACjB,OAAM,IAAI,MACR,oDAAoD,aACrD;CAGH,MAAM,SAASA,4BAAAA,aACb;EACE,GAAG;EACH,KAAK;EACN,EACD,EAAE,uBAAA,gCAAA,uBAAuB,CAC1B;CAED,MAAM,EACJ,iBACA,YACA,aACA,sBACE;CAEJ,MAAM,EAAE,MAAM,gBAAgBC,0BAAa;EACzC,GAAG;EACH,KAAK;EACN,CAAC;AAEF,OAAM;AAKN,KAHyB,YAAY,KAAK,OAAO,QAAQ,CAAC,MACvD,MAAc,EAAE,SAAS,UAC3B,IACuB,mBACtB,OAAM,KAAK,eAAe,MAAM,QAAQ,mBAAmB,GAAI,qBAAmC,mBAA8B;CAGlI,MAAM,mBAAwC,GAC3C,gBAAgB,EAAE,EACpB;AAED,eAAA,kBAAkB,eAAe,eAAe,MAAM,CACnD,OAAO,gBAAgB,EAAE,CAAC,CAC1B,SAAS,QAAgB;AACxB,mBAAiB,OAAO,EAAE;GAC1B;AAEJ,KAAI,CAAC,MAAM,QAAQ,mBAAmB,EAAE;AACtC,MAAI,OAAO,eAAe,WACxB,OAAM,IAAI,MACR,gGACD;EAGH,MAAM,uBAAuB,WAC3B,GAAA,QAAG,WAAW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