posthog-js icon indicating copy to clipboard operation
posthog-js copied to clipboard

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

Open oalexdoda opened this issue 1 year ago • 21 comments

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

oalexdoda avatar Jul 12 '24 20:07 oalexdoda

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 avatar Jul 13 '24 10:07 pauldambra

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...

pauldambra avatar Aug 19 '24 21:08 pauldambra

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.

lincolnremi avatar Sep 13 '24 20:09 lincolnremi

Ah, damn, sorry - pattern matching :)

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

pauldambra avatar Sep 13 '24 20:09 pauldambra

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

pauldambra avatar Sep 13 '24 21:09 pauldambra

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 avatar Sep 14 '24 05:09 neoromantic

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...

neoromantic avatar Sep 14 '24 05:09 neoromantic

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

pauldambra avatar Sep 14 '24 08:09 pauldambra

Can report it's happening in Pages router as well, usually during a heavy rerender Screenshot 2024-09-24 at 10 46 24 AM

jonahallibone avatar Sep 24 '24 14:09 jonahallibone

I am seeing this on our nextjs site, no idea how to debug.

image

lukebelbina avatar Oct 03 '24 16:10 lukebelbina

hey @lukebelbina is the site public for us to test? does it happen in prod or local dev or...?

pauldambra avatar Oct 03 '24 17:10 pauldambra

Facing the same issue on our Vue.js setup. Same stack trace as shown by @lukebelbina Currently, the issue only appears in our staging environment.

Raj1v avatar Oct 15 '24 08:10 Raj1v

Hey @Raj1v

Is there any more information you can share?

I'd love to figure out what triggers this - even if it's outside of PostHog - but at the moment we don't have anything reproducible we can look into.

pauldambra avatar Oct 15 '24 08:10 pauldambra

@pauldambra Yes, the error seems to originate from the _execute_array function. In my understanding, this function sends over events triggered before Posthog got initialized.

I placed a breakpoint in the function (line 687 in screenshot), which is indeed hit many times before the call stack gets exceeded. The breakpoint on line 689 is never hit.

Screenshot 2024-10-15 at 10 21 49 AM

Raj1v avatar Oct 15 '24 08:10 Raj1v

Oh fascinating! That code has been there since day 0 🤯

It looks like the array is a single item so my gut feel is that it's not the content of the array. Can you share your snippet?

pauldambra avatar Oct 15 '24 08:10 pauldambra

import posthog from 'posthog-js'

export default {
	install(app) {
		/**
		 * @type {import('posthog-js').PostHog}
		 */
		const instance = app.config.globalProperties.$posthog = posthog.init(import.meta.env.VITE_POSTHOGKEY, {
			api_host: 'XXXX', // Reverse proxy hosted on Azure Front Door
			ui_host: 'https://eu.posthog.com',
			capture_pageview: false,
			person_profiles: 'identified_only', 
		})
		app.provide('posthog', instance)
		app.config.globalProperties.$posthog = instance;
		window.posthog = instance
	},
}

Using the Vue JS plugin setup from the docs.

Stopping to emit the $pageview event on route changes stops the errors.

Raj1v avatar Oct 15 '24 08:10 Raj1v

Browser also seems to affect the issue. On Safari, the error triggers consistently on each page load. On Chome only occasionally .

Raj1v avatar Oct 15 '24 09:10 Raj1v

But not in your production build?

pauldambra avatar Oct 15 '24 10:10 pauldambra

@pauldambra Correct, not on production Building for production and serving locally also triggers the error. Some $autocapture events also trigger the event, so not exclusively $pagecapture

Really a head-scratcher 🤯

Raj1v avatar Oct 15 '24 10:10 Raj1v

Turns out it was an accidental double initialization from the JS SDK. Once through Google Tag Manager, and once through Vue.

Raj1v avatar Oct 15 '24 21:10 Raj1v

Maybe all it needs is a throttle on the session recordings side and to check if an instance already exists before re-initializing it.

oalexdoda avatar Oct 22 '24 15:10 oalexdoda

Also experiencing this issue!

posthog-js: v1.214.0 next:15.1.0

The error appears after rapid route changes caused, in our case caused by search params updating when tanstack table filters / pagination controls are updated. Setting disable_session_recording=true resolves the issue.

Image Image

crcorbett avatar Feb 03 '25 03:02 crcorbett

So can we have an answer on this issue ? I'm experiencing the exact same thing (with a simple Vite React App)

yonitou avatar Mar 16 '25 14:03 yonitou

Just received Maximum call stack size exceeded in Next.js App Dir. Sentry reported over 1600 events!

