payloadcms / payload

Payload is the open-source, fullstack Next.js framework, giving you instant backend superpowers. Get a full TypeScript backend and admin panel instantly. Use Payload as a headless CMS or for building powerful applications.
https://payloadcms.com
MIT License
23.46k stars 1.49k forks source link

Autosave makes it impossible to create new (localized) documents #8372

Open mattiaz9 opened 1 day ago

mattiaz9 commented 1 day ago

Link to reproduction

No response

Environment Info

Binaries:
  Node: 20.12.2
  npm: 10.5.0
  Yarn: N/A
  pnpm: 9.10.0
Relevant Packages:

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.6.0: Mon Jul 29 21:13:04 PDT 2024; root:xnu-10063.141.2~1/RELEASE_ARM64_T6020
  Available memory (MB): 16384
  Available CPU cores: 12

Describe the Bug

When creating a new document with autosave enabled, it creates a new document instantly, but not the relative localized fields records. This causes an instantaneous redirect to the /doc/{id} page that shows a not found message. I can see in the database a new record in the main document table, but not in the relative _locales table.

This a new bug that I personally encountered recently, either after upgrading to one of the latest beta or when I changed the idType to uuid.

Reproduction Steps

  1. enable autosave in the collection config:

    ...
    versions: {
    drafts: {
    autosave: {
      interval: 100, 
    },
    },
    }
    ...
  2. Enable localization in the payload config

    ...
    localization: {
    defaultLocale: "en",
    locales: [{ code: "en", label: "English" }, { code: "it", label: "Italian" }],
    },
    ...
  3. Add localized: true to some collection fields

  4. Try to create a new document

Adapters and Plugins

No response

r1tsuu commented 13 hours ago

Hey @mattiaz9 I did the same steps I cannot reproduce, using postgres with uuid as well. Can you share your collection config / confirm that it works on a minimal config?

mattiaz9 commented 9 hours ago

Hi @r1tsuu. It's funny, this morning I tried again and it didn't show the 404.

I noticed that the document is still created automatically. I can see 2 versions created immediately:

Screenshot 2024-09-24 at 08 37 44

If I open the first version it gives me this error: TypeError: Cannot convert undefined or null to object

In the second version a can see it changes some fields from null to empty value:

Screenshot 2024-09-24 at 08 37 57

I'm not sure why yesterday it showed me a 404 and today it doesn't, but still, creating the document during autosave, I believe could create some problems like this. It's probably best to disable creation when it is requested from an autosave.