-
Notifications
You must be signed in to change notification settings - Fork 18
Portable Text fields throw "Attempt to apply insert patch to non-array value" when pasting content #104
Description
Describe the bug
When using Portable Text fields (block content) with sanity-plugin-internationalized-array, pasting content into an empty field throws an error:
Error: Attempt to apply insert patch to non-array value
This happens specifically when:
- The field is wrapped with
internationalizedArray - The field type is a Portable Text array field (e.g.,
body,htmlContent) - The field is empty and you try to paste content into it
Root Cause
The plugin doesn't properly initialize the nested value field as an empty array for Portable Text fields. When Sanity's Portable Text editor tries to apply an InsertPatch operation to paste content, it expects an array but finds undefined or a non-array value.
To Reproduce
- Create a schema with an internationalized Portable Text field:
defineField({
name: 'body',
title: 'Body',
type: 'internationalizedArrayBody',
group: 'editorial',
})- Open a document with this field in Sanity Studio
- Try to paste any content into the empty Portable Text editor
- See error: "Attempt to apply insert patch to non-array value"
Expected behavior
The Portable Text field should accept pasted content without errors, just like non-internationalized Portable Text fields do.
Workaround Attempts
Tried several workarounds without success:
- Adding
initialValueto set empty arrays - Creating wrapper components to intercept and fix patches
- Attempting to monkey-patch
InsertPatch.apply
Which versions of Sanity are you using?
sanity: 3.62.3sanity-plugin-internationalized-array: 3.1.5
What operating system are you using?
macOS
Which versions of Node.js / npm are you running?
- Node: v20.11.1
- pnpm: 9.12.3
Additional context
This is a critical issue for content editors who rely on copy-paste workflows. The issue occurs specifically with array-based field types (Portable Text) when they're wrapped by the internationalized array plugin.
The fix likely needs to ensure that when a language tab is selected, the value field for array-based content types is properly initialized as an empty array [] rather than undefined.