-
Notifications
You must be signed in to change notification settings - Fork 33
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
fix: support for SSR #57
Conversation
In addition, we also get rid of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great solution, nicely implemented!
Thanks @paolodamico !
thx @benjackwhite, appreciate the super quick turnaround! could we trouble you with pushing a release so we can ship our app? |
@@ -19,7 +19,7 @@ export class PostHog extends PostHogCore { | |||
|
|||
// posthog-js stores options in one object on | |||
this._storageKey = options?.persistence_name ? `ph_${options.persistence_name}` : `ph_${apiKey}_posthog` | |||
this._storage = getStorage(options?.persistence || 'localStorage') | |||
this._storage = getStorage(options?.persistence || 'localStorage', window) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This still needs to be gated because it is still throwing a ReferenceError – you can't just reference window
in the node.js global scope.
this._storage = getStorage(options?.persistence || 'localStorage', typeof window !== 'undefined' ? window : undefined)
This PR refactors the code
posthog-web
so the library can be imported in Next.js projects with SSR. Instead of window having to be defined just from importing the library, this relies onwindow
being defined on the constructor (i.e. when an object is initialized).Before in a Next.JS project
After (this works)
CC @m1guelpf