I'm happy to share my stack trace:

RangeError: Maximum call stack size exceeded
  at console.overrideMethod(app:///build/installHook.js:1:146977)
  at (./node_modules/.pnpm/@[email protected]/node_modules/@sentry/core/build/esm/utils-hoist/instrument/console.js:36:1)
  at t._log(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:1752)
  at t.warn(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:1960)
  at iq._tryRRWebMethod(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:58871)
  at iq._tryAddCustomEvent(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:58951)
  at iq._pageViewFallBack(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:64033)
  at iq.onRRwebEmit(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:61962)
  at this.stopRrwebemit(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:60599)
  at At(app:///static/recorder.js:1:74046)
  at Wt.addCustomEvent(app:///static/recorder.js:1:79813)
  at iq._tryAddCustomEvent(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:58951)
  at iq._tryRRWebMethod(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:58685)
  at iq._tryAddCustomEvent(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:58951)
  at iq._pageViewFallBack(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:64033)
  at iq.onRRwebEmit(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:61962)
  at this.stopRrwebemit(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:60599)
  at At(app:///static/recorder.js:1:74046)
  at Wt.addCustomEvent(app:///static/recorder.js:1:79813)
  at iq._tryAddCustomEvent(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:58951)
  at iq._tryRRWebMethod(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:58685)
  at iq._tryAddCustomEvent(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:58951)
  at iq._pageViewFallBack(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:64033)
  at iq.onRRwebEmit(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:61962)
  at this.stopRrwebemit(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:60599)
  at At(app:///static/recorder.js:1:74046)
  at Wt.addCustomEvent(app:///static/recorder.js:1:79813)
  at iq._tryAddCustomEvent(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:58951)
  at iq._tryRRWebMethod(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:58685)
  at iq._tryAddCustomEvent(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:58951)
  at iq._pageViewFallBack(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:64033)
  at iq.onRRwebEmit(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:61962)
  at this.stopRrwebemit(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:60599)
  at At(app:///static/recorder.js:1:74046)
  at Wt.addCustomEvent(app:///static/recorder.js:1:79813)
  at iq._tryAddCustomEvent(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:58951)
  at iq._tryRRWebMethod(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:58685)
  at iq._tryAddCustomEvent(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:58951)
  at iq._pageViewFallBack(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:64033)
  at iq.onRRwebEmit(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:61962)
  at this.stopRrwebemit(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:60599)
  at At(app:///static/recorder.js:1:74046)
  at Wt.addCustomEvent(app:///static/recorder.js:1:79813)
  at iq._tryAddCustomEvent(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:58951)
  at iq._tryRRWebMethod(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:58685)
  at iq._tryAddCustomEvent(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:58951)
  at iq._pageViewFallBack(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:64033)
  at iq.onRRwebEmit(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:61962)
  at this.stopRrwebemit(./node_modules/.pnpm/[email protected]_@[email protected]/node_modules/posthog-js/dist/module.js:1:60599)
  at At(app:///static/recorder.js:1:74046)
Relevant Packages:
  next: 15.3.0-canary.18 // Latest available version is detected (15.3.0-canary.18).
  react: 19.0.0
  react-dom: 19.0.0
  typescript: 5.8.2
  posthog-js: 1.232.3
  posthog-node: 4.10.1

typeofweb avatar Mar 22 '25 09:03 typeofweb

Would love to have a fix for this, not the error you want to run into on a friday night.

Chhinna avatar Mar 28 '25 17:03 Chhinna

What is the status of this issue?

velevtzvetlin avatar Apr 18 '25 00:04 velevtzvetlin

Also having issue mentioned above "[PostHog.js] [SessionRecording] could not emit queued rrweb event. RangeError: Maximum call stack size exceeded"

martinstepanek avatar Apr 23 '25 16:04 martinstepanek

I'm also experiencing the same issue.

mohamedary avatar Apr 28 '25 13:04 mohamedary

I appreciate people adding that they're experiencing something,,, but we don't know what the issue is and it doesn't occur for everyone

so we really need more info on your site, your posthog setup, or what is happening when it triggers for you

pauldambra avatar Apr 28 '25 13:04 pauldambra

@pauldambra you can try our live site: https://gamescript.ai where this error regularly occurs. Hopefully inspecting the index file source you'll be able to see our calls. We are using the recommended web snippet.

Our setup is: Axum, Dioxus (wasm)

But from the browser perspective (where this script executes) it's just html/css/js/wasm.

raxosiris avatar May 15 '25 13:05 raxosiris