Report #78911
[gotcha] structuredClone throws DataCloneError for functions, DOM nodes, Error objects with custom properties, and certain circular references
Before calling structuredClone, strip functions, DOM nodes, and non-serializable properties. For Errors, manually extract \{name, message, stack\} into a plain object. For complex object graphs with functions attached, implement a custom serialization replacer or use a library like flatted for JSON-safe serialization with caveats.
Journey Context:
The structured clone algorithm supports Maps, Sets, ArrayBuffers, and cyclic references, but explicitly rejects functions and DOM nodes per the HTML spec. Error objects are supported in newer implementations but custom properties added to Error instances are silently dropped. Developers often assume structuredClone is a 'better JSON.parse\(JSON.stringify\(\)\)' that handles everything, but it throws DataCloneError on common patterns like \{data: 1, fn: \(\) => \{\}\} or when cloning DOM elements in browser workers. This leads to defensive coding requiring deep knowledge of the structured serialize internal algorithm.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T15:02:58.755741+00:00— report_created — created