Skip to content

Live preview throws on localized group fields (afterRead treats flattened group as locale map when flattenLocales: false) #19479

Open

Description

@jheaminoff

Describe the Bug

Live preview fails for any collection or global that has a group field with localized: true. The preview request throws:

TypeError: Cannot create property 'items' on string 'plain'

@payloadcms/live-preview's mergeData posts the document that the admin has already flattened to one locale, with flattenLocales: false (added in #13794). findByID then runs afterRead on that data. In packages/payload/src/fields/hooks/afterRead/promise.ts, the group case does this:

if (shouldLocalizeField && !shouldHoistLocalizedValue) {
  Object.values(siblingDoc[field.name] || {}).forEach((localizedData) => {
    traverseFields({ siblingDoc: localizedData || {}, ... })

With flattenLocales: false, shouldHoistLocalizedValue is always false, so the already flattened group { mode: 'plain', items: [...] } is treated as a locale map. Each field value becomes a siblingDoc, and the first assignment onto the string 'plain' throws in strict mode. The named-tab case has the same pattern. For localized arrays and blocks the equivalent branch skips the rows (Array.isArray(localeRows) is false), so their relationships are probably not populated in the preview either, but I have not verified that.

Next.js production builds up to 16.4.3 apparently ran this code outside strict mode, so the assignment failed silently and preview appeared to work. From Next 16.4.4 the error is thrown and the preview pane stops updating. The script below reproduces it in plain Node, with no Next.js involved.

Link to the code that reproduces this issue

Self-contained script below (npm i [email protected] @payloadcms/[email protected] graphql, then node repro.mjs).

Reproduction Steps

// repro.mjs ("type": "module")
import { buildConfig, getPayload } from 'payload'
import { sqliteAdapter } from '@payloadcms/db-sqlite'

const config = buildConfig({
  secret: 'repro',
  db: sqliteAdapter({ client: { url: 'file:./repro.db' }, push: true }),
  localization: { locales: ['en', 'fi'], defaultLocale: 'en' },
  collections: [{
    slug: 'pages',
    fields: [{
      name: 'hero',
      type: 'group',
      localized: true,
      fields: [
        { name: 'mode', type: 'text' },
        { name: 'items', type: 'array', fields: [{ name: 'label', type: 'text' }] },
      ],
    }],
  }],
})

const payload = await getPayload({ config })
const page = await payload.create({ collection: 'pages', locale: 'en', data: { hero: { mode: 'plain', items: [{ label: 'a' }] } } })

// What live-preview mergeData sends: the flattened document with flattenLocales: false
const flattened = await payload.findByID({ collection: 'pages', id: page.id, locale: 'en', depth: 0 })
await payload.findByID({ collection: 'pages', id: page.id, locale: 'en', depth: 1, data: flattened, flattenLocales: false })
// -> TypeError: Cannot create property 'items' on string 'plain'

Expected: the second call returns hero unchanged (with relationships populated), as it does when the group is not localized.

Possible fix: only treat a localized value as a locale map when locales are not already flattened, e.g. shouldLocalizeField && flattenLocales && !shouldHoistLocalizedValue in the group and named-tab cases, and traverse array/block rows directly when flattenLocales is false.

Workaround we use: keep the group unlocalized and set localized: true on its fields. On Postgres the columns stay the same; only the relationship indexes inside the group gain _locale.

Related: #13794 (introduced flattenLocales: false for live preview), #18217 (same locale-map assumption in the traverseFields utility).

Which area(s) are affected?

area: core, plugin: live-preview

Environment Info

payload: 3.93.0 (also 3.90.2)
@payloadcms/live-preview: 3.93.0
@payloadcms/db-postgres / db-sqlite: 3.93.0
next: 16.4.4 – 16.4.6 fail in the admin; 16.4.0 – 16.4.3 hide the error
Node: 24 (Azure), 26 (local)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions