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

"TypeError: D is not a constructor" in version >=1.112.0

Open ben-roth- opened this issue 1 year ago • 3 comments

I've noticed in my project (a Remix v2 app) that a posthog-js version >=1.112.0 causes an error when I initially load the webpage, but then on refresh everything seems to be working fine. I'm getting the following error:

[1] TypeError: D is not a constructor
[1]     at xhr (/Users/ben.roth/code/sunstate-website/node_modules/posthog-js/src/request.ts:100:17)
[1]     at request (/Users/ben.roth/code/sunstate-website/node_modules/posthog-js/src/request.ts:45:16)
[1]     at e._send_request (/Users/ben.roth/code/sunstate-website/node_modules/posthog-js/src/posthog-core.ts:580:9)
[1]     at e.call (/Users/ben.roth/code/sunstate-website/node_modules/posthog-js/src/decide.ts:36:23)
[1]     at e._loaded (/Users/ben.roth/code/sunstate-website/node_modules/posthog-js/src/posthog-core.ts:517:30)
[1]     at e._init (/Users/ben.roth/code/sunstate-website/node_modules/posthog-js/src/posthog-core.ts:459:18)
[1]     at e.value (/Users/ben.roth/code/sunstate-website/node_modules/posthog-js/src/posthog-core.ts:279:25)
[1]     at /Users/ben.roth/code/sunstate-website/node_modules/posthog-js/react/dist/umd/index.js:30:47
[1]     at Object.useMemo (/Users/ben.roth/code/sunstate-website/node_modules/react-dom/cjs/react-dom-server-legacy.node.development.js:5128:19)
[1]     at Object.useMemo (/Users/ben.roth/code/sunstate-website/node_modules/react/cjs/react.development.js:1650:21)

If I force version 1.111.3 everything works fine

ben-roth- avatar Mar 14 '24 00:03 ben-roth-

same here, I'm using nextjs 14.1.3

goenning avatar Mar 15 '24 10:03 goenning

Same here. 1.112.0 introduced XMLHttpRequest, and it does not work in all environments (e.g. service workers in Chrome v3 extensions).

Edit: It does work if one uses api_transport: 'fetch' when initializing.

mb8z avatar Mar 16 '24 01:03 mb8z

To add to @mb8z helpful answer, the exact syntax that worked for me is:

posthog.init('<your code>', { api_host: 'https://us.i.posthog.com', api_transport: 'fetch' });

wanderingstan avatar May 01 '24 20:05 wanderingstan

Catching up with this. We have used XMLHttpRequest since forever. Wondering what changed such that it actually caused issues here...

I'll check out and see if we can somehow more aggressively swap to fetch if XMLHttpRequest isn't available

benjackwhite avatar May 07 '24 14:05 benjackwhite

Just merged a change that should be an improvement in terms of how the api transport is selected which hopefully means this doesn't happen. The various environments aren't always reliable when it comes to checking for whether a function is supported but lets see...

And indeed, explicitly setting it to fetch will use fetch every time.

benjackwhite avatar May 08 '24 07:05 benjackwhite

Hi! We tried adding api_transport: 'fetch' to Posthog initialization and it got rid of the console errors 😄 However, when we did this it broke all of our session replays, because all the recordings started being only a white screen or a black screen 😢 . We had to revert changes in order to fix the session replays.

AndresGonzalez5 avatar May 15 '24 16:05 AndresGonzalez5