Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not work in Next.js serverless (API route) #255

Closed
2 of 4 tasks
antoinerousseau opened this issue Sep 9, 2024 · 1 comment · May be fixed by #256
Closed
2 of 4 tasks

Does not work in Next.js serverless (API route) #255

antoinerousseau opened this issue Sep 9, 2024 · 1 comment · May be fixed by #256
Labels
bug Something isn't working nodejs web

Comments

@antoinerousseau
Copy link

antoinerousseau commented Sep 9, 2024

Bug description

SSR support was supported in #57), but as mentioned here, the web version gets loaded instead of the Node.js version, and it tries to access the window object, throwing a ReferenceError: window is not defined from here, here or here.

How to reproduce

  1. Create a Next.js app router API route
import { PostHog } from "posthog-js-lite"
export async function GET() {
  const posthog = new PostHog(...)
  posthog.capture(...)
}
  1. Call the API route from a component: you get a ReferenceError: window is not defined

Related sub-libraries

  • All of them
  • posthog-web
  • posthog-node
  • posthog-react-native

Additional context

Related: #67 (comment)

@benjackwhite
Copy link
Collaborator

This lib really isn't designed for the use case in mind. Next-js router API is running server side and as such is basically a node-js environment and so should use the built posthog-node target instead as that way it includes concepts such as graceful shutdown and the correct fetch implementation for your node environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working nodejs web
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants