"TypeError: D is not a constructor" in version >=1.112.0
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
same here, I'm using nextjs 14.1.3
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.
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' });
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
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.
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.