diff --git a/.changeset/nervous-shrimps-behave.md b/.changeset/nervous-shrimps-behave.md new file mode 100644 index 00000000..5c6e115a --- /dev/null +++ b/.changeset/nervous-shrimps-behave.md @@ -0,0 +1,5 @@ +--- +'@frigade/js': patch +--- + +Skip all Frigade API requests when `generateGuestId` is set to `false` and no `userId` has been provided. Previously the SDK would still send a `flowStates` request with an auto-generated guest ID on initialization, page navigation, and tab focus. diff --git a/packages/js-api/src/core/frigade.ts b/packages/js-api/src/core/frigade.ts index 035399c8..c2bb1cb6 100644 --- a/packages/js-api/src/core/frigade.ts +++ b/packages/js-api/src/core/frigade.ts @@ -562,7 +562,7 @@ export class Frigade extends Fetchable { frigadeGlobalState[globalStateKey].refreshStateFromAPI = async ( overrideFlowStateRaw?: FlowStates ) => { - if (this.config.__readOnly) { + if (this.config.__readOnly || this.isAnonymousWithGuestIdDisabled()) { return } diff --git a/packages/js-api/src/shared/fetchable.ts b/packages/js-api/src/shared/fetchable.ts index 8d79f2ba..0b1c42d5 100644 --- a/packages/js-api/src/shared/fetchable.ts +++ b/packages/js-api/src/shared/fetchable.ts @@ -1,4 +1,10 @@ -import { generateGuestId, getEmptyResponse, getHeaders, gracefulFetch } from './utils' +import { + generateGuestId, + getEmptyResponse, + getHeaders, + gracefulFetch, + GUEST_PREFIX, +} from './utils' import { DEFAULT_REFRESH_INTERVAL_IN_MS, FrigadeConfig } from '../core/types' import { frigadeGlobalState, FrigadeGlobalState, getGlobalStateKey } from './state' @@ -24,7 +30,7 @@ export class Fetchable { * @ignore */ public async fetch(path: string, options?: Record) { - if (this.config.__readOnly) { + if (this.config.__readOnly || this.isAnonymousWithGuestIdDisabled()) { return getEmptyResponse() } @@ -39,6 +45,18 @@ export class Fetchable { return `${this.config.apiUrl.replace(/\/$/, '')}/${path.replace(/^\//, '')}` } + /** + * @ignore + * True when `generateGuestId` is explicitly disabled and no real userId has been provided, + * in which case no requests should be sent to the Frigade API. + */ + protected isAnonymousWithGuestIdDisabled(): boolean { + return ( + this.config.generateGuestId === false && + (!this.config.userId || this.config.userId.startsWith(GUEST_PREFIX)) + ) + } + /** * @ignore */