Skip to content

beforeValidate hook changes still require page refresh to reflect changes with versions enabled. #10515

Closed

Description

@HarleySalas

Describe the Bug

I celebrated too early upon seeing #9691 and #10268, however, the issue still persists for me when versions are enabled for a collection.

when beforeValidate hooks run, their changes are not reflected in the UI until a user manually refreshes the page.

Link to the code that reproduces this issue

https://github.com/HarleySalas/payload-3-reproductions/tree/before-validate-requires-refresh

Reproduction Steps

clone the repo. sqlite contains the necessary data. You will be autologged in and you can visit the page collection to observe the behavior by removing the slug field and saving the document to get the slug to be generated via the Title field. Removing versions results in it working fine. As soon as it's got versions enabled, it just stops working and requires a full refresh to see changes reflected on the page.

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

area: core

Environment Info

Payload: 3.16.0
Node: 22.2.0
SQLite, Postgres 16

Activity

  1. added
    status: needs-triage
    validate-reproduction
    on Jan 11, 2025
  2. added and removed
    validate-reproduction
    on Jan 11, 2025
  3. caseyszilagyi commented on Jan 15, 2025

    @caseyszilagyi

    I'm also having the same issue on 3.16.0. I have versions, drafts, and autosave enabled.

    I also experienced the same thing with the beforeChange hook

  4. jmikrut commented on Jan 15, 2025

    @jmikrut
    Member

    I plan to look into this ASAP - just giving a quick update here

  5. jeremyProwseYS commented on Jan 23, 2025

    @jeremyProwseYS

    Just to chime in here with a few of my findings related to this issue:

    Alongside the issues with the Admin UI not reflecting changes via hooks when autosave is enabled, hooks sometimes receive stale content from other related resources / fields.

    Reproduction Steps

    This is pretty easy to replicate using the default website template, here are a couple of examples:

    Example 1 (Admin UI not updating)

    Use the value of the "Title" field in a hook to update the SEO "Title" field:

    • Add a basic beforeValidation hook to the existing MetaTitleField function in the pages collection:
    ...,
    overrides: {
      hooks: {
        beforeValidate: [({ data, value }) => (value = data?.title || '')],
      },
    }
    ...
    
    • The meta "Title" field wont update until the page is refreshed.

    Example 2 (data exposed to hooks is stale)

    Using the included nested docs plugin:

    • Enable nested docs for the pages collection in src/plugins/index.ts.
    • Add a field with a basic beforeValidation hook like so:
    ...,
    {
      name: 'uri',
      type: 'text',
      hooks: {
        // use the last breadcrumb's url as the uri for this doc.
        beforeValidate: [({ data, value }) => (value = data?.breadcrumbs?.pop()?.url || '')],
      },
    },
    ...
    
    • Create a new page and add a title.
    • Breadcrumbs via the nested docs plugin wont update until refresh.
    • The newly added URI field wont update even after refresh.
    • The URI field will only update after another save and refresh.

    Hypothesis / Findings

    Disabling autosave and following the same step - instead this time clicking "Save Draft" rather than relying on autosave results in all fields being filled out correctly with no need for a refresh or additional save.

    The issues with the nested docs plugin are far more impactful than just the admin ui not updating, digging deeper into the records returned when querying breadcrumbs on a draft doc show that the initial draft doc is returned and not the most recent autosave. Again, this is not the result when using the manual "Save Draft" button instead of autosave.

    Further Context

    To provide some context, I've tested this with the latest releases of Payload (at the time of writing) along with testing in Payload 2.0 and can see these issues are also present there. A best guess is some issue with the autosave function itself and how it bubbles events. As stated, without autosave enabled, when the standard "Save Draft" button is used, hooks work as expected, so this (as far as I can tell) points to something in the event lifecycle for autosave.

    Payload: 3.18.0
    @payloadcms/plugin-nested-docs: 3.18.0
    Node: 20-Alpine
    Mongo
    

    If any further info or context is required please just let me know, I'm happy to provide more examples.
    Thanks for your continued work on this awesome CMS Payload team <3

  6. Sandro48 commented on Jan 26, 2025

    @Sandro48

    Ah yes we have pretty much the exact same findings as jeremy, +1

  7. matteo-naif commented on Feb 12, 2025

    @matteo-naif

    Also here the same problem

  8. tobedamobe commented on Mar 5, 2025

    @tobedamobe

    Here too!

  9. ryanovas commented on Apr 2, 2025

    @ryanovas

    Having the same problem on 3.32.0. Can confirm that disabling autosave fixes it for me as well.

  10. christophemenager commented on Apr 5, 2025

    @christophemenager

    Same issue for me using 3.33.0, took me quite some time to find this issue and figure out this was a bug :/

  11. self-assigned this
    on May 9, 2025
  12. removed
    status: needs-triage
    on May 9, 2025
  13. removed their assignment
    on May 13, 2025
  14. d717an commented on May 23, 2025

    @d717an

    Same issue for us on v3.36.1, though we're using a beforeChange hook. Disabling autosave fixes the issue.

  15. lcnogueira commented on Jun 8, 2025

    @lcnogueira
    Contributor

    I also see this issue on 3.41.0 if using beforeChange AND using versions.drafts.autosave: true. If I disable autosave, it works as expected.

  16. Borcsa134 commented on Jun 23, 2025

    @Borcsa134

    I also have this issue on v.3.36.1, any progress on this?

  17. self-assigned this
    on Jun 27, 2025
  18. jacobsfletch commented on Aug 11, 2025

    @jacobsfletch
    Member

    I've got a fix queued up here: #13416

  19. added a commit that references this issue on Aug 11, 2025
    1d81b0c
  20. github-actions commented on Aug 13, 2025

    @github-actions
    Contributor

    🚀 This is included in version v3.51.0

  21. github-actions commented on Aug 21, 2025

    @github-actions
    Contributor

    This issue has been automatically locked.
    Please open a new issue if this issue persists with any additional detail.

  22. locked as resolved and limited conversation to collaborators on Aug 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions