Orderable collections throw `DuplicateFieldName` for `_order` on the second dev reload with server fast refresh

Aperta
#19,748 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@damnsamn ci sta già lavorando.

Dal 19/11/2026.

  • #19750 di @damnsamn — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
next.js, node.js, typescript
Ambito
backend, devtools

Direzione di ricerca

Start with packages/payload/src/config/orderable/index.ts, especially addOrderableFieldsAndHook, and review the reproducing test described for packages/payload/src/config/sanitize.spec.ts. Run pnpm vitest run --project unit packages/payload/src/config/sanitize.spec.ts and check repeated sanitization of the same orderable collection config. Done means the repeated pass does not add duplicate _order fields or ordering hooks and the third pass does not throw; an open pull request is already linked.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

area: core Bug created-by: Contributor Payload OSS
Describe the Bug

With Next.js server fast refresh on (the default since #18187) and reorderable collections enabled, the second Payload config reload in dev throws:

DuplicateFieldName: A field with the name '_order' was found multiple times on the same level. Field names must be unique.

From then on every getPayload call waits indefinitely (the reload promise in getPayload never settles after the throw), so every route hangs until the dev server restarts.

Cause: addOrderableFieldsAndHook (packages/payload/src/config/orderable/index.ts) unconditionally calls collection.fields.unshift(orderField) and collection.hooks.beforeChange.push(orderBeforeChangeHook). With server fast refresh, Next only re-evaluates changed modules, so an orderable collection whose module didn't change passes the same, already-sanitized config object into the next sanitizeConfig. The first reload gives it a second _order field and a second hook; the next reload's field sanitization throws.

v3 has the same code but isn't affected by default, as its withPayload still sets turbopackServerFastRefresh: false.

Fix: skip _order when the collection already has it, and replace the previous ordering hook instead of adding another. PR to follow.

Link to the code that reproduces this issue

https://github.com/damnsamn/payload-fork/commit/f1fdb82a51b2ba5fecb4dc2e6a596e317b119574

Reproduction Steps

The linked commit adds a failing test to packages/payload/src/config/sanitize.spec.ts that sanitizes the same orderable collection config twice, as a dev reload does for an unchanged module:

pnpm vitest run --project unit packages/payload/src/config/sanitize.spec.ts

It finds two _order fields after the second pass; a third pass throws DuplicateFieldName.

In an app:

  1. In a Payload v4 project on Next.js 16.3+, add a collection with orderable: true and any second collection.
  2. Run next dev.
  3. Save a change to the second collection's file, then request any route. This works.
  4. Save another change, then request any route. DuplicateFieldName is logged and every request hangs until restart.

Adding --no-server-fast-refresh to next dev avoids it.

Which area(s) are affected?

area: core

Environment Info
payload: 4.0.0-beta.9
@payloadcms/next: 4.0.0-beta.9
@payloadcms/db-mongodb: 4.0.0-beta.9
next: 16.4.5 (Turbopack)
node: 24.22.3

Code unchanged on main (7a41086).

Lingua principale
TypeScript
Stelle
45.8k
Fork
4.4k
Merge medio
3g 4h
PR unite (30g)
210

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di payloadcms/payload

Tutte le issue di payloadcms/payload

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.