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

chore: Rework SDK initialisation #1023

Merged
merged 28 commits into from
Mar 7, 2024
Merged

chore: Rework SDK initialisation #1023

merged 28 commits into from
Mar 7, 2024

Conversation

benjackwhite
Copy link
Collaborator

@benjackwhite benjackwhite commented Feb 14, 2024

Changes

TAKE 2! Had a PR a while ago for this but it got stalled by tests. Since then the test suite improved so now they work!

We get a lot of issues with people initialising PostHog incorrectly, sometimes due to think an env var is set when it isn't.
I started trying to add this and the spaghetti code of initialisation caused such a headache that I felt it was time we improved this.

Big change so we should validate this heavily but this:

  • Removes the multiple states that create a lib, instead using PostHog class as the basis and moving all initialisation into the two methods
  • Simplifies the global state side of things
  • Still keeps the snippet specific stuff but now it is relatively straightforward acting on the initialised PostHog instance rather than a hacked global override

TODO

  • Check the multiple paths
  • Check we have tests for these paths and they all pass
  • Ensure pre-loaded calls still work

Checklist

@posthog-bot
Copy link
Collaborator

Hey @benjackwhite! 👋
This pull request seems to contain no description. Please add useful context, rationale, and/or any other information that will help make sense of this change now and in the distant Mars-based future.

Copy link

github-actions bot commented Feb 14, 2024

Size Change: -3.85 kB (0%)

Total Size: 858 kB

Filename Size Change
dist/array.full.js 184 kB -1 kB (-1%)
dist/array.js 126 kB -991 B (-1%)
dist/es.js 126 kB -926 B (-1%)
dist/module.js 126 kB -926 B (-1%)
ℹ️ View Unchanged
Filename Size
dist/exception-autocapture.js 12 kB
dist/recorder-v2.js 106 kB
dist/recorder.js 58.6 kB
dist/surveys-module-previews.js 62 kB
dist/surveys.js 58.3 kB

compressed-size-action

@benjackwhite benjackwhite marked this pull request as ready for review February 14, 2024 09:40
Copy link
Collaborator

@mariusandra mariusandra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me!

I hope nobody was accidentally relying on the undocumented old initialization path. Seems low risk.

# Conflicts:
#	src/posthog-core.ts
@posthog-bot
Copy link
Collaborator

This PR hasn't seen activity in a week! Should it be merged, closed, or further worked on? If you want to keep it open, post a comment or remove the stale label – otherwise this will be closed in another week.

Copy link
Member

@pauldambra pauldambra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

runs locally, is smaller when bundled 👨‍🍳 👌

@posthog-bot
Copy link
Collaborator

This PR hasn't seen activity in a week! Should it be merged, closed, or further worked on? If you want to keep it open, post a comment or remove the stale label – otherwise this will be closed in another week.

# Conflicts:
#	src/__tests__/posthog-core.js
@benjackwhite benjackwhite added the bump patch Bump patch version when this PR gets merged label Mar 6, 2024
@benjackwhite benjackwhite merged commit f8c2b76 into main Mar 7, 2024
13 checks passed
@benjackwhite benjackwhite deleted the fix/init-alert branch March 7, 2024 08:16
@robbie-c
Copy link
Member

I was debugging https://posthoghelp.zendesk.com/agent/tickets/11815 and I believe this PR is the most likely root cause. The global variable posthog isn't fully initialized when the loaded callback is called. The fix was pretty small.

Might be worth delaying the loaded callback so this fix isn't necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bump patch Bump patch version when this PR gets merged waiting
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants