Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Autosave failing when there are multiple drafts

default discord avatar
kumar_patellast year
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
    last year

    Hey

    @1056178491177046066

    what version of Payload are you on?

  • default discord avatar
    kumar_patellast year

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

    @969226489549713438

    .



    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
    last year

    Amazing!



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

  • default discord avatar
    kumar_patellast year

    Thank you! : )

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.