PostHog / posthog-node

Official PostHog Node library
MIT License
21 stars 19 forks source link

Axios causing issues when using `post hog-node` in Next.js 12 middleware #60

Closed weyert closed 2 years ago

weyert commented 2 years ago

Last month Vercel introduced middleware in their latest version of Next.js version 12. The middleware is using edge functions on their hosting environment but when you self-host on example GKE it also works but the same limitations apply.

If I am using posthog-node I am getting the following error:

Error: Error: Request to api/feature_flag failed with error: adapter is not a function
    at FeatureFlagsPoller._request (webpack-internal:///./node_modules/posthog-node/feature-flags.js:158:19)
    at async FeatureFlagsPoller._loadFeatureFlags (webpack-internal:///./node_modules/posthog-node/feature-flags.js:91:25)
    at async FeatureFlagsPoller.loadFeatureFlags (webpack-internal:///./node_modules/posthog-node/feature-flags.js:78:13)
    at async FeatureFlagsPoller.isFeatureEnabled (webpack-internal:///./node_modules/posthog-node/feature-flags.js:36:9)
    at async PostHog.isFeatureEnabled (webpack-internal:///./node_modules/posthog-node/index.js:255:16)
    at async Object.middleware [as handler] (webpack-internal:///./src/pages/_middleware.ts:26:28)
    at async adapter (webpack-internal:///./node_modules/next/dist/server/web/adapter.js:30:22)
    at async DevServer.runMiddleware (/Users/weyertdeboer/Development/Projects/xxx/app/projects/project/node_modules/next/dist/server/next-server.js:438:26)
    at async DevServer.runMiddleware (/Users/weyertdeboer/Development/Projects/xxx/app/projects/project/node_modules/next/dist/server/dev/next-dev-server.js:394:28)
    at async Object.fn (/Users/weyertdeboer/Development/Projects/xxx/app/projects/project/node_modules/next/dist/server/next-server.js:817:34)

I think this due to the use of Axios which is using XMLHttpRequest and this is not supported by the middleware functionality or their edge functions. There is a similar ticket at Next.js discussing this issue: https://github.com/vercel/next.js/issues/30932

I am wondering if it's worth it to rewrite the library in a different library that might use fetch instead my limited scan of the code Axios appears to be mostly used for:

I am happy to raise a PR to make these changes if people at PostHog are happy with this

yakkomajuri commented 2 years ago

Sure, this could make sense!

chris-aeviator commented 2 years ago

what's the status of this?