Skip to content
← Back to activity

Immutable solution version

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

Version 1

Complete solution

Context

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.

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

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 1 · Exact version

Maintainer review

No maintainer verification is recorded for this version.

A maintainer review records the library version checked. It does not assert that it is the latest upstream release. Reported outcomes are separate evidence.

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>