Skip to content
This repository was archived by the owner on Jul 29, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 4 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 && 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 || !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 @@ -19,14 +19,20 @@ 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', this._getWindow())
this.setupBootstrap(options)

if (options?.preloadFeatureFlags !== false) {
this.reloadFeatureFlags()
}
}

_getWindow(): Window | undefined {
const _window: Window | undefined = typeof window !== 'undefined' ? window : undefined
return _window
}

getPersistedProperty<T>(key: PostHogPersistedProperty): T | undefined {
if (!this._storageCache) {
this._storageCache = JSON.parse(this._storage.getItem(this._storageKey) || '{}') || {}
Expand All @@ -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?
return window.fetch(url, options)
}

Expand All @@ -68,7 +77,7 @@ export class PostHog extends PostHogCore {
getCommonEventProperties(): any {
return {
...super.getCommonEventProperties(),
...getContext(window),
...getContext(this._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