@leanbase-giangnd/js 0.2.2 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/leanbase.ts CHANGED
@@ -93,6 +93,10 @@ export class Leanbase extends PostHogCore {
93
93
  consent!: ConsentManager
94
94
  sessionRecording?: SessionRecording
95
95
  isRemoteConfigLoaded?: boolean
96
+ private _remoteConfigLoadAttempted: boolean = false
97
+ private _remoteConfigResolved: boolean = false
98
+ private _featureFlagsResolved: boolean = false
99
+ private _maybeStartedSessionRecording: boolean = false
96
100
  personProcessingSetOncePropertiesSent = false
97
101
  isLoaded: boolean = false
98
102
  initialPageviewCaptured: boolean
@@ -130,11 +134,21 @@ export class Leanbase extends PostHogCore {
130
134
 
131
135
  if (this.sessionManager && this.config.cookieless_mode !== 'always') {
132
136
  this.sessionRecording = new SessionRecording(this)
133
- this.sessionRecording.startIfEnabledOrStop()
134
137
  }
135
138
 
139
+ // Start session recording only once flags + remote config have been resolved.
140
+ // This matches the PostHog browser SDK where replay activation is driven by remote config and flags.
136
141
  if (this.config.preloadFeatureFlags !== false) {
137
- this.reloadFeatureFlags()
142
+ this.reloadFeatureFlags({
143
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
144
+ cb: (_err) => {
145
+ this._featureFlagsResolved = true
146
+ this._maybeStartSessionRecording()
147
+ },
148
+ })
149
+ } else {
150
+ // If feature flags preload is explicitly disabled, treat this requirement as satisfied.
151
+ this._featureFlagsResolved = true
138
152
  }
139
153
 
140
154
  this.config.loaded?.(this)
@@ -146,9 +160,25 @@ export class Leanbase extends PostHogCore {
146
160
  }, 1)
147
161
  }
148
162
 
149
- addEventListener(document, 'DOMContentLoaded', () => {
150
- this.loadRemoteConfig()
151
- })
163
+ const triggerRemoteConfigLoad = (reason: 'immediate' | 'dom' | 'no-document') => {
164
+ logger.info(`remote config load triggered via ${reason}`)
165
+ void this.loadRemoteConfig()
166
+ }
167
+
168
+ if (document) {
169
+ if (document.readyState === 'loading') {
170
+ logger.info('remote config load deferred until DOMContentLoaded')
171
+ const onDomReady = () => {
172
+ document?.removeEventListener('DOMContentLoaded', onDomReady)
173
+ triggerRemoteConfigLoad('dom')
174
+ }
175
+ addEventListener(document, 'DOMContentLoaded', onDomReady, { once: true } as any)
176
+ } else {
177
+ triggerRemoteConfigLoad('immediate')
178
+ }
179
+ } else {
180
+ triggerRemoteConfigLoad('no-document')
181
+ }
152
182
  addEventListener(window, 'onpagehide' in self ? 'pagehide' : 'unload', this.capturePageLeave.bind(this), {
153
183
  passive: false,
154
184
  })
@@ -191,11 +221,20 @@ export class Leanbase extends PostHogCore {
191
221
  }
192
222
 
193
223
  async loadRemoteConfig() {
194
- if (!this.isRemoteConfigLoaded) {
224
+ if (this._remoteConfigLoadAttempted) {
225
+ return
226
+ }
227
+ this._remoteConfigLoadAttempted = true
228
+
229
+ try {
195
230
  const remoteConfig = await this.reloadRemoteConfigAsync()
196
231
  if (remoteConfig) {
197
232
  this.onRemoteConfig(remoteConfig as RemoteConfig)
198
233
  }
234
+ } finally {
235
+ // Regardless of success/failure, we consider remote config "resolved" so replay isn't blocked forever.
236
+ this._remoteConfigResolved = true
237
+ this._maybeStartSessionRecording()
199
238
  }
200
239
  }
201
240
 
@@ -210,6 +249,29 @@ export class Leanbase extends PostHogCore {
210
249
  this.isRemoteConfigLoaded = true
211
250
  this.replayAutocapture?.onRemoteConfig(config)
212
251
  this.sessionRecording?.onRemoteConfig(config)
252
+
253
+ // Remote config has been applied; allow replay start if flags are also ready.
254
+ this._remoteConfigResolved = true
255
+ this._maybeStartSessionRecording()
256
+ }
257
+
258
+ private _maybeStartSessionRecording(): void {
259
+ if (this._maybeStartedSessionRecording) {
260
+ return
261
+ }
262
+ if (!this.sessionRecording) {
263
+ return
264
+ }
265
+ if (!this._featureFlagsResolved || !this._remoteConfigResolved) {
266
+ return
267
+ }
268
+
269
+ this._maybeStartedSessionRecording = true
270
+ try {
271
+ this.sessionRecording.startIfEnabledOrStop()
272
+ } catch (e) {
273
+ logger.error('Failed to start session recording', e)
274
+ }
213
275
  }
214
276
 
215
277
  fetch(url: string, options: PostHogFetchOptions): Promise<PostHogFetchResponse> {
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const version = '0.2.2'
1
+ export const version = '0.2.4'
package/LICENSE DELETED
@@ -1,37 +0,0 @@
1
- Copyright 2025 Leanflag Limited
2
- This project is a fork of PostHog/posthog-js.
3
-
4
- All rights reserved. This software is proprietary to Leanflag Limited and may only be used for Leanflag Limited products and internal purposes. Redistribution, modification, or commercial use outside of Leanflag Limited is strictly prohibited without explicit written permission from Leanflag Limited.
5
-
6
-
7
- ---
8
-
9
- Copyright 2020 Posthog / Hiberly, Inc.
10
-
11
- Copyright 2015 Mixpanel, Inc.
12
-
13
-
14
- Some files in this codebase contain code from getsentry/sentry-javascript by Software, Inc. dba Sentry.
15
- In such cases it is explicitly stated in the file header. This license only applies to the relevant code in such cases.
16
-
17
- MIT License
18
-
19
- Copyright (c) 2012 Functional Software, Inc. dba Sentry
20
-
21
- Permission is hereby granted, free of charge, to any person obtaining a copy of
22
- this software and associated documentation files (the "Software"), to deal in
23
- the Software without restriction, including without limitation the rights to
24
- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
25
- of the Software, and to permit persons to whom the Software is furnished to do
26
- so, subject to the following conditions:
27
-
28
- The above copyright notice and this permission notice shall be included in all
29
- copies or substantial portions of the Software.
30
-
31
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37
- SOFTWARE.