-
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?
Changes from all commits
ce9e181
7fb87e8
a33b84f
ea62d80
cd30a0c
ef4509d
e57547b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,11 @@ import { PostHogStorage, getStorage } from './storage' | |
import { version } from '../package.json' | ||
import { PostHogOptions } from './types' | ||
|
||
export function _getWindow(): Window | undefined { | ||
const _window: Window | undefined = typeof window !== 'undefined' ? window : undefined | ||
return _window | ||
} | ||
|
||
export class PostHog extends PostHogCore { | ||
private _storage: PostHogStorage | ||
private _storageCache: any | ||
|
@@ -19,7 +24,8 @@ 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', window) | ||
|
||
this._storage = getStorage(options?.persistence || 'localStorage', _getWindow()) | ||
this.setupBootstrap(options) | ||
|
||
if (options?.preloadFeatureFlags !== false) { | ||
|
@@ -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 commentThe 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 commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. yep, that looks like the fix to me... |
||
return window.fetch(url, options) | ||
} | ||
|
||
|
@@ -68,7 +77,7 @@ export class PostHog extends PostHogCore { | |
getCommonEventProperties(): any { | ||
return { | ||
...super.getCommonEventProperties(), | ||
...getContext(window), | ||
...getContext(_getWindow()), | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,9 +93,6 @@ const createStorageLike = (store: any): PostHogStorage => { | |
} | ||
|
||
const checkStoreIsSupported = (storage: PostHogStorage, key = '__mplssupport__'): boolean => { | ||
if (!window) { | ||
return false | ||
} | ||
Comment on lines
-96
to
-98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
try { | ||
const val = 'xyz' | ||
storage.setItem(key, val) | ||
|
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