Skip to content

reordering blocks using a beforeChange hook mixes up block types in the admin UI and can lead to corrupted documents #12368

Closed

Description

@ioExpander

Describe the Bug

I have a use case where I need to reorder blocks based on dates using a beforeChange hook.
However when reordering the blocks the UI seem to mix up blocks types and contents.

In the reproduction repo I have setup a very simple Pages example that sorts blocks in descending order and can reproduce the main issue.

In a more complex example I have managed to corrupt blocks data after the third save with an incorrect state in the admin UI.

Link to the code that reproduces this issue

https://github.com/ioExpander/payload-issue-blocksReorder.git

Reproduction Steps

pnpm install && pnpm dev
create .env file using .env.example
create admin account
create and save an empty page, then follow instructions to add blocks
Add block one, title "one" order "1", save the Page document
Add block two, title "two" order "2", save the page document

The blocks are saved but the admin UI is confused between blocks types (ie : block 01 is of type one but has block two contents)

Image

Adding block `three, title "three" order "3" etc will continue producing the same bug.

Which area(s) are affected? (Select all that apply)

area: ui

Environment Info

Binaries:
  Node: 20.19.0
  npm: 10.8.2
  Yarn: 1.22.22
  pnpm: 10.6.4
Relevant Packages:
  payload: 3.33.0
  next: 15.3.0
  @payloadcms/email-nodemailer: 3.33.0
  @payloadcms/graphql: 3.33.0
  @payloadcms/next/utilities: 3.33.0
  @payloadcms/payload-cloud: 3.33.0
  @payloadcms/richtext-lexical: 3.33.0
  @payloadcms/translations: 3.33.0
  @payloadcms/ui/shared: 3.33.0
  react: 19.1.0
  react-dom: 19.1.0
Operating System:
  Platform: linux
  Arch: x64
  Version: #32~22.04.1-Ubuntu SMP Thu Apr  3 20:26:27 UTC 2025
  Available memory (MB): 7938
  Available CPU cores: 2

Activity

  1. added
    status: needs-triage
    validate-reproduction
    on May 9, 2025
  2. added and removed
    validate-reproduction
    on May 9, 2025
  3. ioExpander commented on May 9, 2025

    @ioExpander
    ContributorAuthor

    this is the Pages collection config in the reproduction repo

    import type { CollectionConfig, Field } from 'payload'
    
    const blockFields: Field[] = [
      {
        name: 'title',
        type: 'text',
      },
      {
        name: 'order',
        type: 'number',
      },
    ]
    
    export const Pages: CollectionConfig = {
      slug: 'pages',
      fields: [
        {
          name: 'blocks',
          label: false,
          type: 'blocks',
          admin: {
            initCollapsed: true, // Collapse the blocks by default
            isSortable: true, // we'll try to sort the blocks by date on save once the bug is resolved
          },
          hooks: {
            beforeChange: [
              ({ value }) => {
                return value.sort((a, b) => {
                  return b.order - a.order
                })
              },
            ],
          },
          blocks: [
            {
              slug: 'one',
              fields: blockFields,
            },
            {
              slug: 'two',
              fields: blockFields,
            },
            {
              slug: 'three',
              fields: blockFields,
            },
            {
              slug: 'four',
              fields: blockFields,
            },
          ],
        },
      ],
    }
  4. ioExpander commented on Jun 4, 2025

    @ioExpander
    ContributorAuthor

    Hi. I've updated to the latest payload version (3.40.0) and unfortunately the issue is still present.
    I've bumped the packages in the reproduction repo as well.

    Is there any way I can help diagnose this issue further ? Thank you.

  5. JessRynkar commented on Jan 20, 2026

    @JessRynkar
    Member

    Hi! Apologies for the long delay in getting a response here, are you still experiencing this on the latest version v3.72.0?

  6. ioExpander commented on Jan 20, 2026

    @ioExpander
    ContributorAuthor

    Hi @JessRynkar . thank you for looking into this.
    Yes the issue is still present in v3.72.0. I've updated the repro repo as well.

    Image
  7. JessRynkar commented on Jan 20, 2026

    @JessRynkar
    Member

    Thank you for confirming, we'll get someone assigned to this.

  8. added a commit that references this issue on Nov 18, 2026
    3956aa5
  9. removed
    status: needs-triage
    on Nov 18, 2026
  10. github-actions commented on Nov 20, 2026

    @github-actions
    Contributor

    🚀 This is included in version v4.0.0-beta.11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions