Skip to content

Commit ebfb0eb

Browse files
fix(ui): fallback localization data was appearing in document (#11743)
1 parent 8a51fe1 commit ebfb0eb

File tree

4 files changed

+54
-28
lines changed

4 files changed

+54
-28
lines changed

packages/payload/src/utilities/addLocalesToRequest.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,39 @@ export function addLocalesToRequestFromData(req: PayloadRequest): void {
1414
} = req
1515

1616
if (data) {
17-
let localeOnReq = req.locale
18-
let fallbackLocaleOnReq = req.fallbackLocale
17+
const localeOnReq = req.locale
18+
const fallbackLocaleOnReq = req.fallbackLocale
19+
let localeFromData
20+
let fallbackLocaleFromData
1921

2022
if (!localeOnReq && data?.locale && typeof data.locale === 'string') {
21-
localeOnReq = data.locale
23+
localeFromData = data.locale
2224
}
2325

2426
if (!fallbackLocaleOnReq) {
2527
if (data?.['fallback-locale'] && typeof data?.['fallback-locale'] === 'string') {
26-
fallbackLocaleOnReq = data['fallback-locale']
28+
fallbackLocaleFromData = data['fallback-locale']
2729
}
2830

2931
if (data?.['fallbackLocale'] && typeof data?.['fallbackLocale'] === 'string') {
30-
fallbackLocaleOnReq = data['fallbackLocale']
32+
fallbackLocaleFromData = data['fallbackLocale']
3133
}
3234
}
3335

34-
const { fallbackLocale, locale } = sanitizeLocales({
35-
fallbackLocale: fallbackLocaleOnReq,
36-
locale: localeOnReq,
37-
localization: config.localization,
38-
})
36+
if (!localeOnReq || !fallbackLocaleOnReq) {
37+
const { fallbackLocale, locale } = sanitizeLocales({
38+
fallbackLocale: fallbackLocaleFromData,
39+
locale: localeFromData,
40+
localization: config.localization,
41+
})
3942

40-
if (locale) {
41-
req.locale = locale
42-
}
43-
if (fallbackLocale) {
44-
req.fallbackLocale = fallbackLocale
43+
if (localeFromData) {
44+
req.locale = locale
45+
}
46+
47+
if (fallbackLocaleFromData) {
48+
req.fallbackLocale = fallbackLocale
49+
}
4550
}
4651
}
4752
}

packages/payload/src/utilities/createPayloadRequest.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { getPayload } from '../index.js'
1111
import { sanitizeLocales } from './addLocalesToRequest.js'
1212
import { getRequestLanguage } from './getRequestLanguage.js'
1313
import { parseCookies } from './parseCookies.js'
14-
import { sanitizeFallbackLocale } from './sanitizeFallbackLocale.js'
1514

1615
type Args = {
1716
config: Promise<SanitizedConfig> | SanitizedConfig
@@ -66,18 +65,13 @@ export const createPayloadRequest = async ({
6665
: {}
6766

6867
if (localization) {
69-
fallbackLocale = sanitizeFallbackLocale({
70-
fallbackLocale,
71-
locale,
72-
localization,
73-
})
74-
7568
const locales = sanitizeLocales({
7669
fallbackLocale,
7770
locale,
7871
localization,
7972
})
8073

74+
fallbackLocale = locales.fallbackLocale
8175
locale = locales.locale
8276
}
8377

packages/payload/src/utilities/sanitizeFallbackLocale.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@ export const sanitizeFallbackLocale = ({
2020
locale,
2121
localization,
2222
}: Args): null | string => {
23-
const hasFallbackLocale =
24-
fallbackLocale === undefined || fallbackLocale === null
25-
? localization && localization.fallback
26-
: fallbackLocale
27-
? !['false', 'none', 'null'].includes(fallbackLocale)
28-
: false
23+
let hasFallbackLocale = false
24+
25+
if (fallbackLocale === undefined || fallbackLocale === null) {
26+
hasFallbackLocale = Boolean(localization && localization.fallback)
27+
}
28+
29+
if (fallbackLocale && !['false', 'none', 'null'].includes(fallbackLocale)) {
30+
hasFallbackLocale = true
31+
}
2932

3033
if (hasFallbackLocale) {
3134
if (!fallbackLocale) {

test/localization/e2e.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,30 @@ describe('Localization', () => {
556556

557557
await cdpSession.detach()
558558
})
559+
560+
test('should not show fallback data after saving data', async () => {
561+
await page.goto(url.create)
562+
await changeLocale(page, defaultLocale)
563+
await page.locator('#field-title').fill(title)
564+
565+
await saveDocAndAssert(page)
566+
await changeLocale(page, spanishLocale)
567+
568+
// POST data
569+
await page.locator('#field-description').fill('non-localized description')
570+
await saveDocAndAssert(page)
571+
572+
// POST updated data
573+
await page.locator('#field-description').fill('non-localized description 2')
574+
await saveDocAndAssert(page)
575+
576+
// The title should not have posted with a value
577+
await expect
578+
.poll(() => page.locator('#field-title').inputValue(), {
579+
timeout: POLL_TOPASS_TIMEOUT,
580+
})
581+
.not.toBe(title)
582+
})
559583
})
560584

561585
test('should use label in search filter when string or object', async () => {

0 commit comments

Comments
 (0)