Skip to content

feat(ui): batch relationship value fetches to reduce N*M API requests#15758

Open
jhb-dev wants to merge 5 commits intopayloadcms:mainfrom
jhb-dev:feat/batch-relationship-fetches
Open

feat(ui): batch relationship value fetches to reduce N*M API requests#15758
jhb-dev wants to merge 5 commits intopayloadcms:mainfrom
jhb-dev:feat/batch-relationship-fetches

Conversation

@jhb-dev
Copy link
Contributor

@jhb-dev jhb-dev commented Feb 25, 2026

What?

Introduces a RelationshipValueCacheProvider that batches document title fetches across all RelationshipInput field instances. Instead of each relationship field making its own REST request on mount, all fields that mount in the same render tick have their ID lookups collected and fired as a single request per collection.

Why?

When relationship fields are placed inside arrays (or blocks), each row's relationship field independently fetches its referenced document title via REST. With N array rows and M relationship fields per row, this produces N*M parallel requests. This causes:

  • Severe UI performance degradation on documents with many array rows
  • MongoDB connection exhaustion on Vercel (hitting the 500 connection limit)
  • Unnecessary request bursts when drag-reordering array items

How?

  • New RelationshipValueCacheProvider (packages/ui/src/providers/RelationshipValueCache/index.tsx) uses queueMicrotask to collect all getDoc() calls within a single tick, then fires one batched request per collection using where: { id: { in: [...allIds] } }
  • Results are cached by collection:locale:id key so subsequent renders don't refetch
  • Cache is invalidated on document delete events, updated on document save events, and cleared entirely on locale changes
  • Collection fetches within a batch run in parallel via Promise.all
  • RelationshipInput checks the shared cache before requesting, and delegates uncached IDs to the batching provider
  • Provider is wired into the Root provider tree inside DocumentEventsProvider

Fixes #13329

@jhb-dev
Copy link
Contributor Author

jhb-dev commented Feb 25, 2026

The failing CI check ("E2E - versions 1/3") is a pre-existing flaky test on main — unrelated to the changes in this PR. The test (test/versions/e2e.spec.ts:548 — autosave "Create new" navigation timeout) also fails on the latest main commit.

I'd appreciate a review from the team to get your thoughts on this approach before moving forward.

@jhb-dev jhb-dev marked this pull request as ready for review March 1, 2026 18:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Performance issues with large array list

1 participant