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

Segment broke with next js 13.5.2 #969

Closed
AbdelrhmanX7 opened this issue Oct 13, 2023 · 3 comments
Closed

Segment broke with next js 13.5.2 #969

AbdelrhmanX7 opened this issue Oct 13, 2023 · 3 comments

Comments

@AbdelrhmanX7
Copy link

AbdelrhmanX7 commented Oct 13, 2023

i got this issue while i am using yarn build with next js 13.5.2
image

@silesky
Copy link
Contributor

silesky commented Oct 13, 2023

Thanks for opening an issue @AbdelrhmanX7. We made some changes that I think likely surfaced this issue recently, but It does looks like you're calling .track in a server environment. This is a no/no, particularly as this library is not isomorphic.

From 1.58.0 onward, I think it's extra important that you ensure you only call analytics.track() in a browser environment so you don't get runtime errors.

For more info, check out these next.js examples:
https://github.com/vercel/next.js/tree/canary/examples/with-segment-analytics
https://github.com/vercel/next.js/tree/canary/examples/with-segment-analytics-pages-router

@AbdelrhmanX7
Copy link
Author

I downgrade to segment v1.51 and everything works fine now and i am already using segment analytics.track in a client side and this issue just appeared from nothing and i don't even know what is the main reason for this issue everything was fine and suddenly everything stopped working

@silesky
Copy link
Contributor

silesky commented Oct 20, 2023

@AbdelrhmanX7

"document is not defined" means that it's being called in a server context. Please see those next.js examples I posted in my previous comment.

Older versions of this library may have seemed like they "worked" because this error didn't bubble up in your code, but calling track in a server context is never something we support, and can even be dangerous if it can result in fatal unhandled promises that could even crash future node app inadvertently. It's actually better to fail loudly.

Our position is basically that calling analytics methods in a server context represents a bug in one's code, so it's up to ths owner to fix it. Usually it's just following established patterns. e.g. call track in a useEffect hook, check for the existence of "window", etc. See Next.js examples.

@silesky silesky closed this as not planned Won't fix, can't repro, duplicate, stale Oct 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants