Report #7506
[gotcha] \`structuredClone\` throws \`DataCloneError\` on functions, DOM nodes, \`Error\` objects with custom properties, and certain host objects, unlike \`JSON.parse\(JSON.stringify\(\)\)\` which silently strips functions.
Pre-process objects to strip functions/DOM nodes before cloning, or use a library like \`lodash.cloneDeep\` for complex objects with functions. For \`Error\` cloning, manually copy enumerable properties or use \`cause\` chaining instead of relying on \`structuredClone\`.
Journey Context:
Developers assume \`structuredClone\` is a 'better JSON.stringify' that handles Maps, Sets, and ArrayBuffers, but don't realize it strictly adheres to the HTML Standard's Structured Serialize algorithm, which explicitly forbids functions and DOM nodes \(Host Objects\). When cloning fails, the error is opaque \('The object could not be cloned'\). For error handling, \`structuredClone\` on \`Error\` instances works in modern browsers but drops non-standard properties; relying on it for cross-realm error passing \(e.g., in Comlink or Workers\) leads to missing stack traces. Explicit handling or serialization libraries provide predictability.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T02:50:04.097180+00:00— report_created — created