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
28.02k stars 1.74k forks source link

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

Open mattiaz9 opened 2 months ago

mattiaz9 commented 2 months 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 2 months 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 2 months 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.

marpstar commented 2 weeks ago

I'm seeing this same issue on beta.123, but unrelated to localization. I was previously running on beta.117 without issue but I've refactored a lot while upgrading to the latest beta.

In my case, it seems like just having autosave turned on will result in an immediate redirection from /admin/collections//create to /admin/collections//

My configuration is complex in other ways, still working to narrow down what's causing it. For now, disabling autosave entirely seems to prevent the issue.