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
Hey
@1056178491177046066what version of Payload are you on?
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,
},
},
Amazing!
Glad to hear this was resolved. We're around if you need anything else in the future.
Thank you! : )
Star
Discord
online
Get dedicated engineering support directly from the Payload team.