Autosave failing when there are multiple drafts

default discord avatar
valyaiken
2 months ago
4

I'm getting the error

"There was a problem while autosaving this document."

whenever I have an existing draft and try to add a new record to a collection. The autosave only starts working when I press "publish changes".



Autosave failing when there are multiple drafts

  • discord user avatar
    seanzubrickas
    Payload Team
    2 months ago

    Hey @valyaiken what version of Payload are you on?

  • default discord avatar
    valyaiken
    2 months ago

    Hey Sean, I'm using the latest version of payload, however I've resolved the issue with the help of @dribbens.



    I was using the following collection configuration:



    import { CollectionConfig } from "payload/types"
    import columns from "../blocks/columns"
    import { formatSlug } from "../helpers"
    
    const Pages: CollectionConfig = {
        slug: "pages",
        admin: {
            useAsTitle: "title"
        },
        versions: {
            drafts: {
                autosave: true,
            },
        },
        fields: [
            {
                name: "title",
                type: "text",
            },
            {
                name: "excerpt",
                type: "textarea",
            },
            {
                name: "image",
                type: "upload",
                relationTo: "media",
            },
            {
                name: "slug",
                type: "text",
                unique: true,
                hooks: {
                    beforeChange: [formatSlug],
                },
                admin: {
                    position: "sidebar",
                    readOnly: true,
                },
            },
            {
                name: "content",
                type: "blocks",
                blocks: [columns],
            },
        ],
    }
    
    export default Pages


    The

    beforeChange

    hook in the slug field was making the autosave fail whenever there were multiple drafts present.



    I resolved it by replacing

    beforeChange

    with

    beforeValidate

    like follows:


            {
                name: "slug",
                type: "text",
                unique: true,
                hooks: {
                    beforeValidate: [formatSlug],
                },
                admin: {
                    position: "sidebar",
                    readOnly: true,
                },
            },
  • discord user avatar
    seanzubrickas
    Payload Team
    2 months ago

    Amazing!



    Glad to hear this was resolved. We're around if you need anything else in the future.

  • default discord avatar
    valyaiken
    2 months ago

    Thank you! : )

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.