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

fix: check if window is available before using on web env #256

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions posthog-web/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { utils } from '../../posthog-core'
import { version } from '../package.json'

export function getContext(window: Window): any {
export function getContext(window: Window | undefined): any {
let context = {}
if (window.navigator) {
if (window?.navigator) {
const userAgent = window.navigator.userAgent
context = {
...context,
Expand All @@ -21,6 +21,7 @@ export function getContext(window: Window): any {
$screen_dpr: window.devicePixelRatio,
}
}

context = {
...context,
$lib: 'js',
Expand Down Expand Up @@ -114,7 +115,10 @@ function browserVersion(userAgent: string, vendor: string, opera: boolean): numb
return parseFloat(matches[matches.length - 2])
}

function os(window: Window): string {
function os(window: Window | undefined): string {
if (!window?.navigator) {
return ''
}
const a = window.navigator.userAgent
if (/Windows/i.test(a)) {
if (/Phone/.test(a) || /WPDesktop/.test(a)) {
Expand Down
13 changes: 11 additions & 2 deletions posthog-web/src/posthog-web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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?
Copy link
Collaborator

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?

Copy link
Member Author

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?

Copy link
Collaborator

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...

return window.fetch(url, options)
}

Expand All @@ -68,7 +77,7 @@ export class PostHog extends PostHogCore {
getCommonEventProperties(): any {
return {
...super.getCommonEventProperties(),
...getContext(window),
...getContext(_getWindow()),
}
}
}
3 changes: 0 additions & 3 deletions posthog-web/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member Author

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

try {
const val = 'xyz'
storage.setItem(key, val)
Expand Down