Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Stop the dashboard reloading when a store speaks another language (#14456)

Damian Legawiec3a1ac2851a59
diff --git a/.changeset/dashboard-locale-reload-loop.md b/.changeset/dashboard-locale-reload-loop.md
new file mode 100644
index 0000000000..56ca327539
--- /dev/null
+++ b/.changeset/dashboard-locale-reload-loop.md
@@ -0,0 +1,9 @@
+---
+'@spree/dashboard-core': patch
+---
+
+Fix an endless reload when a store sets a non-English admin language.
+
+i18next was initialized with the stored language but only the English bundle, so it resolved the language to the fallback and reported English no matter what was stored. The store-default reconciler compares those two values to decide whether the page needs reloading, saw a permanent mismatch, and reloaded on every boot — each reload rotating a refresh token until the API's rate limit stopped it.
+
+Every bundle is now registered when i18next starts, so the stored language resolves to itself and the reconciler reloads only when the language genuinely changes.
diff --git a/packages/dashboard-core/src/lib/i18n.ts b/packages/dashboard-core/src/lib/i18n.ts
index 32504c4000..16f971e4ca 100644
--- a/packages/dashboard-core/src/lib/i18n.ts
+++ b/packages/dashboard-core/src/lib/i18n.ts
@@ -116,8 +116,9 @@ export function reconcileStoreDefaultLocale(
}
// All non-English core bundles, imported EAGERLY so the active language has its
-// resources synchronously available (registered just after init() below) —
-// no flash, no async race with module-load `i18n.t(...)` calls.
+// resources synchronously available when init() runs below — no flash, no async
+// race with module-load `i18n.t(...)` calls, and the stored language resolves
+// to itself rather than falling back.
const coreLocales = import.meta.glob<{ default: Record<string, unknown> }>(
['../locales/*.json', '!../locales/en.json'],
{ eager: true },
@@ -160,10 +161,23 @@ export function switchLocale(code: string): void {
// after this side-effect import has settled. The `deep` + `overwrite` flags
// merge the extension into the base namespace without dropping framework keys.
//
-// English ships eagerly; `lng` is read from localStorage so a previously
-// chosen language is the initial language on first paint (no flash).
+// `lng` is read from localStorage so a previously chosen language is the
+// initial language on first paint (no flash).
i18n.use(initReactI18next).init({
- resources: { en: { translation: en } },
+ // Every bundle is registered up front. Passing `lng` with only English
+ // loaded would resolve the language to the fallback and leave
+ // `resolvedLanguage` stuck on 'en' — which reads as "the stored language
+ // never took effect" and sends the store-default reconciler into a
+ // reload loop.
+ resources: {
+ en: { translation: en },
+ ...Object.fromEntries(
+ Object.entries(coreLocales).map(([path, mod]) => [
+ path.replace('../locales/', '').replace('.json', ''),
+ { translation: mod.default },
+ ]),
+ ),
+ },
lng: readStoredLocale(),
fallbackLng: 'en',
interpolation: { escapeValue: false },
@@ -176,12 +190,6 @@ i18n.use(initReactI18next).init({
: undefined,
})
-// Register the eager non-English core bundles now that i18next is initialized.
-for (const [path, mod] of Object.entries(coreLocales)) {
- const code = path.replace('../locales/', '').replace('.json', '')
- i18n.addResourceBundle(code, 'translation', mod.default, true, true)
-}
-
// Reflect the active language on the root <html> element so the document
// advertises the right `lang` (a11y, spellcheck, font selection) and `dir`
// (RTL for Arabic/Hebrew/etc.). `i18n.dir()` consults i18next's built-in RTL
diff --git a/packages/dashboard-core/tests/i18n-init.test.ts b/packages/dashboard-core/tests/i18n-init.test.ts
new file mode 100644
index 0000000000..869096eb87
--- /dev/null
+++ b/packages/dashboard-core/tests/i18n-init.test.ts
@@ -0,0 +1,65 @@
+import { createInstance } from 'i18next'
+import { describe, expect, it, vi } from 'vitest'
+
+/**
+ * `reconcileStoreDefaultLocale` decides whether to reload the page by comparing
+ * the store's language against `i18n.resolvedLanguage`. That comparison is only
+ * meaningful if the stored language actually resolved — and it resolves to the
+ * fallback when its bundle is missing at `init()` time.
+ *
+ * Registering bundles after init leaves `resolvedLanguage` on 'en' forever, so
+ * the reconciler sees a mismatch on every boot and reloads in a loop. These
+ * tests pin the ordering that avoids it.
+ */
+describe('i18n initialization ordering', () => {
+ it('resolves a non-English language when its bundle is present at init', async () => {
+ const i18n = createInstance()
+ await i18n.init({
+ resources: {
+ en: { translation: { hello: 'Hello' } },
+ pl: { translation: { hello: 'Cześć' } },
+ },
+ lng: 'pl',
+ fallbackLng: 'en',
+ })
+
+ expect(i18n.resolvedLanguage).toBe('pl')
+ expect(i18n.t('hello')).toBe('Cześć')
+ })
+
+ it('falls back to English when the bundle is registered only after init', async () => {
+ const i18n = createInstance()
+ await i18n.init({
+ resources: { en: { translation: { hello: 'Hello' } } },
+ lng: 'pl',
+ fallbackLng: 'en',
+ })
+ i18n.addResourceBundle('pl', 'translation', { hello: 'Cześć' }, true, true)
+
+ // The bundle is there, but the language resolved before it arrived — this
+ // is the state that made the reconciler reload endlessly.
+ expect(i18n.resolvedLanguage).toBe('en')
+ })
+
+ it('resolves the stored language, so the reconciler sees no mismatch', async () => {
+ // A store whose admin language is Polish, already adopted and persisted.
+ // The module reads this at import time to pick its initial language.
+ const store = new Map([
+ ['spree-admin-locale', 'pl'],
+ ['spree-admin-locale-auto-store', 'store_1'],
+ ])
+ vi.stubGlobal('localStorage', {
+ getItem: (key: string) => store.get(key) ?? null,
+ setItem: (key: string, value: string) => store.set(key, value),
+ removeItem: (key: string) => store.delete(key),
+ })
+
+ const { i18n, coreLocaleCodes } = await import('../src/lib/i18n')
+
+ expect(coreLocaleCodes()).toContain('pl')
+ // The guard inside reconcileStoreDefaultLocale is `target !== current`,
+ // where current is this value. It must equal the stored language or the
+ // page reloads on every boot, forever.
+ expect(i18n.resolvedLanguage ?? i18n.language).toBe('pl')
+ })
+})