Solved: Chrome, Web Workers, and DataCloneError (DOM Exception 25)

I recently put together a web app using web workers (which are cool), and noticed that it didn't work on the Chrome browser.  I've seen that Chrome can be a little stricter compared to other browsers sometimes - failing rather than just putting a warning in the console log and going on its way.

In this case, the message in the console was the following:

Uncaught Error: DataCloneError: DOM Exception 25


This seemed a little strange, as I was not explicitly or intentionally trying to clone any DOM elements, as far as I knew.

A quick google led me to this stack overflow post.  It turns out that the problem was that I was passing an object that had functions/methods from the web worker back to the main script (via postMessage).  The cross-process serialization mechanism for web workers can only handle "stringifiable" things.  The Safari browser can't do this, either, but it just silently sets those properties of the object to null rather than stop working altogether.  So, I added a method on the relevant object that returned its non-function properties, and passed that stuff back from the web worker.  It then worked fine on Chrome.

No comments:

Post a Comment

Popular Posts