Skip to content

fix(ui): prevent re-render cascade in UploadHandlersProvider#16088

Open
mahmoodhamdi wants to merge 1 commit intopayloadcms:mainfrom
mahmoodhamdi:fix/cloud-storage-memory-leak
Open

fix(ui): prevent re-render cascade in UploadHandlersProvider#16088
mahmoodhamdi wants to merge 1 commit intopayloadcms:mainfrom
mahmoodhamdi:fix/cloud-storage-memory-leak

Conversation

@mahmoodhamdi
Copy link
Copy Markdown
Contributor

What

Fixes the memory leak / browser crash when using multiple collections with a cloud storage plugin (e.g. s3Storage, azureStorage, gcsStorage).

Closes #16039

Why

UploadHandlersProvider used useState to store the handlers map. Each ClientUploadHandler provider (one per upload collection) calls setUploadHandler on mount, which triggers setState → a full re-render of the admin tree below it. With N collections, that's N state updates each causing a cascading re-render of the entire admin panel, plus N Map clone allocations.

The handlers map doesn't need to trigger re-renders — getUploadHandler is only called during form submission, never during rendering.

Changes

  • Replaced useState with useRef for the upload handlers map — registering a handler no longer triggers a re-render
  • Wrapped getUploadHandler and setUploadHandler in useCallback for stable references
  • Memoized the context value with useMemo to prevent unnecessary consumer re-renders

Testing

  • pnpm run build:ui passes with no errors
  • Verified that getUploadHandler is only consumed during form submission (Form/index.tsx:582) and bulk upload (FormsManager/index.tsx:362), never during render — so switching from state to ref is safe
  • The context type signature (UploadHandlersContext) and public API are unchanged

Replace useState with useRef for the upload handlers map to prevent
unnecessary re-renders when multiple cloud storage collections register
their handlers on mount.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory leak when using multiple collections in a cloud storage plugin

1 participant