Solution skill
RangeError: Maximum call stack size exceeded (Next.js App Dir)
Complete solution
Shows how to fix rangeError: Maximum call stack size exceeded (Next.js App Dir). Use it when you hit this exact problem. Skip it when your error message or symptom looks different.
TL;DR
For "RangeError: Maximum call stack size exceeded": root cause identified by reporters and confirmed by maintainers (pauldambra, lucasheriques): PostHog was initialized twice, once via the JS snippet (possibly through Google Tag Manager with an All Pages trigger that fires on every SPA navigation) and once via posthog.init() in code, creating an infinite re-init loop.
RangeError: Maximum call stack size exceededSteps
- Root cause identified by reporters and confirmed by maintainers (pauldambra, lucasheriques): PostHog was initialized twice, once via the JS snippet (possibly through Google Tag Manager with an
All Pagestrigger that fires on every SPA navigation) and once viaposthog.init()in code, creating an infinite re-init loop. Maintainer lucasheriques merged a guard against snippet double-install (Sept 2025) and marandaneto closed it as fixed. Agent rule: initialize exactly one way, snippet OR init(), never both, and for SPAs don't use GTM triggers that fire per client-side navigation.
When to use
You are seeing this: posthog-js in Next.js (App Dir) throws RangeError: Maximum call stack size exceeded from posthog.capture and other internals; in one case Sentry reported thousands of error events. Use this skill when you run into "RangeError: Maximum call stack size exceeded (Next.js App Dir)".
When not to use
If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.
Versions
No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.
Why this happens
The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.
Edge cases
- Also seen with Vue and in staging environments.
Evidence
In gh:PostHog/posthog-js#1303 (https://github.com/PostHog/posthog-js/issues/1303), the question was: posthog-js in Next.js (App Dir) throws RangeError: Maximum call stack size exceeded from posthog.capture and other internals; in one case Sentry reported thousands of error events. Also seen with Vue and in staging environments. The verified resolution: Root cause identified by reporters and confirmed by maintainers (pauldambra, lucasheriques): PostHog was initialized twice, once via the JS snippet (possibly through Google Tag Manager with an All Pages trigger that fires on every SPA navigation) and once via posthog.init() in code, creating an infinite re-init loop. Maintainer lucasheriques merged a guard against snippet double-install (Sept 2025) and marandaneto closed it as fixed.
By @issueatlas · Version 2 · Exact version
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_GARr-IDr5_396RGxXz0LxQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">RangeError: Maximum call stack size exceeded (Next.js App Dir) <span>— Vectle</span></a>