Skip to content

Commit adcfed2

Browse files
committed
fix: Eager initialize constructor classes
These were previously available _before_ `init()` had been called. Though edge-case-y, this ensures full backwards compatibility.
1 parent b09724d commit adcfed2

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

packages/browser/src/__tests__/extension-classes.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,32 @@ describe('__extensionClasses enrollment', () => {
8181
expect(posthog.autocapture).toBeInstanceOf(MockAutocapture)
8282
})
8383

84+
it('eagerly constructs extensions from defaults before init()', () => {
85+
PostHog.__defaultExtensionClasses = AllExtensions
86+
87+
const posthog = new PostHog()
88+
89+
expect(posthog.toolbar).toBeDefined()
90+
expect(posthog.surveys).toBeDefined()
91+
expect(posthog.conversations).toBeDefined()
92+
expect(posthog.logs).toBeDefined()
93+
expect(posthog.experiments).toBeDefined()
94+
expect(posthog.exceptions).toBeDefined()
95+
})
96+
97+
it('does not eagerly construct extensions when no defaults exist', () => {
98+
PostHog.__defaultExtensionClasses = {}
99+
100+
const posthog = new PostHog()
101+
102+
expect(posthog.toolbar).toBeUndefined()
103+
expect(posthog.surveys).toBeUndefined()
104+
expect(posthog.conversations).toBeUndefined()
105+
expect(posthog.logs).toBeUndefined()
106+
expect(posthog.experiments).toBeUndefined()
107+
expect(posthog.exceptions).toBeUndefined()
108+
})
109+
84110
it('default extensions are used when __extensionClasses is not provided', async () => {
85111
PostHog.__defaultExtensionClasses = AllExtensions
86112

packages/browser/src/posthog-core.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,17 @@ export class PostHog implements PostHogInterface {
436436
this.requestRouter = new RequestRouter(this)
437437
this.consent = new ConsentManager(this)
438438
this.externalIntegrations = new ExternalIntegrations(this)
439+
440+
// Eagerly construct extensions from default classes so they're available before init().
441+
// For the slim bundle, these remain undefined until _initExtensions sets them from config.
442+
const ext = PostHog.__defaultExtensionClasses ?? {}
443+
this.toolbar = ext.toolbar && new ext.toolbar(this)
444+
this.surveys = ext.surveys && new ext.surveys(this)
445+
this.conversations = ext.conversations && new ext.conversations(this)
446+
this.logs = ext.logs && new ext.logs(this)
447+
this.experiments = ext.experiments && new ext.experiments(this)
448+
this.exceptions = ext.exceptions && new ext.exceptions(this)
449+
439450
// NOTE: See the property definition for deprecation notice
440451
this.people = {
441452
set: (prop: string | Properties, to?: string, callback?: RequestCallback) => {
@@ -713,7 +724,7 @@ export class PostHog implements PostHogInterface {
713724
// Due to name mangling, we can't easily iterate and assign these extensions
714725
// The assignment needs to also be mangled. Thus, the loop is unrolled.
715726
if (ext.exceptions) {
716-
this._extensions.push((this.exceptions = new ext.exceptions(this)))
727+
this._extensions.push((this.exceptions = this.exceptions ?? new ext.exceptions(this)))
717728
}
718729
if (ext.historyAutocapture) {
719730
this._extensions.push((this.historyAutocapture = new ext.historyAutocapture(this)))
@@ -736,13 +747,13 @@ export class PostHog implements PostHogInterface {
736747
this._extensions.push((this.autocapture = new ext.autocapture(this)))
737748
}
738749
if (ext.surveys) {
739-
this._extensions.push((this.surveys = new ext.surveys(this)))
750+
this._extensions.push((this.surveys = this.surveys ?? new ext.surveys(this)))
740751
}
741752
if (ext.logs) {
742-
this._extensions.push((this.logs = new ext.logs(this)))
753+
this._extensions.push((this.logs = this.logs ?? new ext.logs(this)))
743754
}
744755
if (ext.conversations) {
745-
this._extensions.push((this.conversations = new ext.conversations(this)))
756+
this._extensions.push((this.conversations = this.conversations ?? new ext.conversations(this)))
746757
}
747758
if (ext.productTours) {
748759
this._extensions.push((this.productTours = new ext.productTours(this)))
@@ -762,10 +773,10 @@ export class PostHog implements PostHogInterface {
762773
)
763774
}
764775
if (ext.toolbar) {
765-
this._extensions.push((this.toolbar = new ext.toolbar(this)))
776+
this._extensions.push((this.toolbar = this.toolbar ?? new ext.toolbar(this)))
766777
}
767778
if (ext.experiments) {
768-
this._extensions.push((this.experiments = new ext.experiments(this)))
779+
this._extensions.push((this.experiments = this.experiments ?? new ext.experiments(this)))
769780
}
770781

771782
this._extensions.forEach((extension) => {

0 commit comments

Comments
 (0)