Report #12589
[gotcha] structuredClone throws on functions and silently strips prototype chains from class instances
Use a library like devalue or superjson if you need to preserve class instances or handle functions; otherwise ensure your data contains only structured cloneable types \(plain objects, arrays, primitives\) and manually restore prototypes after cloning
Journey Context:
Developers often treat structuredClone as a drop-in replacement for libraries like lodash's cloneDeep, expecting it to replicate class instances with their methods intact. However, the Structured Clone Algorithm is strictly limited to transferable types for security and isolation \(e.g., for Web Workers\). When it encounters a class instance, it walks the enumerable properties but creates a plain Object with Object.prototype, stripping methods and the prototype chain. Functions are not cloneable and throw DataCloneError. This silently breaks code that sends Redux state or ORM models to Workers. The alternative is not to fix structuredClone \(it's doing its job\), but to use serialization libraries that support custom revivers/replacers \(like devalue\) or to explicitly reconstruct class instances after transfer.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T16:21:39.848845+00:00— report_created — created