-
Notifications
You must be signed in to change notification settings - Fork 39
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: check if window is available before using on web env #256
base: main
Are you sure you want to change the base?
Conversation
Size Change: +281 B (+0.38%) Total Size: 73.8 kB
ℹ️ View Unchanged
|
if (!window) { | ||
return false | ||
} |
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.
checkStoreIsSupported
is only called if there's a window
@@ -50,6 +56,9 @@ export class PostHog extends PostHogCore { | |||
} | |||
|
|||
fetch(url: string, options: PostHogFetchOptions): Promise<PostHogFetchResponse> { | |||
// TODO: what to do here? | |||
// should we move this to core? https://github.com/PostHog/posthog-js-lite/blob/main/posthog-node/src/fetch.ts | |||
// and reuse it here? if window isn't available? |
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.
I don't really get what this means. How can we fetch if there is no 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.
In both Service Workers and Web Workers, there is no Window object, but the fetch() function is still available globally as part of the worker environment.
So I was assuming that https://github.com/PostHog/posthog-js-lite/blob/main/posthog-node/src/fetch.ts does something similar and we could reuse it here as a fallback if window
isn't available.
Does that make sense?
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.
Ah i see. Yeah then I guess that same line would I think work for loading fetch from the global instead of window?
Might be a bit tricky to test...
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.
yep, that looks like the fix to me...
Co-authored-by: Ben White <[email protected]>
Co-authored-by: Ben White <[email protected]>
const _window: Window | undefined = typeof window !== 'undefined' ? window : undefined | ||
return _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.
very nit-picky but _window
can be in-lined here
const _window: Window | undefined = typeof window !== 'undefined' ? window : undefined | |
return _window | |
return typeof window !== 'undefined' ? window : undefined |
@@ -50,6 +56,9 @@ export class PostHog extends PostHogCore { | |||
} | |||
|
|||
fetch(url: string, options: PostHogFetchOptions): Promise<PostHogFetchResponse> { | |||
// TODO: what to do here? | |||
// should we move this to core? https://github.com/PostHog/posthog-js-lite/blob/main/posthog-node/src/fetch.ts | |||
// and reuse it here? if window isn't available? |
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.
yep, that looks like the fix to me...
Problem
Closes #255
Similar to #57
Changes
Release info Sub-libraries affected
Bump level
Libraries affected
Changelog notes