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

Issue when importing @aptabase/web on server #7

Closed
ngregrichardson opened this issue Jul 19, 2024 · 1 comment · Fixed by #8
Closed

Issue when importing @aptabase/web on server #7

ngregrichardson opened this issue Jul 19, 2024 · 1 comment · Fixed by #8

Comments

@ngregrichardson
Copy link
Contributor

Loving Aptabase so far, but running into an annoyance with using this SDK with SSR frameworks (specifically SvelteKit). I want to exclusively run events clientside, so I make sure to run init and trackEvent only on the client. However, functions like getBrowserLocale are called on import to create defaultLocale (https://github.com/aptabase/aptabase-js/blob/main/packages/shared.ts#L1), which causes an error since navigator.languages does not exist (TypeError: Cannot read properties of undefined (reading 'length'))

Would you accept a PR to either:

  1. Make sure navigator.languages exists before checking length

or

  1. defaultLocale is a "singleton" that is first created when it is requested (which seems to only be in sendEvent()

I'd be happy to do the work, just want to chat about how it should be done (if at all)

@ivnbogdan
Copy link
Contributor

Thanks for using Aptabase and for reporting this!

This definitely needs a fix, and it probably makes more sense to read the locale value as late as possible - so like you mentioned in option 2, on sendEvent.
That way when sendEvent is called and is executed on client side navigator.languages should be available.

Adding an extra check with navigator.languages?.length also helps, but it doesn't result in the correct language being loaded, so would go with reading it on sendEvent primarily

ngregrichardson added a commit to ngregrichardson/aptabase-js that referenced this issue Jul 19, 2024
`shared.ts` currently reads the default browser locale and is debug on import, which causes
issueswhen imported in a server component. This moves the reads for those to whenever the first
event issent and those values are actually required, which is typically on the client.

fix aptabase#7
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

Successfully merging a pull request may close this issue.

2 participants