From 11b2511aed1236e364aba60c72c17aefd3e69930 Mon Sep 17 00:00:00 2001 From: Jozef Harag Date: Mon, 9 Dec 2024 13:00:05 +0100 Subject: [PATCH] feat: add `persistence` flag (#904) Added persistence flag Available options: - `'cookie'` (default): Session state will be stored in a browser cookie. - `'localStorage'`: Session state will be stored in the browser's localStorage. If not specified, `'cookie'` will be used as the default storage method. --- packages/web/src/index.ts | 11 +++++++++-- packages/web/src/types/config.ts | 15 ++++++++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/packages/web/src/index.ts b/packages/web/src/index.ts index 25ff8aee..cb2de9c6 100644 --- a/packages/web/src/index.ts +++ b/packages/web/src/index.ts @@ -297,6 +297,13 @@ export const SplunkRum: SplunkOtelWebType = { }, ) + if (!['localStorage', 'cookie', undefined].includes(processedOptions.persistence)) { + diag.error( + 'Invalid persistence flag: The value for "persistence" must be either "cookie", "localStorage", or omitted entirely.', + ) + return + } + this._processedOptions = processedOptions if (processedOptions.realm) { @@ -359,7 +366,7 @@ export const SplunkRum: SplunkOtelWebType = { eventTarget, processedOptions.cookieDomain, !!options._experimental_allSpansExtendSession, - processedOptions.useLocalStorage, + processedOptions.persistence === 'localStorage', ).deinit const instrumentations = INSTRUMENTATIONS.map(({ Instrument, confKey, disable }) => { @@ -523,7 +530,7 @@ export const SplunkRum: SplunkOtelWebType = { updateSessionStatus({ forceStore: false, - useLocalStorage: this._processedOptions.useLocalStorage ?? false, + useLocalStorage: this._processedOptions.persistence === 'localStorage', forceActivity: this._processedOptions._experimental_allSpansExtendSession, }) }, diff --git a/packages/web/src/types/config.ts b/packages/web/src/types/config.ts index d855be89..21215bb7 100644 --- a/packages/web/src/types/config.ts +++ b/packages/web/src/types/config.ts @@ -127,6 +127,18 @@ export interface SplunkOtelWebConfig { /** Configuration for instrumentation modules. */ instrumentations?: SplunkOtelWebOptionsInstrumentations + /** + * Specifies where session data should be stored. + * + * Available options: + * - `'cookie'` (default): Session data will be stored in a browser cookie. + * + * - `'localStorage'`: Session data will be stored in the browser's localStorage. + * + * If not specified, `'cookie'` will be used as the default storage method. + */ + persistence?: 'cookie' | 'localStorage' + /** * The name of your organization’s realm. Automatically configures beaconUrl with correct URL */ @@ -150,9 +162,6 @@ export interface SplunkOtelWebConfig { */ tracer?: WebTracerConfig - /** Use local storage to save session ID instead of cookie */ - useLocalStorage?: boolean - /** * Sets a value for the 'app.version' attribute */