stitches: Stitches throwing exceptions "TypeError - Converting circular structure to JSON"

Bug report

Describe the bug

I’m raising this bug here as requested on discord 😃

Our team has been adopting stitches for a while and since release 0.2.0 we’ve started seeing exceptions in production that we can’t seem to replicate locally and we need some help

Basically we started seeing loads of instances of: 
 TypeError - Converting circular structure to JSON. .. at JSON.stringify 
 
The call stack lead us to stitches createMemo function.

To Reproduce

Although we see a really high number of exceptions, we cannot seem to reproduce this 😦 We forked the code and added a bunch of logs to try to get as much info as possible, see below.

Expected behavior

There should be no exceptions thrown

Screenshots / Information

This a screenshot another user on discord shared about the same issue:

Exception

System information

  • OS: both
  • Browser (if applies): It seems to occur in multiple browsers, safari, chrome and firefox (last 2 version) at least
  • Version of Stitches: 0.2.2
  • Version of Next.js: 9.x.x
  • Version of React: 16.13

Additional context

Since we could not replicate we forked the code to wrap that function in try catch, and the exceptions went away (we still got the error logs).

We also logged the object value failing to get stringified and we got this shape:

{
  "prefix": "",
  "media": "[object Object]",
  "root": "[object HTMLDocument]",
  "theme": "[object Object]",
  "themeMap": "[object Object]",
  "utils": "[object Object]"
}

After adding a few more logs, we zoomed in in on the root object, which seems to point at times to globalThis.document.

In our case the document does in in fact have a circular reference (there is a form on the page with internal react object with circular references)
. From what we gather from the logs we added, useMemo is stringifying an object of type HTMLDoc, which can in certain instances, contain circular references and throw exceptions.

We still can’t really replicate this issue locally, so any pointers or hints would be awesome

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 10
  • Comments: 17 (8 by maintainers)

Most upvoted comments

Ok, re-opening.

Thanks so much for this. Sounds serious. We’ll investigate!

@peduarte Thanks! Very helpful to know about that workaround with the version pinning. That should be enough for our project right now

Please reopen, this error is still existing, in 1.0.0 and 1.1.0.

This bug happens when you stringify Window object (JSON.stringify(window)).

Really don’t have time to do a PR, so let me quickly describe why this bug occurs:

createStitches function calls createCreateThemeFunction with input parameter config that has root property of type HTMLDocument. This config gets serialized with JSON.strinfigy inside. Some 3rd code can add anything to globalThis.document. Like an object with reference to a window. For example HotJar does that. And then it crashes.

LInk to problematic line of code: https://github.com/modulz/stitches/blob/52b781883b506b453cdb013fe098ed9176f3539b/packages/core/src/createStitches.js#L47

@peduarte do you need to serialize config.root at all? Didn’t go that deep. Maybe that’s a simple fix. Serialising entire document seems very dangerous and it definitely shouldn’t be there.