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

Conversation

marandaneto
Copy link
Member

Problem

Closes #255
Similar to #57

Changes

Release info Sub-libraries affected

Bump level

  • Major
  • Minor
  • Patch

Libraries affected

  • All of them
  • posthog-web
  • posthog-node
  • posthog-react-native

Changelog notes

  • Added support for X

Copy link

github-actions bot commented Sep 10, 2024

Size Change: +281 B (+0.38%)

Total Size: 73.8 kB

Filename Size Change
posthog-web/lib/index.cjs.js 16.9 kB +143 B (+0.85%)
posthog-web/lib/index.esm.js 16.9 kB +138 B (+0.82%)
ℹ️ View Unchanged
Filename Size
posthog-node/lib/index.cjs.js 20 kB
posthog-node/lib/index.esm.js 20 kB

compressed-size-action

Comment on lines -96 to -98
if (!window) {
return false
}
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

@marandaneto marandaneto requested review from benjackwhite and a team September 10, 2024 08:46
posthog-web/src/context.ts Outdated Show resolved Hide resolved
posthog-web/src/context.ts Outdated Show resolved Hide resolved
posthog-web/src/posthog-web.ts Outdated Show resolved Hide resolved
@@ -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...

marandaneto and others added 3 commits September 10, 2024 13:39
Co-authored-by: Ben White <ben@posthog.com>
Co-authored-by: Ben White <ben@posthog.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Does not work in Next.js serverless (API route)
2 participants