PostHog / posthog-js

Send usage data from your web app or site to PostHog, with autocapture.
https://posthog.com/docs/libraries/js
Other
271 stars 112 forks source link

RangeError: Maximum call stack size exceeded (Next.js App Dir) #1303

Open oalexdoda opened 2 months ago

oalexdoda commented 2 months ago

Using PostHog JS (React) with a Next.js (app dir) turbopack app. The posthog.capture function (as well as other internal ones like autocapture) trigger a maximum stack depth size exceeded error.

This seems to have been a bug for a very long time but it's uncaught. Sentry's billing is absolutely in pain when that error fires endlessly. How can we debug and prevent this?

Screenshot 2024-07-12 at 8 58 55 PM

pauldambra commented 2 months ago

Hey @oalexdoda

copying your question from the other issue you posted it

How can we add data-turbo-eval="false" while using the Next.js / React SDK? Not sure if related but this is happening on Next.js app dir turbopack. Absolutely destroys our Sentry events billing.

There'a a chance you can't but honestly it's a question for turbopack not for posthog

Turbopack is pretty clever - but it's cleverness means that it simply won't always work with all third party libraries.

I think that the only way to use turbopack with posthog (and other libraries too) is to include the snippet and mark it data-turbo-eval="false"

My understanding of turbopack is that this is a trade-off of using it. Some things will be faster with the trade-off some things will need work arounds.

I guess that's maybe a frustrating answer but you all know turbopack better than us so if there is a solution it's way more likely to come from turbopack or the turbopack community than from us.

pauldambra commented 4 weeks ago

closing since this isn't something we can fix in PostHog (I think)

happy to hear solutions from others if we're missing a trick...

lincolnremi commented 4 days ago

Is there a mixup here between Next.js turbopack and the Rails hotwired/turbo library? I'm not aware of any data-turbo-eval attribute that Next.js turbopack uses.

pauldambra commented 4 days ago

Ah, damn, sorry - pattern matching :)

in which case some public example or repro would be super helpful!

pauldambra commented 4 days ago

(have edited this misleading link out of the issue description)

neoromantic commented 4 days ago

Stumbled on this too.

module.js:1 [PostHog.js] [SessionRecording] could not emit queued rrweb event. RangeError: Maximum call stack size exceeded
...

Happens after numerous route changes on Next 15@canary with app router.

neoromantic commented 4 days ago

Happens sporadically, when i transition between pages.

For now, i couldn't identify the reason, but it happens only after 10-15 route changes.

Not every time, but pretty reproducible.

No idea how to debug...

pauldambra commented 4 days ago

Not every time, but pretty reproducible.

Is it possible you could create the reproduction @neoromantic - the ecosystem is pretty large and i could end up chasing shadows 🙈

What's interesting in the error you share is the 'rrweb queueing' is only intended as a stopgap. It's there because sometimes recording takes slightly longer to start and we try to poke custom events in before its ready. So it points at some other thing causing a problem with that message as a symptom