SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.
TypeScript apmapplication-monitoringdistributed-tracinggogood-first-issuejaegerloglogsmetricsmonitoringnextjsobservabilityopen-sourceopentelemetryprometheusreactreactjsself-hostedtracingtypescript
feat(base-path): scope localStorage/sessionStorage keys to base path + fix livetail url (#11029)
SagarRajput-785a38d560868
diff --git a/frontend/.oxlintrc.json b/frontend/.oxlintrc.jsonindex a93ebed989..5d0b78b4f2 100644--- a/frontend/.oxlintrc.json+++ b/frontend/.oxlintrc.json@@ -288,6 +288,18 @@// Prevents navigator.clipboard - use useCopyToClipboard hook instead (disabled in tests via override)"signoz/no-raw-absolute-path": "error",// Prevents window.open(path), window.location.origin + path, window.location.href = path+ "no-restricted-globals": [+ "error",+ {+ "name": "localStorage",+ "message": "Use scoped wrappers from api/browser/localstorage/ instead (ensures keys are prefixed when served under a URL base path)."+ },+ {+ "name": "sessionStorage",+ "message": "Use scoped wrappers from api/browser/sessionstorage/ instead (ensures keys are prefixed when served under a URL base path)."+ }+ ],+ // Prevents direct localStorage/sessionStorage access — use scoped wrappers"no-restricted-imports": ["error",{@@ -601,7 +613,9 @@// Should ignore due to mocks"signoz/no-navigator-clipboard": "off",// Tests can use navigator.clipboard directly,- "signoz/no-raw-absolute-path":"off"+ "signoz/no-raw-absolute-path":"off",+ "no-restricted-globals": "off"+ // Tests need raw localStorage/sessionStorage to seed DOM state for isolation}},{diff --git a/frontend/index.html b/frontend/index.htmlindex da790fc02e..8d6059c0d6 100644--- a/frontend/index.html+++ b/frontend/index.html@@ -68,8 +68,14 @@// Mirrors the logic in ThemeProvider (hooks/useDarkMode/index.tsx).(function () {try {- var theme = localStorage.getItem('THEME');- var autoSwitch = localStorage.getItem('THEME_AUTO_SWITCH') === 'true';+ // When served under a URL prefix (e.g. /signoz/), storage keys are scoped+ // to that prefix by the React app (see utils/storage.ts getScopedKey).+ // Read the <base> tag — already populated by the Go template — to derive+ // the same prefix here, before any JS module has loaded.+ var basePath = (document.querySelector('base') || {}).getAttribute('href') || '/';+ var prefix = basePath === '/' ? '' : basePath;+ var theme = localStorage.getItem(prefix + 'THEME');+ var autoSwitch = localStorage.getItem(prefix + 'THEME_AUTO_SWITCH') === 'true';if (autoSwitch) {theme = window.matchMedia('(prefers-color-scheme: dark)').matches? 'dark'diff --git a/frontend/src/api/browser/localstorage/__tests__/get.test.ts b/frontend/src/api/browser/localstorage/__tests__/get.test.tsnew file mode 100644index 0000000000..cddff010f1--- /dev/null+++ b/frontend/src/api/browser/localstorage/__tests__/get.test.ts@@ -0,0 +1,130 @@+/**+ * localstorage/get — lazy migration tests.+ *+ * basePath is memoized at module init, so each describe block re-imports the+ * module with a fresh DOM state via jest.isolateModules.+ */++type GetModule = typeof import('../get');++function loadGetModule(href: string): GetModule {+ const base = document.createElement('base');+ base.setAttribute('href', href);+ document.head.append(base);++ let mod!: GetModule;+ jest.isolateModules(() => {+ // oxlint-disable-next-line typescript-eslint/no-require-imports, typescript-eslint/no-var-requires+ mod = require('../get');+ });+ return mod;+}++afterEach(() => {+ for (const el of document.head.querySelectorAll('base')) {+ el.remove();+ }+ localStorage.clear();+});++describe('get — root path "/"', () => {+ it('reads the bare key', () => {+ const { default: get } = loadGetModule('/');+ localStorage.setItem('AUTH_TOKEN', 'tok');+ expect(get('AUTH_TOKEN')).toBe('tok');+ });++ it('returns null when key is absent', () => {+ const { default: get } = loadGetModule('/');+ expect(get('MISSING')).toBeNull();+ });++ it('does NOT promote bare keys (no-op at root)', () => {+ const { default: get } = loadGetModule('/');+ localStorage.setItem('THEME', 'light');+ get('THEME');+ // bare key must still be present — no migration at root+ expect(localStorage.getItem('THEME')).toBe('light');+ });+});++describe('get — prefixed path "/signoz/"', () => {+ it('reads an already-scoped key directly', () => {+ const { default: get } = loadGetModule('/signoz/');+ localStorage.setItem('/signoz/AUTH_TOKEN', 'scoped-tok');+ expect(get('AUTH_TOKEN')).toBe('scoped-tok');+ });++ it('returns null when neither scoped nor bare key exists', () => {+ const { default: get } = loadGetModule('/signoz/');+ expect(get('MISSING')).toBeNull();+ });++ it('lazy-migrates bare key to scoped key on first read', () => {+ const { default: get } = loadGetModule('/signoz/');+ localStorage.setItem('AUTH_TOKEN', 'old-tok');++ const result = get('AUTH_TOKEN');++ expect(result).toBe('old-tok');+ expect(localStorage.getItem('/signoz/AUTH_TOKEN')).toBe('old-tok');+ expect(localStorage.getItem('AUTH_TOKEN')).toBeNull();+ });++ it('scoped key takes precedence over bare key', () => {+ const { default: get } = loadGetModule('/signoz/');+ localStorage.setItem('AUTH_TOKEN', 'bare-tok');+ localStorage.setItem('/signoz/AUTH_TOKEN', 'scoped-tok');++ expect(get('AUTH_TOKEN')).toBe('scoped-tok');+ // bare key left untouched — scoped already existed+ expect(localStorage.getItem('AUTH_TOKEN')).toBe('bare-tok');+ });++ it('subsequent reads after migration use scoped key (no double-write)', () => {+ const { default: get } = loadGetModule('/signoz/');+ localStorage.setItem('THEME', 'dark');++ get('THEME'); // triggers migration+ localStorage.removeItem('THEME'); // simulate bare key gone++ // second read still finds the scoped key+ expect(get('THEME')).toBe('dark');+ });+});++describe('get — two-prefix isolation', () => {+ it('/signoz/ and /testing/ do not share migrated values', () => {+ localStorage.setItem('THEME', 'light');++ const base1 = document.createElement('base');+ base1.setAttribute('href', '/signoz/');+ document.head.append(base1);+ let getSignoz!: GetModule['default'];+ jest.isolateModules(() => {+ // oxlint-disable-next-line typescript-eslint/no-require-imports, typescript-eslint/no-var-requires+ getSignoz = require('../get').default;+ });+ base1.remove();++ // migrate bare → /signoz/THEME+ getSignoz('THEME');++ const base2 = document.createElement('base');+ base2.setAttribute('href', '/testing/');+ document.head.append(base2);+ let getTesting!: GetModule['default'];+ jest.isolateModules(() => {+ // oxlint-disable-next-line typescript-eslint/no-require-imports, typescript-eslint/no-var-requires+ getTesting = require('../get').default;+ });+ base2.remove();++ // /testing/ prefix: bare key already gone, scoped key does not exist+ expect(getTesting('THEME')).toBeNull();+ expect(localStorage.getItem('/signoz/THEME')).toBe('light');+ expect(localStorage.getItem('/testing/THEME')).toBeNull();+ });+});++export {};diff --git a/frontend/src/api/browser/localstorage/get.ts b/frontend/src/api/browser/localstorage/get.tsindex 026c7a7171..f017fbc5ea 100644--- a/frontend/src/api/browser/localstorage/get.ts+++ b/frontend/src/api/browser/localstorage/get.ts@@ -1,7 +1,26 @@+/* oxlint-disable no-restricted-globals */+import { getBasePath } from 'utils/basePath';+import { getScopedKey } from 'utils/storage';+const get = (key: string): string | null => {try {- return localStorage.getItem(key);- } catch (e) {+ const scopedKey = getScopedKey(key);+ const value = localStorage.getItem(scopedKey);++ // Lazy migration: if running under a URL prefix and the scoped key doesn't+ // exist yet, fall back to the bare key (written by a previous root deployment).+ // Promote it to the scoped key and remove the bare key so future reads are fast.+ if (value === null && getBasePath() !== '/') {+ const bare = localStorage.getItem(key);+ if (bare !== null) {+ localStorage.setItem(scopedKey, bare);+ localStorage.removeItem(key);+ return bare;+ }+ }++ return value;+ } catch {return '';}};diff --git a/frontend/src/api/browser/localstorage/remove.ts b/frontend/src/api/browser/localstorage/remove.tsindex b1c5c40265..e59b394fc9 100644--- a/frontend/src/api/browser/localstorage/remove.ts+++ b/frontend/src/api/browser/localstorage/remove.ts@@ -1,8 +1,11 @@+/* oxlint-disable no-restricted-globals */+import { getScopedKey } from 'utils/storage';+const remove = (key: string): boolean => {try {- window.localStorage.removeItem(key);+ localStorage.removeItem(getScopedKey(key));return true;- } catch (e) {+ } catch {return false;}};diff --git a/frontend/src/api/browser/localstorage/set.ts b/frontend/src/api/browser/localstorage/set.tsindex 1ce9f313b6..e50a3b3aac 100644--- a/frontend/src/api/browser/localstorage/set.ts+++ b/frontend/src/api/browser/localstorage/set.ts@@ -1,8 +1,11 @@+/* oxlint-disable no-restricted-globals */+import { getScopedKey } from 'utils/storage';+const set = (key: string, value: string): boolean => {try {- localStorage.setItem(key, value);+ localStorage.setItem(getScopedKey(key), value);return true;- } catch (e) {+ } catch {return false;}};diff --git a/frontend/src/api/browser/sessionstorage/__tests__/get.test.ts b/frontend/src/api/browser/sessionstorage/__tests__/get.test.tsnew file mode 100644index 0000000000..33f750aedc--- /dev/null+++ b/frontend/src/api/browser/sessionstorage/__tests__/get.test.ts@@ -0,0 +1,81 @@+/**+ * sessionstorage/get — lazy migration tests.+ * Mirrors the localStorage get tests; same logic, different storage.+ */++type GetModule = typeof import('../get');++function loadGetModule(href: string): GetModule {+ const base = document.createElement('base');+ base.setAttribute('href', href);+ document.head.append(base);++ let mod!: GetModule;+ jest.isolateModules(() => {+ // oxlint-disable-next-line typescript-eslint/no-require-imports, typescript-eslint/no-var-requires+ mod = require('../get');+ });+ return mod;+}++afterEach(() => {+ for (const el of document.head.querySelectorAll('base')) {+ el.remove();+ }+ sessionStorage.clear();+});++describe('get — root path "/"', () => {+ it('reads the bare key', () => {+ const { default: get } = loadGetModule('/');+ sessionStorage.setItem('retry-lazy-refreshed', 'true');+ expect(get('retry-lazy-refreshed')).toBe('true');+ });++ it('returns null when key is absent', () => {+ const { default: get } = loadGetModule('/');+ expect(get('MISSING')).toBeNull();+ });++ it('does NOT promote bare keys at root', () => {+ const { default: get } = loadGetModule('/');+ sessionStorage.setItem('retry-lazy-refreshed', 'true');+ get('retry-lazy-refreshed');+ expect(sessionStorage.getItem('retry-lazy-refreshed')).toBe('true');+ });+});++describe('get — prefixed path "/signoz/"', () => {+ it('reads an already-scoped key directly', () => {+ const { default: get } = loadGetModule('/signoz/');+ sessionStorage.setItem('/signoz/retry-lazy-refreshed', 'true');+ expect(get('retry-lazy-refreshed')).toBe('true');+ });++ it('returns null when neither scoped nor bare key exists', () => {+ const { default: get } = loadGetModule('/signoz/');+ expect(get('MISSING')).toBeNull();+ });++ it('lazy-migrates bare key to scoped key on first read', () => {+ const { default: get } = loadGetModule('/signoz/');+ sessionStorage.setItem('retry-lazy-refreshed', 'true');++ const result = get('retry-lazy-refreshed');++ expect(result).toBe('true');+ expect(sessionStorage.getItem('/signoz/retry-lazy-refreshed')).toBe('true');+ expect(sessionStorage.getItem('retry-lazy-refreshed')).toBeNull();+ });++ it('scoped key takes precedence over bare key', () => {+ const { default: get } = loadGetModule('/signoz/');+ sessionStorage.setItem('retry-lazy-refreshed', 'bare');+ sessionStorage.setItem('/signoz/retry-lazy-refreshed', 'scoped');++ expect(get('retry-lazy-refreshed')).toBe('scoped');+ expect(sessionStorage.getItem('retry-lazy-refreshed')).toBe('bare');+ });+});++export {};diff --git a/frontend/src/api/browser/sessionstorage/get.ts b/frontend/src/api/browser/sessionstorage/get.tsnew file mode 100644index 0000000000..9eb5dfe5b8--- /dev/null+++ b/frontend/src/api/browser/sessionstorage/get.ts@@ -0,0 +1,27 @@+/* oxlint-disable no-restricted-globals */+import { getBasePath } from 'utils/basePath';+import { getScopedKey } from 'utils/storage';++const get = (key: string): string | null => {+ try {+ const scopedKey = getScopedKey(key);+ const value = sessionStorage.getItem(scopedKey);++ // Lazy migration: same pattern as localStorage — promote bare keys written+ // by a previous root deployment to the scoped key on first read.+ if (value === null && getBasePath() !== '/') {+ const bare = sessionStorage.getItem(key);+ if (bare !== null) {+ sessionStorage.setItem(scopedKey, bare);+ sessionStorage.removeItem(key);+ return bare;+ }+ }++ return value;+ } catch {+ return '';+ }+};++export default get;diff --git a/frontend/src/api/browser/sessionstorage/remove.ts b/frontend/src/api/browser/sessionstorage/remove.tsnew file mode 100644index 0000000000..040157115b--- /dev/null+++ b/frontend/src/api/browser/sessionstorage/remove.ts@@ -0,0 +1,13 @@+/* oxlint-disable no-restricted-globals */+import { getScopedKey } from 'utils/storage';++const remove = (key: string): boolean => {+ try {+ sessionStorage.removeItem(getScopedKey(key));+ return true;+ } catch {+ return false;+ }+};++export default remove;diff --git a/frontend/src/api/browser/sessionstorage/set.ts b/frontend/src/api/browser/sessionstorage/set.tsnew file mode 100644index 0000000000..791416954f--- /dev/null+++ b/frontend/src/api/browser/sessionstorage/set.ts@@ -0,0 +1,13 @@+/* oxlint-disable no-restricted-globals */+import { getScopedKey } from 'utils/storage';++const set = (key: string, value: string): boolean => {+ try {+ sessionStorage.setItem(getScopedKey(key), value);+ return true;+ } catch {+ return false;+ }+};++export default set;diff --git a/frontend/src/api/generatedAPIInstance.ts b/frontend/src/api/generatedAPIInstance.tsindex c29d0b0a4e..d996b74308 100644--- a/frontend/src/api/generatedAPIInstance.ts+++ b/frontend/src/api/generatedAPIInstance.ts@@ -20,6 +20,7 @@ export const GeneratedAPIInstance = <T>(generatedAPIAxiosInstance.interceptors.request.use(interceptorsRequestBasePath);generatedAPIAxiosInstance.interceptors.request.use(interceptorsRequestResponse);+generatedAPIAxiosInstance.interceptors.request.use(interceptorsRequestBasePath);generatedAPIAxiosInstance.interceptors.response.use(interceptorsResponse,interceptorRejected,diff --git a/frontend/src/api/logs/livetail.ts b/frontend/src/api/logs/livetail.tsindex 150f63d193..1b54c43fa7 100644--- a/frontend/src/api/logs/livetail.ts+++ b/frontend/src/api/logs/livetail.ts@@ -3,13 +3,16 @@ import getLocalStorageKey from 'api/browser/localstorage/get';import { ENVIRONMENT } from 'constants/env';import { LOCALSTORAGE } from 'constants/localStorage';import { EventSourcePolyfill } from 'event-source-polyfill';+import { withBasePath } from 'utils/basePath';// 10 min in msconst TIMEOUT_IN_MS = 10 * 60 * 1000;export const LiveTail = (queryParams: string): EventSourcePolyfill =>new EventSourcePolyfill(- `${ENVIRONMENT.baseURL}${apiV1}logs/tail?${queryParams}`,+ ENVIRONMENT.baseURL+ ? `${ENVIRONMENT.baseURL}${apiV1}logs/tail?${queryParams}`+ : withBasePath(`${apiV1}logs/tail?${queryParams}`),{headers: {Authorization: `Bearer ${getLocalStorageKey(LOCALSTORAGE.AUTH_TOKEN)}`,diff --git a/frontend/src/components/QueryBuilderV2/QueryV2/previousQuery.utils.ts b/frontend/src/components/QueryBuilderV2/QueryV2/previousQuery.utils.tsindex 0e93369314..cd7fa7758c 100644--- a/frontend/src/components/QueryBuilderV2/QueryV2/previousQuery.utils.ts+++ b/frontend/src/components/QueryBuilderV2/QueryV2/previousQuery.utils.ts@@ -1,10 +1,13 @@+import getSessionStorageApi from 'api/browser/sessionstorage/get';+import removeSessionStorageApi from 'api/browser/sessionstorage/remove';+import setSessionStorageApi from 'api/browser/sessionstorage/set';import { IBuilderQuery } from 'types/api/queryBuilder/queryBuilderData';export const PREVIOUS_QUERY_KEY = 'previousQuery';function getPreviousQueryFromStore(): Record<string, IBuilderQuery> {try {- const raw = sessionStorage.getItem(PREVIOUS_QUERY_KEY);+ const raw = getSessionStorageApi(PREVIOUS_QUERY_KEY);if (!raw) {return {};}@@ -17,7 +20,7 @@ function getPreviousQueryFromStore(): Record<string, IBuilderQuery> {function writePreviousQueryToStore(store: Record<string, IBuilderQuery>): void {try {- sessionStorage.setItem(PREVIOUS_QUERY_KEY, JSON.stringify(store));+ setSessionStorageApi(PREVIOUS_QUERY_KEY, JSON.stringify(store));} catch {// ignore quota or serialization errors}@@ -63,7 +66,7 @@ export const removeKeyFromPreviousQuery = (key: string): void => {export const clearPreviousQuery = (): void => {try {- sessionStorage.removeItem(PREVIOUS_QUERY_KEY);+ removeSessionStorageApi(PREVIOUS_QUERY_KEY);} catch {// no-op}diff --git a/frontend/src/components/ResizeTable/DynamicColumnTable.tsx b/frontend/src/components/ResizeTable/DynamicColumnTable.tsxindex 137285414a..70ba75270e 100644--- a/frontend/src/components/ResizeTable/DynamicColumnTable.tsx+++ b/frontend/src/components/ResizeTable/DynamicColumnTable.tsx@@ -108,8 +108,7 @@ function DynamicColumnTable({// Update URL with new page number while preserving other paramsurlQuery.set('page', page.toString());- const newUrl = `${window.location.pathname}?${urlQuery.toString()}`;- safeNavigate(newUrl);+ safeNavigate({ search: `?${urlQuery.toString()}` });// Call original pagination handler if providedif (pagination?.onChange && !!pageSize) {diff --git a/frontend/src/components/ResizeTable/utils.ts b/frontend/src/components/ResizeTable/utils.tsindex d3bba5c395..1f1b029f7c 100644--- a/frontend/src/components/ResizeTable/utils.ts+++ b/frontend/src/components/ResizeTable/utils.ts@@ -1,3 +1,6 @@+import getLocalStorageKey from 'api/browser/localstorage/get';+import setLocalStorageKey from 'api/browser/localstorage/set';+import { DynamicColumnsKey } from './contants';import {GetNewColumnDataFunction,@@ -12,7 +15,7 @@ export const getVisibleColumns: GetVisibleColumnsFunction = ({}) => {let columnVisibilityData: { [key: string]: boolean };try {- const storedData = localStorage.getItem(tablesource);+ const storedData = getLocalStorageKey(tablesource);if (typeof storedData === 'string' && dynamicColumns) {columnVisibilityData = JSON.parse(storedData);return dynamicColumns.filter((column) => {@@ -28,7 +31,7 @@ export const getVisibleColumns: GetVisibleColumnsFunction = ({initialColumnVisibility[key] = false;});- localStorage.setItem(tablesource, JSON.stringify(initialColumnVisibility));+ setLocalStorageKey(tablesource, JSON.stringify(initialColumnVisibility));} catch (error) {console.error(error);}@@ -42,14 +45,14 @@ export const setVisibleColumns = ({dynamicColumns,}: SetVisibleColumnsProps): void => {try {- const storedData = localStorage.getItem(tablesource);+ const storedData = getLocalStorageKey(tablesource);if (typeof storedData === 'string' && dynamicColumns) {const columnVisibilityData = JSON.parse(storedData);const { key } = dynamicColumns[index];if (key) {columnVisibilityData[key] = checked;}- localStorage.setItem(tablesource, JSON.stringify(columnVisibilityData));+ setLocalStorageKey(tablesource, JSON.stringify(columnVisibilityData));}} catch (error) {console.error(error);diff --git a/frontend/src/container/DashboardContainer/components/DashboardHeader/DashboardBreadcrumbs.tsx b/frontend/src/container/DashboardContainer/components/DashboardHeader/DashboardBreadcrumbs.tsxindex 38cde5c004..3940027fc1 100644--- a/frontend/src/container/DashboardContainer/components/DashboardHeader/DashboardBreadcrumbs.tsx+++ b/frontend/src/container/DashboardContainer/components/DashboardHeader/DashboardBreadcrumbs.tsx@@ -1,5 +1,6 @@import { useCallback, useRef } from 'react';import { Button } from 'antd';+import getSessionStorageApi from 'api/browser/sessionstorage/get';import ROUTES from 'constants/routes';import { DASHBOARDS_LIST_QUERY_PARAMS_STORAGE_KEY } from 'hooks/dashboard/useDashboardsListQueryParams';import { useSafeNavigate } from 'hooks/useSafeNavigate';@@ -26,7 +27,7 @@ function DashboardBreadcrumbs(): JSX.Element {const { title = '', image = Base64Icons[0] } = selectedData || {};const goToListPage = useCallback(() => {- const dashboardsListQueryParamsString = sessionStorage.getItem(+ const dashboardsListQueryParamsString = getSessionStorageApi(DASHBOARDS_LIST_QUERY_PARAMS_STORAGE_KEY,);diff --git a/frontend/src/container/DashboardContainer/visualization/panels/utils/legendVisibilityUtils.ts b/frontend/src/container/DashboardContainer/visualization/panels/utils/legendVisibilityUtils.tsindex ccd2c5ee7a..48f960f486 100644--- a/frontend/src/container/DashboardContainer/visualization/panels/utils/legendVisibilityUtils.ts+++ b/frontend/src/container/DashboardContainer/visualization/panels/utils/legendVisibilityUtils.ts@@ -1,3 +1,6 @@+import getLocalStorageKey from 'api/browser/localstorage/get';+import removeLocalStorageKey from 'api/browser/localstorage/remove';+import setLocalStorageKey from 'api/browser/localstorage/set';import { LOCALSTORAGE } from 'constants/localStorage';import { GraphVisibilityState, SeriesVisibilityItem } from '../types';@@ -12,7 +15,7 @@ export function getStoredSeriesVisibility(widgetId: string,): SeriesVisibilityItem[] | null {try {- const storedData = localStorage.getItem(LOCALSTORAGE.GRAPH_VISIBILITY_STATES);+ const storedData = getLocalStorageKey(LOCALSTORAGE.GRAPH_VISIBILITY_STATES);if (!storedData) {return null;@@ -29,7 +32,7 @@ export function getStoredSeriesVisibility(} catch (error) {if (error instanceof SyntaxError) {// If the stored data is malformed, remove it- localStorage.removeItem(LOCALSTORAGE.GRAPH_VISIBILITY_STATES);+ removeLocalStorageKey(LOCALSTORAGE.GRAPH_VISIBILITY_STATES);}// Silently handle parsing errors - fall back to default visibilityreturn null;@@ -42,7 +45,7 @@ export function updateSeriesVisibilityToLocalStorage(): void {let visibilityStates: GraphVisibilityState[] = [];try {- const storedData = localStorage.getItem(LOCALSTORAGE.GRAPH_VISIBILITY_STATES);+ const storedData = getLocalStorageKey(LOCALSTORAGE.GRAPH_VISIBILITY_STATES);visibilityStates = JSON.parse(storedData || '[]');} catch (error) {if (error instanceof SyntaxError) {@@ -63,7 +66,7 @@ export function updateSeriesVisibilityToLocalStorage(];}- localStorage.setItem(+ setLocalStorageKey(LOCALSTORAGE.GRAPH_VISIBILITY_STATES,JSON.stringify(visibilityStates),);diff --git a/frontend/src/container/ExplorerOptions/ExplorerOptions.tsx b/frontend/src/container/ExplorerOptions/ExplorerOptions.tsxindex 03767ebf00..d914d0ea96 100644--- a/frontend/src/container/ExplorerOptions/ExplorerOptions.tsx+++ b/frontend/src/container/ExplorerOptions/ExplorerOptions.tsx@@ -22,6 +22,8 @@ import {Tooltip,Typography,} from 'antd';+import getLocalStorageKey from 'api/browser/localstorage/get';+import setLocalStorageKey from 'api/browser/localstorage/set';import logEvent from 'api/common/logEvent';import { TelemetryFieldKey } from 'api/v5/v5';import axios from 'axios';@@ -472,7 +474,7 @@ function ExplorerOptions({value: string;}): void => {// Retrieve stored views from local storage- const storedViews = localStorage.getItem(PRESERVED_VIEW_LOCAL_STORAGE_KEY);+ const storedViews = getLocalStorageKey(PRESERVED_VIEW_LOCAL_STORAGE_KEY);// Initialize or parse the stored viewsconst updatedViews: PreservedViewsInLocalStorage = storedViews@@ -486,7 +488,7 @@ function ExplorerOptions({};// Save the updated views back to local storage- localStorage.setItem(+ setLocalStorageKey(PRESERVED_VIEW_LOCAL_STORAGE_KEY,JSON.stringify(updatedViews),);@@ -537,7 +539,7 @@ function ExplorerOptions({const removeCurrentViewFromLocalStorage = (): void => {// Retrieve stored views from local storage- const storedViews = localStorage.getItem(PRESERVED_VIEW_LOCAL_STORAGE_KEY);+ const storedViews = getLocalStorageKey(PRESERVED_VIEW_LOCAL_STORAGE_KEY);if (storedViews) {// Parse the stored views@@ -547,7 +549,7 @@ function ExplorerOptions({delete parsedViews[PRESERVED_VIEW_TYPE];// Update local storage with the modified views- localStorage.setItem(+ setLocalStorageKey(PRESERVED_VIEW_LOCAL_STORAGE_KEY,JSON.stringify(parsedViews),);@@ -672,7 +674,7 @@ function ExplorerOptions({}const parsedPreservedView = JSON.parse(- localStorage.getItem(PRESERVED_VIEW_LOCAL_STORAGE_KEY) || '{}',+ getLocalStorageKey(PRESERVED_VIEW_LOCAL_STORAGE_KEY) || '{}',);const preservedView = parsedPreservedView[PRESERVED_VIEW_TYPE] || {};diff --git a/frontend/src/container/ExplorerOptions/utils.ts b/frontend/src/container/ExplorerOptions/utils.tsindex b8d6f6e614..61c0264d8c 100644--- a/frontend/src/container/ExplorerOptions/utils.ts+++ b/frontend/src/container/ExplorerOptions/utils.ts@@ -1,4 +1,6 @@import { Color } from '@signozhq/design-tokens';+import getLocalStorageKey from 'api/browser/localstorage/get';+import setLocalStorageKey from 'api/browser/localstorage/set';import { showErrorNotification } from 'components/ExplorerCard/utils';import { LOCALSTORAGE } from 'constants/localStorage';import { QueryParams } from 'constants/query';@@ -71,7 +73,7 @@ export const generateRGBAFromHex = (hex: string, opacity: number): string =>export const getExplorerToolBarVisibility = (dataSource: string): boolean => {try {- const showExplorerToolbar = localStorage.getItem(+ const showExplorerToolbar = getLocalStorageKey(LOCALSTORAGE.SHOW_EXPLORER_TOOLBAR,);if (showExplorerToolbar === null) {@@ -84,7 +86,7 @@ export const getExplorerToolBarVisibility = (dataSource: string): boolean => {[DataSource.TRACES]: true,[DataSource.LOGS]: true,};- localStorage.setItem(+ setLocalStorageKey(LOCALSTORAGE.SHOW_EXPLORER_TOOLBAR,JSON.stringify(parsedShowExplorerToolbar),);@@ -103,13 +105,13 @@ export const setExplorerToolBarVisibility = (dataSource: string,): void => {try {- const showExplorerToolbar = localStorage.getItem(+ const showExplorerToolbar = getLocalStorageKey(LOCALSTORAGE.SHOW_EXPLORER_TOOLBAR,);if (showExplorerToolbar) {const parsedShowExplorerToolbar = JSON.parse(showExplorerToolbar);parsedShowExplorerToolbar[dataSource] = value;- localStorage.setItem(+ setLocalStorageKey(LOCALSTORAGE.SHOW_EXPLORER_TOOLBAR,JSON.stringify(parsedShowExplorerToolbar),);diff --git a/frontend/src/container/GridCardLayout/GridCard/FullView/utils.ts b/frontend/src/container/GridCardLayout/GridCard/FullView/utils.tsindex 0a7a52a5c1..4ae672f287 100644--- a/frontend/src/container/GridCardLayout/GridCard/FullView/utils.ts+++ b/frontend/src/container/GridCardLayout/GridCard/FullView/utils.ts@@ -1,3 +1,5 @@+import getLocalStorageKey from 'api/browser/localstorage/get';+import setLocalStorageKey from 'api/browser/localstorage/set';import { LOCALSTORAGE } from 'constants/localStorage';import getLabelName from 'lib/getLabelName';import { QueryData } from 'types/api/widgets/getQuery';@@ -100,7 +102,7 @@ export const saveLegendEntriesToLocalStorage = ({try {existingEntries = JSON.parse(- localStorage.getItem(LOCALSTORAGE.GRAPH_VISIBILITY_STATES) || '[]',+ getLocalStorageKey(LOCALSTORAGE.GRAPH_VISIBILITY_STATES) || '[]',);} catch (error) {console.error('Error parsing LEGEND_GRAPH from local storage', error);@@ -115,7 +117,7 @@ export const saveLegendEntriesToLocalStorage = ({}try {- localStorage.setItem(+ setLocalStorageKey(LOCALSTORAGE.GRAPH_VISIBILITY_STATES,JSON.stringify(existingEntries),);diff --git a/frontend/src/container/GridCardLayout/GridCard/utils.ts b/frontend/src/container/GridCardLayout/GridCard/utils.tsindex 70fcb2b5e3..80f64dac11 100644--- a/frontend/src/container/GridCardLayout/GridCard/utils.ts+++ b/frontend/src/container/GridCardLayout/GridCard/utils.ts@@ -1,5 +1,6 @@/* eslint-disable sonarjs/cognitive-complexity */import type { NotificationInstance } from 'antd/es/notification/interface';+import getLocalStorageKey from 'api/browser/localstorage/get';import { NavigateToExplorerProps } from 'components/CeleryTask/useNavigateToExplorer';import { LOCALSTORAGE } from 'constants/localStorage';import { PANEL_TYPES } from 'constants/queryBuilder';@@ -44,8 +45,8 @@ export const getLocalStorageGraphVisibilityState = ({],};- if (localStorage.getItem(LOCALSTORAGE.GRAPH_VISIBILITY_STATES) !== null) {- const legendGraphFromLocalStore = localStorage.getItem(+ if (getLocalStorageKey(LOCALSTORAGE.GRAPH_VISIBILITY_STATES) !== null) {+ const legendGraphFromLocalStore = getLocalStorageKey(LOCALSTORAGE.GRAPH_VISIBILITY_STATES,);let legendFromLocalStore: {@@ -94,8 +95,8 @@ export const getGraphVisibilityStateOnDataChange = ({graphVisibilityStates: Array(options.series.length).fill(true),legendEntry: showAllDataSet(options),};- if (localStorage.getItem(LOCALSTORAGE.GRAPH_VISIBILITY_STATES) !== null) {- const legendGraphFromLocalStore = localStorage.getItem(+ if (getLocalStorageKey(LOCALSTORAGE.GRAPH_VISIBILITY_STATES) !== null) {+ const legendGraphFromLocalStore = getLocalStorageKey(LOCALSTORAGE.GRAPH_VISIBILITY_STATES,);let legendFromLocalStore: {diff --git a/frontend/src/container/ListOfDashboard/DashboardsList.tsx b/frontend/src/container/ListOfDashboard/DashboardsList.tsxindex ce1612cbd2..e74058a3e0 100644--- a/frontend/src/container/ListOfDashboard/DashboardsList.tsx+++ b/frontend/src/container/ListOfDashboard/DashboardsList.tsx@@ -28,6 +28,8 @@ import {Typography,} from 'antd';import type { TableProps } from 'antd/lib';+import getLocalStorageKey from 'api/browser/localstorage/get';+import setLocalStorageKey from 'api/browser/localstorage/set';import logEvent from 'api/common/logEvent';import createDashboard from 'api/v1/dashboards/create';import { AxiosError } from 'axios';@@ -147,7 +149,7 @@ function DashboardsList(): JSX.Element {);const getLocalStorageDynamicColumns = (): DashboardDynamicColumns => {- const dashboardDynamicColumnsString = localStorage.getItem('dashboard');+ const dashboardDynamicColumnsString = getLocalStorageKey('dashboard');let dashboardDynamicColumns: DashboardDynamicColumns = {createdAt: true,createdBy: true,@@ -161,7 +163,7 @@ function DashboardsList(): JSX.Element {);if (isEmpty(tempDashboardDynamicColumns)) {- localStorage.setItem('dashboard', JSON.stringify(dashboardDynamicColumns));+ setLocalStorageKey('dashboard', JSON.stringify(dashboardDynamicColumns));} else {dashboardDynamicColumns = { ...tempDashboardDynamicColumns };}@@ -169,7 +171,7 @@ function DashboardsList(): JSX.Element {console.error(error);}} else {- localStorage.setItem('dashboard', JSON.stringify(dashboardDynamicColumns));+ setLocalStorageKey('dashboard', JSON.stringify(dashboardDynamicColumns));}return dashboardDynamicColumns;@@ -183,7 +185,7 @@ function DashboardsList(): JSX.Element {visibleColumns: DashboardDynamicColumns,): void {try {- localStorage.setItem('dashboard', JSON.stringify(visibleColumns));+ setLocalStorageKey('dashboard', JSON.stringify(visibleColumns));} catch (error) {console.error(error);}diff --git a/frontend/src/hooks/dashboard/useDashboardsListQueryParams.ts b/frontend/src/hooks/dashboard/useDashboardsListQueryParams.tsindex f7a49b5b87..2043ab5a94 100644--- a/frontend/src/hooks/dashboard/useDashboardsListQueryParams.ts+++ b/frontend/src/hooks/dashboard/useDashboardsListQueryParams.ts@@ -1,4 +1,5 @@import { useState } from 'react';+import setSessionStorageApi from 'api/browser/sessionstorage/set';import { useSafeNavigate } from 'hooks/useSafeNavigate';import useUrlQuery from 'hooks/useUrlQuery';import isEqual from 'lodash-es/isEqual';@@ -61,7 +62,7 @@ function useDashboardsListQueryParams(): {const queryParamsString = params.toString();- sessionStorage.setItem(+ setSessionStorageApi(DASHBOARDS_LIST_QUERY_PARAMS_STORAGE_KEY,queryParamsString,);diff --git a/frontend/src/hooks/useLocalStorage.ts b/frontend/src/hooks/useLocalStorage.tsindex 89af2f5f03..d39858c75a 100644--- a/frontend/src/hooks/useLocalStorage.ts+++ b/frontend/src/hooks/useLocalStorage.ts@@ -1,34 +1,20 @@import { useCallback, useEffect, useRef, useState } from 'react';+import getLocalStorageApi from 'api/browser/localstorage/get';+import removeLocalStorageApi from 'api/browser/localstorage/remove';+import setLocalStorageApi from 'api/browser/localstorage/set';-/**- * A React hook for interacting with localStorage.- * It allows getting, setting, and removing items from localStorage.- *- * @template T The type of the value to be stored.- * @param {string} key The localStorage key.- * @param {T | (() => T)} defaultValue The default value to use if no value is found in localStorage,- * @returns {[T, (value: T | ((prevState: T) => T)) => void, () => void]}- * A tuple containing:- * - The current value from state (and localStorage).- * - A function to set the value (updates state and localStorage).- * - A function to remove the value from localStorage and reset state to defaultValue.- */export function useLocalStorage<T>(key: string,defaultValue: T | (() => T),): [T, (value: T | ((prevState: T) => T)) => void, () => void] {- // Stabilize the defaultValue to prevent unnecessary re-rendersconst defaultValueRef = useRef<T | (() => T)>(defaultValue);- // Update the ref if defaultValue changes (for cases where it's intentionally dynamic)useEffect(() => {if (defaultValueRef.current !== defaultValue) {defaultValueRef.current = defaultValue;}}, [defaultValue]);- // This function resolves the defaultValue if it's a function,- // and handles potential errors during localStorage access or JSON parsing.const readValueFromStorage = useCallback((): T => {const resolveddefaultValue =defaultValueRef.current instanceof Function@@ -36,33 +22,25 @@ export function useLocalStorage<T>(: defaultValueRef.current;try {- const item = window.localStorage.getItem(key);- // If item exists, parse it, otherwise return the resolved default value.+ const item = getLocalStorageApi(key);if (item) {return JSON.parse(item) as T;}} catch (error) {- // Log error and fall back to default value if reading/parsing fails.console.warn(`Error reading localStorage key "${key}":`, error);}return resolveddefaultValue;}, [key]);- // Initialize state by reading from localStorage.const [storedValue, setStoredValue] = useState<T>(readValueFromStorage);- // This function updates both localStorage and the React state.const setValue = useCallback((value: T | ((prevState: T) => T)) => {try {- // If a function is passed to setValue, it receives the latest value from storage.const latestValueFromStorage = readValueFromStorage();const valueToStore =value instanceof Function ? value(latestValueFromStorage) : value;-- // Save to localStorage.- window.localStorage.setItem(key, JSON.stringify(valueToStore));- // Update React state.+ setLocalStorageApi(key, JSON.stringify(valueToStore));setStoredValue(valueToStore);} catch (error) {console.warn(`Error setting localStorage key "${key}":`, error);@@ -71,11 +49,9 @@ export function useLocalStorage<T>([key, readValueFromStorage],);- // This function removes the item from localStorage and resets the React state.const removeValue = useCallback(() => {try {- window.localStorage.removeItem(key);- // Reset state to the (potentially resolved) defaultValue.+ removeLocalStorageApi(key);setStoredValue(defaultValueRef.current instanceof Function? (defaultValueRef.current as () => T)()@@ -86,12 +62,9 @@ export function useLocalStorage<T>(}}, [key]);- // useEffect to update the storedValue if the key changes,- // or if the defaultValue prop changes causing readValueFromStorage to change.- // This ensures the hook reflects the correct localStorage item if its key prop dynamically changes.useEffect(() => {setStoredValue(readValueFromStorage());- }, [key, readValueFromStorage]); // Re-run if key or the read function changes.+ }, [key, readValueFromStorage]);return [storedValue, setValue, removeValue];}diff --git a/frontend/src/providers/Dashboard/helpers/selectedRowWidgetIdHelper.ts b/frontend/src/providers/Dashboard/helpers/selectedRowWidgetIdHelper.tsindex 3d82b897fa..8b5e15abba 100644--- a/frontend/src/providers/Dashboard/helpers/selectedRowWidgetIdHelper.ts+++ b/frontend/src/providers/Dashboard/helpers/selectedRowWidgetIdHelper.ts@@ -1,3 +1,8 @@+import getSessionStorageApi from 'api/browser/sessionstorage/get';+import removeSessionStorageApi from 'api/browser/sessionstorage/remove';+import setSessionStorageApi from 'api/browser/sessionstorage/set';+import { getScopedKey } from 'utils/storage';+const PREFIX = 'dashboard_row_widget_';function getKey(dashboardId: string): string {@@ -8,21 +13,25 @@ export function setSelectedRowWidgetId(dashboardId: string,widgetId: string,): void {- const key = getKey(dashboardId);+ const unscopedKey = getKey(dashboardId);+ const scopedPrefix = getScopedKey(PREFIX);+ const scopedKey = getScopedKey(unscopedKey);- // remove all other selected widget ids for the dashboard before setting the new one- // to ensure only one widget is selected at a time. Helps out in weird navigate and refresh scenarios+ // Object.keys returns the raw/already-scoped keys from the browser.+ // Direct sessionStorage.removeItem is intentional here — k is already fully scoped.+ // oxlint-disable-next-line no-restricted-globalsObject.keys(sessionStorage)- .filter((k) => k.startsWith(PREFIX) && k !== key)+ .filter((k) => k.startsWith(scopedPrefix) && k !== scopedKey)+ // oxlint-disable-next-line no-restricted-globals.forEach((k) => sessionStorage.removeItem(k));- sessionStorage.setItem(key, widgetId);+ setSessionStorageApi(unscopedKey, widgetId);}export function getSelectedRowWidgetId(dashboardId: string): string | null {- return sessionStorage.getItem(getKey(dashboardId));+ return getSessionStorageApi(getKey(dashboardId));}export function clearSelectedRowWidgetId(dashboardId: string): void {- sessionStorage.removeItem(getKey(dashboardId));+ removeSessionStorageApi(getKey(dashboardId));}diff --git a/frontend/src/providers/Timezone.tsx b/frontend/src/providers/Timezone.tsxindex 19ec756d7d..be740a7328 100644--- a/frontend/src/providers/Timezone.tsx+++ b/frontend/src/providers/Timezone.tsx@@ -9,6 +9,8 @@ import React, {useMemo,useState,} from 'react';+import getLocalStorageKey from 'api/browser/localstorage/get';+import setLocalStorageKey from 'api/browser/localstorage/set';import {getBrowserTimezone,getTimezoneObjectByTimezoneString,@@ -43,7 +45,7 @@ function TimezoneProvider({}): JSX.Element {const getStoredTimezoneValue = (): Timezone | null => {try {- const timezoneValue = localStorage.getItem(LOCALSTORAGE.PREFERRED_TIMEZONE);+ const timezoneValue = getLocalStorageKey(LOCALSTORAGE.PREFERRED_TIMEZONE);if (timezoneValue) {return getTimezoneObjectByTimezoneString(timezoneValue);}@@ -55,7 +57,7 @@ function TimezoneProvider({const setStoredTimezoneValue = (value: string): void => {try {- localStorage.setItem(LOCALSTORAGE.PREFERRED_TIMEZONE, value);+ setLocalStorageKey(LOCALSTORAGE.PREFERRED_TIMEZONE, value);} catch (error) {console.error('Error saving timezone to localStorage:', error);}diff --git a/frontend/src/providers/preferences/configs/logsUpdaterConfig.ts b/frontend/src/providers/preferences/configs/logsUpdaterConfig.tsindex 3f7d3c1964..ec4e46f0a3 100644--- a/frontend/src/providers/preferences/configs/logsUpdaterConfig.ts+++ b/frontend/src/providers/preferences/configs/logsUpdaterConfig.ts@@ -1,4 +1,5 @@import { Dispatch, SetStateAction } from 'react';+import getLocalStorageKey from 'api/browser/localstorage/get';import setLocalStorageKey from 'api/browser/localstorage/set';import { TelemetryFieldKey } from 'api/v5/v5';import { LOCALSTORAGE } from 'constants/localStorage';@@ -48,7 +49,7 @@ const getLogsUpdaterConfig = (// Also update local storageconst local = JSON.parse(- localStorage.getItem(LOCALSTORAGE.LOGS_LIST_OPTIONS) || '{}',+ getLocalStorageKey(LOCALSTORAGE.LOGS_LIST_OPTIONS) || '{}',);local.selectColumns = newColumns;setLocalStorageKey(LOCALSTORAGE.LOGS_LIST_OPTIONS, JSON.stringify(local));@@ -76,7 +77,7 @@ const getLogsUpdaterConfig = (// Also update local storageconst local = JSON.parse(- localStorage.getItem(LOCALSTORAGE.LOGS_LIST_OPTIONS) || '{}',+ getLocalStorageKey(LOCALSTORAGE.LOGS_LIST_OPTIONS) || '{}',);Object.assign(local, newFormatting);setLocalStorageKey(LOCALSTORAGE.LOGS_LIST_OPTIONS, JSON.stringify(local));diff --git a/frontend/src/providers/preferences/configs/tracesUpdaterConfig.ts b/frontend/src/providers/preferences/configs/tracesUpdaterConfig.tsindex 86cbd2b56d..6470681e86 100644--- a/frontend/src/providers/preferences/configs/tracesUpdaterConfig.ts+++ b/frontend/src/providers/preferences/configs/tracesUpdaterConfig.ts@@ -1,4 +1,5 @@import { Dispatch, SetStateAction } from 'react';+import getLocalStorageKey from 'api/browser/localstorage/get';import setLocalStorageKey from 'api/browser/localstorage/set';import { TelemetryFieldKey } from 'api/v5/v5';import { LOCALSTORAGE } from 'constants/localStorage';@@ -37,7 +38,7 @@ const getTracesUpdaterConfig = (});const local = JSON.parse(- localStorage.getItem(LOCALSTORAGE.TRACES_LIST_OPTIONS) || '{}',+ getLocalStorageKey(LOCALSTORAGE.TRACES_LIST_OPTIONS) || '{}',);local.selectColumns = newColumns;setLocalStorageKey(LOCALSTORAGE.TRACES_LIST_OPTIONS, JSON.stringify(local));diff --git a/frontend/src/store/actions/trace/util.ts b/frontend/src/store/actions/trace/util.tsindex df2955bbb8..29d16bdb09 100644--- a/frontend/src/store/actions/trace/util.ts+++ b/frontend/src/store/actions/trace/util.ts@@ -70,7 +70,7 @@ export const updateURL = (userSelectedFilter: JSON.stringify(Object.fromEntries(userSelectedFilter)),};history.replace(- `${window.location.pathname}?${createQueryParams(queryParams)}`,+ `${history.location.pathname}?${createQueryParams(queryParams)}`,);};diff --git a/frontend/src/store/reducers/logs.ts b/frontend/src/store/reducers/logs.tsindex 4e46d00e69..4739d59366 100644--- a/frontend/src/store/reducers/logs.ts+++ b/frontend/src/store/reducers/logs.ts@@ -29,6 +29,7 @@ import {UPDATE_SELECTED_FIELDS,} from 'types/actions/logs';import { ILogsReducer } from 'types/reducer/logs';+import { withBasePath } from 'utils/basePath';const supportedLogsOrder = [OrderPreferenceItems.ASC,@@ -37,7 +38,7 @@ const supportedLogsOrder = [function getLogsOrder(): OrderPreferenceItems {// set the value of order from the URL only when order query param is present and the user is landing on the old logs explorer page- if (window.location.pathname === ROUTES.OLD_LOGS_EXPLORER) {+ if (window.location.pathname === withBasePath(ROUTES.OLD_LOGS_EXPLORER)) {const orderParam = new URLSearchParams(window.location.search).get('order');if (orderParam) {diff --git a/frontend/src/utils/__tests__/app.test.ts b/frontend/src/utils/__tests__/app.test.tsindex a2f82f0ed3..ec9073c88f 100644--- a/frontend/src/utils/__tests__/app.test.ts+++ b/frontend/src/utils/__tests__/app.test.ts@@ -1,23 +1,23 @@-import { getLocation } from 'utils/getLocation';-import { buildAbsolutePath } from '../app';-jest.mock('utils/getLocation');+// buildAbsolutePath reads history.location.pathname (basename-relative) rather than+// window.location.pathname, so we mock lib/history instead of utils/getLocation.+jest.mock('lib/history', () => ({+ __esModule: true,+ default: {+ location: { pathname: '/' },+ },+}));++// oxlint-disable-next-line typescript-eslint/no-require-imports, typescript-eslint/no-var-requires+const mockHistory = require('lib/history').default as {+ location: { pathname: string };+};const BASE_PATH = '/some-base-path';const mockLocation = (pathname: string): void => {- (getLocation as jest.Mock).mockReturnValue({- pathname,- href: `http://localhost:8080${pathname}`,- origin: 'http://localhost:8080',- protocol: 'http:',- host: 'localhost',- hostname: 'localhost',- port: '',- search: '',- hash: '',- });+ mockHistory.location.pathname = pathname;};describe('buildAbsolutePath', () => {diff --git a/frontend/src/utils/__tests__/storage.test.ts b/frontend/src/utils/__tests__/storage.test.tsnew file mode 100644index 0000000000..d8146324c5--- /dev/null+++ b/frontend/src/utils/__tests__/storage.test.ts@@ -0,0 +1,72 @@+/**+ * storage.ts memoizes basePath at module init (via basePath.ts IIFE).+ * Use jest.isolateModules to re-import storage with a fresh DOM state each time.+ */++type StorageModule = typeof import('../storage');++function loadStorageModule(href?: string): StorageModule {+ if (href !== undefined) {+ const base = document.createElement('base');+ base.setAttribute('href', href);+ document.head.append(base);+ }+ let mod!: StorageModule;+ jest.isolateModules(() => {+ // oxlint-disable-next-line typescript-eslint/no-require-imports, typescript-eslint/no-var-requires+ mod = require('../storage');+ });+ return mod;+}++afterEach(() => {+ document.head.querySelectorAll('base').forEach((el) => el.remove());+ localStorage.clear();+});++describe('getScopedKey — root path "/"', () => {+ it('returns the bare key unchanged', () => {+ const { getScopedKey } = loadStorageModule('/');+ expect(getScopedKey('AUTH_TOKEN')).toBe('AUTH_TOKEN');+ });++ it('backward compat: scoped key equals direct localStorage key', () => {+ const { getScopedKey } = loadStorageModule('/');+ localStorage.setItem('AUTH_TOKEN', 'tok');+ expect(localStorage.getItem(getScopedKey('AUTH_TOKEN'))).toBe('tok');+ });+});++describe('getScopedKey — prefixed path "/signoz/"', () => {+ it('prefixes the key with the base path', () => {+ const { getScopedKey } = loadStorageModule('/signoz/');+ expect(getScopedKey('AUTH_TOKEN')).toBe('/signoz/AUTH_TOKEN');+ });++ it('isolates from root namespace', () => {+ const { getScopedKey } = loadStorageModule('/signoz/');+ localStorage.setItem('AUTH_TOKEN', 'root-tok');+ expect(localStorage.getItem(getScopedKey('AUTH_TOKEN'))).toBeNull();+ });+});++describe('getScopedKey — prefixed path "/testing/"', () => {+ it('prefixes the key with /testing/', () => {+ const { getScopedKey } = loadStorageModule('/testing/');+ expect(getScopedKey('THEME')).toBe('/testing/THEME');+ });+});++describe('getScopedKey — prefixed path "/playwright/"', () => {+ it('prefixes the key with /playwright/', () => {+ const { getScopedKey } = loadStorageModule('/playwright/');+ expect(getScopedKey('THEME')).toBe('/playwright/THEME');+ });+});++describe('getScopedKey — no <base> tag', () => {+ it('falls back to bare key (basePath defaults to "/")', () => {+ const { getScopedKey } = loadStorageModule();+ expect(getScopedKey('THEME')).toBe('THEME');+ });+});diff --git a/frontend/src/utils/app.ts b/frontend/src/utils/app.tsindex 61fc72719a..42701c06cd 100644--- a/frontend/src/utils/app.ts+++ b/frontend/src/utils/app.ts@@ -2,8 +2,8 @@ import getLocalStorage from 'api/browser/localstorage/get';import { FeatureKeys } from 'constants/features';import { SKIP_ONBOARDING } from 'constants/onboarding';import dayjs from 'dayjs';+import history from 'lib/history';import { get } from 'lodash-es';-import { getLocation } from 'utils/getLocation';export const isOnboardingSkipped = (): boolean =>getLocalStorage(SKIP_ONBOARDING) === 'true';@@ -40,16 +40,15 @@ export function isIngestionActive(data: any): boolean {const key = get(table, 'columns[0].id');const value = get(table, `rows[0].data["${key}"]`) || '0';- return parseInt(value, 10) > 0;+ return Number.parseInt(value, 10) > 0;}/**- * Builds an absolute path by combining the current page's pathname with a relative path.- *- * @param {Object} params - The parameters for building the absolute path- * @param {string} params.relativePath - The relative path to append to the current pathname- * @param {string} [params.urlQueryString] - Optional query string to append to the final path (without leading '?')+ * Builds a path by combining the current page's pathname with a relative path.*+ * @param {Object} params+ * @param {string} params.relativePath - Relative path to append to the current pathname+ * @param {string} [params.urlQueryString] - Query string without leading '?'* @returns {string} The constructed absolute path, optionally with query string*/export function buildAbsolutePath({@@ -59,14 +58,18 @@ export function buildAbsolutePath({relativePath: string;urlQueryString?: string;}): string {- const { pathname } = getLocation();+ const currentPathname = history.location.pathname;if (!relativePath) {- return urlQueryString ? `${pathname}?${urlQueryString}` : pathname;+ return urlQueryString+ ? `${currentPathname}?${urlQueryString}`+ : currentPathname;}// ensure base path always ends with a forward slash- const basePath = pathname.endsWith('/') ? pathname : `${pathname}/`;+ const basePath = currentPathname.endsWith('/')+ ? currentPathname+ : `${currentPathname}/`;// handle relative path starting with a forward slashconst normalizedRelativePath = relativePath.startsWith('/')diff --git a/frontend/src/utils/customTimeRangeUtils.ts b/frontend/src/utils/customTimeRangeUtils.tsindex 3010b80207..553173b341 100644--- a/frontend/src/utils/customTimeRangeUtils.ts+++ b/frontend/src/utils/customTimeRangeUtils.ts@@ -1,3 +1,6 @@+import getLocalStorageKey from 'api/browser/localstorage/get';+import removeLocalStorageKey from 'api/browser/localstorage/remove';+import setLocalStorageKey from 'api/browser/localstorage/set';import { LOCALSTORAGE } from 'constants/localStorage';import { DateTimeRangeType } from 'container/TopNav/CustomDateTimeModal';import dayjs from 'dayjs';@@ -16,9 +19,7 @@ const MAX_STORED_RANGES = 3;*/export const getCustomTimeRanges = (): CustomTimeRange[] => {try {- const stored = localStorage.getItem(- LOCALSTORAGE.LAST_USED_CUSTOM_TIME_RANGES,- );+ const stored = getLocalStorageKey(LOCALSTORAGE.LAST_USED_CUSTOM_TIME_RANGES);if (!stored) {return [];}@@ -78,7 +79,7 @@ export const addCustomTimeRange = (// Store in localStoragetry {- localStorage.setItem(+ setLocalStorageKey(LOCALSTORAGE.LAST_USED_CUSTOM_TIME_RANGES,JSON.stringify(updatedRanges),);@@ -94,7 +95,7 @@ export const addCustomTimeRange = (*/export const clearCustomTimeRanges = (): void => {try {- localStorage.removeItem(LOCALSTORAGE.LAST_USED_CUSTOM_TIME_RANGES);+ removeLocalStorageKey(LOCALSTORAGE.LAST_USED_CUSTOM_TIME_RANGES);} catch (error) {console.warn('Failed to clear custom time ranges from localStorage:', error);}@@ -112,7 +113,7 @@ export const removeCustomTimeRange = (timestamp: number): CustomTimeRange[] => {);try {- localStorage.setItem(+ setLocalStorageKey(LOCALSTORAGE.LAST_USED_CUSTOM_TIME_RANGES,JSON.stringify(updatedRanges),);diff --git a/frontend/src/utils/lazyWithRetries.ts b/frontend/src/utils/lazyWithRetries.tsindex be6dcbcae3..57df8ec7fb 100644--- a/frontend/src/utils/lazyWithRetries.ts+++ b/frontend/src/utils/lazyWithRetries.ts@@ -1,3 +1,5 @@+import getSessionStorageApi from 'api/browser/sessionstorage/get';+import setSessionStorageApi from 'api/browser/sessionstorage/set';import { SESSIONSTORAGE } from 'constants/sessionStorage';type ComponentImport = () => Promise<any>;@@ -5,18 +7,17 @@ type ComponentImport = () => Promise<any>;export const lazyRetry = (componentImport: ComponentImport): Promise<any> =>new Promise((resolve, reject) => {const hasRefreshed: boolean = JSON.parse(- window.sessionStorage.getItem(SESSIONSTORAGE.RETRY_LAZY_REFRESHED) ||- 'false',+ getSessionStorageApi(SESSIONSTORAGE.RETRY_LAZY_REFRESHED) || 'false',);componentImport().then((component: any) => {- window.sessionStorage.setItem(SESSIONSTORAGE.RETRY_LAZY_REFRESHED, 'false');+ setSessionStorageApi(SESSIONSTORAGE.RETRY_LAZY_REFRESHED, 'false');resolve(component);}).catch((error: Error) => {if (!hasRefreshed) {- window.sessionStorage.setItem(SESSIONSTORAGE.RETRY_LAZY_REFRESHED, 'true');+ setSessionStorageApi(SESSIONSTORAGE.RETRY_LAZY_REFRESHED, 'true');window.location.reload();}diff --git a/frontend/src/utils/storage.ts b/frontend/src/utils/storage.tsnew file mode 100644index 0000000000..df970ceaf8--- /dev/null+++ b/frontend/src/utils/storage.ts@@ -0,0 +1,11 @@+import { getBasePath } from 'utils/basePath';++/**+ * Returns a storage key scoped to the runtime base path.+ * At root ("/") the bare key is returned unchanged — backward compatible.+ * At any other prefix the key is prefixed: "/signoz/AUTH_TOKEN".+ */+export function getScopedKey(key: string): string {+ const basePath = getBasePath();+ return basePath === '/' ? key : `${basePath}${key}`;+}