The structured clone algorithm - Web APIs | MDN (original) (raw)
Things that don't work with structured clone
- Function objects cannot be duplicated by the structured clone algorithm; attempting to throws a
DataCloneErrorexception. - Cloning DOM nodes likewise throws a
DataCloneErrorexception. - Certain object properties are not preserved:
- The
lastIndexproperty of RegExp objects is not preserved. - Property descriptors, setters, getters, and similar metadata-like features are not duplicated. For example, if an object is marked readonly with a property descriptor, it will be read/write in the duplicate, since that's the default.
- The prototype chain is not walked or duplicated.
- Class private elements are not duplicated. (Although internal fields of built-in types may.)
- The
Supported types
JavaScript types
- Array
- ArrayBuffer
- Boolean
- DataView
- Date
- Error types (but see Error types below).
- Map
- Number
- Object objects: but only plain objects (e.g., from object literals).
- Primitive types, except
symbol. - RegExp: but note that
lastIndexis not preserved. - Set
- String
- TypedArray
Error types
For Error types, the error name must be one of: Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError (or will be set to "Error").
Browsers must serialize the properties name and message, and are expected to serialize other "interesting" properties of the errors such as stack, cause, etc.
AggregateError support is expected to be added to the specification in whatwg/html#5749 (and is already supported in some browsers).
Web/API types
- AudioData
- Blob
- CropTarget
- CryptoKey
- DOMException: browsers must serialize the properties name and message. Other attributes may also be serialized/cloned.
- DOMMatrix
- DOMMatrixReadOnly
- DOMPoint
- DOMPointReadOnly
- DOMQuad
- DOMRect
- DOMRectReadOnly
- EncodedAudioChunk
- EncodedVideoChunk
- FencedFrameConfig
- File
- FileList
- FileSystemDirectoryHandle
- FileSystemFileHandle
- FileSystemHandle
- GPUCompilationInfo
- GPUCompilationMessage
- GPUPipelineError
- ImageBitmap
- ImageData
- RTCCertificate
- RTCEncodedAudioFrame
- RTCEncodedVideoFrame
- VideoFrame
- WebTransportError
**Note:**Serializable objects are marked up in Web IDL files with the attribute [Serializable].