Skip to content

Commit dda3e6f

Browse files
author
Prashant.patil
committed
test 1: check mixapnel sanity on SSR
1 parent 5cd09df commit dda3e6f

File tree

2 files changed

+69
-23
lines changed

2 files changed

+69
-23
lines changed

src/embed/base.ts

+40-21
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { uploadMixpanelEvent, MIXPANEL_EVENT } from '../mixpanel-service';
3535
import { getEmbedConfig, setEmbedConfig } from './embedConfig';
3636
import { getQueryParamString, getValueFromWindow, storeValueInWindow } from '../utils';
3737
import { resetAllCachedServices } from '../utils/resetServices';
38+
import { isBrowser } from '../utils';
3839

3940
const CONFIG_DEFAULTS: Partial<EmbedConfig> = {
4041
loginFailedMessage: 'Not logged in',
@@ -238,33 +239,51 @@ export const init = (embedConfig: EmbedConfig): AuthEventEmitter => {
238239
);
239240

240241
setGlobalLogLevelOverride(embedConfig.logLevel);
241-
registerReportingObserver();
242+
243+
// Only register browser-specific observers when in browser
244+
if (isBrowser()) {
245+
registerReportingObserver();
246+
}
242247

243248
const authEE = new EventEmitter<AuthStatus | AuthEvent>();
244249
setAuthEE(authEE);
245-
handleAuth();
246-
247-
console.log('init mixpanel done', getEmbedConfig());
248-
alert("yaha tak to aa gya bhai bina error ke");
249-
250-
const { password, ...configToTrack } = getEmbedConfig();
251-
uploadMixpanelEvent(MIXPANEL_EVENT.VISUAL_SDK_CALLED_INIT, {
252-
...configToTrack,
253-
usedCustomizationSheet: embedConfig.customizations?.style?.customCSSUrl != null,
254-
usedCustomizationVariables: embedConfig.customizations?.style?.customCSS?.variables != null,
255-
usedCustomizationRules:
256-
embedConfig.customizations?.style?.customCSS?.rules_UNSTABLE != null,
257-
usedCustomizationStrings: !!embedConfig.customizations?.content?.strings,
258-
usedCustomizationIconSprite: !!embedConfig.customizations?.iconSpriteUrl,
259-
});
250+
251+
// For SSR, we handle auth differently based on auth type
252+
if (embedConfig.authType === AuthType.TrustedAuthTokenCookieless) {
253+
// Cookieless auth can work in SSR
254+
handleAuth();
255+
} else if (isBrowser()) {
256+
// Other auth types need browser capabilities
257+
handleAuth();
258+
}
260259

261-
if (getEmbedConfig().callPrefetch) {
262-
prefetch(getEmbedConfig().thoughtSpotHost);
260+
// Skip browser-only operations when in SSR
261+
if (isBrowser()) {
262+
console.log('init mixpanel done', getEmbedConfig());
263+
alert("yaha tak to aa gya bhai bina error ke");
264+
265+
const { password, ...configToTrack } = getEmbedConfig();
266+
uploadMixpanelEvent(MIXPANEL_EVENT.VISUAL_SDK_CALLED_INIT, {
267+
...configToTrack,
268+
usedCustomizationSheet: embedConfig.customizations?.style?.customCSSUrl != null,
269+
usedCustomizationVariables: embedConfig.customizations?.style?.customCSS?.variables != null,
270+
usedCustomizationRules:
271+
embedConfig.customizations?.style?.customCSS?.rules_UNSTABLE != null,
272+
usedCustomizationStrings: !!embedConfig.customizations?.content?.strings,
273+
usedCustomizationIconSprite: !!embedConfig.customizations?.iconSpriteUrl,
274+
});
275+
276+
if (getEmbedConfig().callPrefetch) {
277+
prefetch(getEmbedConfig().thoughtSpotHost);
278+
}
263279
}
264280

265-
// Resolves the promise created in the initPromiseKey
266-
getValueFromWindow<InitFlagStore>(initFlagKey).initPromiseResolve(authEE);
267-
getValueFromWindow<InitFlagStore>(initFlagKey).isInitCalled = true;
281+
// Store initialization flag in appropriate storage (server or browser)
282+
const initFlagStore = getValueFromWindow<InitFlagStore>(initFlagKey);
283+
if (initFlagStore) {
284+
initFlagStore.initPromiseResolve(authEE);
285+
initFlagStore.isInitCalled = true;
286+
}
268287

269288
return authEE as AuthEventEmitter;
270289
};

src/utils.ts

+29-2
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,9 @@ export const getTypeFromValue = (value: any): [string, string] => {
337337
};
338338

339339
const sdkWindowKey = '_tsEmbedSDK' as any;
340+
let serverStorage: Record<string, any> = {};
341+
342+
export const isBrowser = () : boolean => typeof window !== 'undefined';
340343

341344
/**
342345
* Stores a value in the global `window` object under the `_tsEmbedSDK` namespace.
@@ -354,6 +357,15 @@ export function storeValueInWindow<T>(
354357
value: T,
355358
options: { ignoreIfAlreadyExists?: boolean } = {},
356359
): T {
360+
361+
if (!isBrowser()) {
362+
if (options.ignoreIfAlreadyExists && key in serverStorage) {
363+
return serverStorage[key];
364+
}
365+
serverStorage[key] = value;
366+
return value;
367+
}
368+
357369
if (!window[sdkWindowKey]) {
358370
(window as any)[sdkWindowKey] = {};
359371
}
@@ -371,8 +383,12 @@ export function storeValueInWindow<T>(
371383
* @param key - The key whose value needs to be retrieved.
372384
* @returns The stored value or `undefined` if the key is not found.
373385
*/
374-
export const getValueFromWindow = <T = any>
375-
(key: string): T => (window as any)?.[sdkWindowKey]?.[key];
386+
export const getValueFromWindow = <T = any>(key: string): T => {
387+
if (!isBrowser()) {
388+
return serverStorage[key];
389+
}
390+
return (window as any)?.[sdkWindowKey]?.[key];
391+
};
376392

377393
/**
378394
* Resets the key if it exists in the `window` object under the `_tsEmbedSDK` key.
@@ -381,9 +397,20 @@ export const getValueFromWindow = <T = any>
381397
* @returns - boolean indicating if the key was reset
382398
*/
383399
export function resetValueFromWindow(key: string): boolean {
400+
if (!isBrowser()) {
401+
if (key in serverStorage) {
402+
delete serverStorage[key];
403+
return true;
404+
}
405+
return false;
406+
}
384407
if (key in window[sdkWindowKey]) {
385408
delete (window as any)[sdkWindowKey][key];
386409
return true;
387410
}
388411
return false;
389412
}
413+
414+
export const clearServerStorage = () => {
415+
serverStorage = {};
416+
};

0 commit comments

Comments
 (0)