sanity-io / sanity-plugin-internationalized-array

A plugin to register array fields with a custom input component to store field values in multiple languages, queryable by using the language ID as an array `_key`.
MIT License
43 stars 11 forks source link

Initial Value Templates don't work if document contains internationalized array field #81

Open icelandcruiser opened 1 week ago

icelandcruiser commented 1 week ago

When using Initial Value Templates (https://www.sanity.io/docs/initial-value-templates), the plugin appears to overwrite, remove, or ignore the initial values. This behavior occurs regardless of whether the initial values are defined directly within the document schema or via the structure builder. When a document includes one or more internationalized array fields, the initial values fail to persist.

To Reproduce

  1. Define a schema with an initial value, following the example from the documentation (https://www.sanity.io/docs/initial-value-templates#b7d6d6ad2da6).
  2. Open a document in Sanity Studio and verify that the isHighlighted field (boolean) has its initial value set to false.
  3. Add an internationalized array field to the document schema.
  4. Reopen the document in Sanity Studio, the value for isHighlighted should be unset

Expected behavior The addition of internationalized array fields should not affect initial values.

Which versions of Sanity are you using? Sanity: 3.60.0 sanity-plugin-internationalized-array: 3.0.1

What operating system are you using? macOS Sonoma 14.5

Which versions of Node.js / npm are you running? Node: 9.8.1 npm: v18.18.2

icelandcruiser commented 1 week ago

I figured out the issue started with v1.7.0. The versions before that were fine. Oh and If I remove the defaultLanguages setting from sanity.config.ts, the latest version (3.0.1) works also...

nm2501 commented 22 hours ago

I can confirm that this is indeed a bug. Potentially also slightly non-deterministic. I observed that it very occasionally works, but most of the time the values from initialValue are not applied. Might also be helpful to know that when supplying a function to initialValue then that function is invoked even when the values don't end up being applied to the document.

icelandcruiser commented 21 hours ago

True, I've also seen it work maybe 1 in 10 times or so for some documents, but not at all for others. It seems like there's a race condition during initialization.