Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
JavaScript

JavaScript: Fix structuredClone Throwing on an Object That Worked Fine With JSON.stringify Because It Contains a Value structuredClone Explicitly Refuses to Clone

- 10.11.26 - ErcanOPAK

⚡ Fix structuredClone Throwing on an Object That Worked Fine With JSON.stringify Because It Contains a Value structuredClone Explicitly Refuses to Clone

structuredClone and JSON.stringify/JSON.parse are often reached for interchangeably as a quick way to deep-copy an object, but they handle values neither can meaningfully represent in completely opposite ways: JSON.stringify silently drops a function property and converts an unsupported value into something nonsensical, all without ever throwing. structuredClone, built specifically as a real deep-clone algorithm rather than a text-serialization round-trip, explicitly throws a DataCloneError the moment it hits a value it can’t clone — a function, most DOM nodes, certain unsupported object types — so code switched from the old JSON-based copy trick to structuredClone for its better fidelity (real Dates, Maps, Sets, circular references) can start throwing on exactly the kind of object that used to ‘work’ simply because the old method never actually copied it correctly in the first place.

🔎 The Problem

const original = {
  name: 'Widget',
  onSave: () => console.log('saved'), // a function property
};

// Old approach - never threw, but silently dropped onSave entirely:
const copy1 = JSON.parse(JSON.stringify(original));
console.log(copy1); // { name: 'Widget' } - onSave is just gone, no error

// Switched to structuredClone for better fidelity:
const copy2 = structuredClone(original);
// Uncaught DOMException: Failed to execute 'structuredClone':
// function onSave could not be cloned.

✅ Fix: Strip Unclonable Values Deliberately Before Cloning

  • Explicitly remove or extract function properties and other unclonable values before calling structuredClone, rather than letting the clone call itself fail on data it was never going to meaningfully duplicate anyway.
  • If a function needs to travel alongside the cloned data, pass it separately rather than as a property of the object being cloned — structuredClone‘s error is actually correct here, since a function genuinely can’t be deep-cloned the way a plain object can.
  • Treat the switch from a JSON-based copy to structuredClone as a chance to audit what’s actually in the object being copied, since the JSON approach’s silent data loss was very likely already a latent bug that simply never surfaced as an error.

🚨 Why This Is Dangerous

  • Code that relied on the JSON round-trip’s silent dropping of a function property may have been shipping a copy that was already subtly wrong — missing a callback it should have preserved — for a long time before switching to a stricter cloning method finally made that gap visible as an actual error.

JSON.stringify’s silence about what it can’t represent isn’t a feature structuredClone copied — throwing loudly is the one case where the stricter tool is doing you a favor.

— JavaScript Tip

Related posts:

JavaScript: Why a Closure Inside a Loop Captures the Same Value on Every Iteration

JavaScript: Why a Closure Inside a Loop Captures the Same Value on Every Iteration

JavaScript: Fix a Debounced Function Still Firing Once per Call Instead of Once per Burst Because th...

JavaScript: Fix a Debounced Function Still Firing Once per Call Instead of Once per Burst Because th...

JavaScript: Use Rest Operator for Function Arguments

JavaScript: Use Rest Operator for Function Arguments

Views: 6

Post navigation

Photoshop: Fix an Entire Document Suddenly Looking Flat and Washed Out Because Proof Colors (Ctrl+Y) Got Toggled On by a Stray Keyboard Shortcut
HTML5: Fix the Browser’s Native Form Validation Message Never Appearing Because a novalidate Attribute Left on the Form From Earlier Testing Was Never Removed

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (857)
  • How to select distinct rows in a datatable in C# (854)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (653)
  • Add Constraint to SQL Table to ensure email contains @ (608)
  • Average of all values in a column that are not zero in SQL (570)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (546)
  • Find numbers with more than two decimal places in SQL (475)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit
  • SQL Server: Fix ROW_NUMBER Ties Reordering Between Identical Runs
  • C#: Fix an Enum HasFlag Returning True for the Zero Value
  • WordPress: Fix Featured Images Vanishing After a CDN Migration
  • Photoshop: Fix Content-Aware Fill Smearing a Repeating Pattern
  • Asp.Net Core: Fix Antiforgery Tokens Rejected Behind a Reverse Proxy

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (857)
  • How to select distinct rows in a datatable in C# (854)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com