diff --git a/.env b/.env index 4053bfe3..fc9f6d6b 100644 --- a/.env +++ b/.env @@ -43,3 +43,6 @@ VITE_STATIC_SUPER_ROLE=R_SUPER # sourcemap VITE_SOURCE_MAP=N + +# Used to differentiate storage across different domains +VITE_STORAGE_PREFIX=SOY_ diff --git a/packages/utils/src/storage.ts b/packages/utils/src/storage.ts index 5dd8cdc6..850562ab 100644 --- a/packages/utils/src/storage.ts +++ b/packages/utils/src/storage.ts @@ -3,7 +3,7 @@ import localforage from 'localforage'; /** The storage type */ export type StorageType = 'local' | 'session'; -export function createStorage(type: StorageType) { +export function createStorage(type: StorageType, storagePrefix: string) { const stg = type === 'session' ? window.sessionStorage : window.localStorage; const storage = { @@ -16,7 +16,7 @@ export function createStorage(type: StorageType) { set(key: K, value: T[K]) { const json = JSON.stringify(value); - stg.setItem(key as string, json); + stg.setItem(`${storagePrefix}${key as string}`, json); }, /** * Get session @@ -24,7 +24,7 @@ export function createStorage(type: StorageType) { * @param key Session key */ get(key: K): T[K] | null { - const json = stg.getItem(key as string); + const json = stg.getItem(`${storagePrefix}${key as string}`); if (json) { let storageData: T[K] | null = null; @@ -37,12 +37,12 @@ export function createStorage(type: StorageType) { } } - stg.removeItem(key as string); + stg.removeItem(`${storagePrefix}${key as string}`); return null; }, remove(key: keyof T) { - stg.removeItem(key as string); + stg.removeItem(`${storagePrefix}${key as string}`); }, clear() { stg.clear(); diff --git a/src/typings/env.d.ts b/src/typings/env.d.ts index d151ffbc..a2a6aa14 100644 --- a/src/typings/env.d.ts +++ b/src/typings/env.d.ts @@ -101,5 +101,7 @@ declare namespace Env { * @link https://docs.iconify.design/api/providers.html */ readonly VITE_ICONIFY_URL?: string; + /** Used to differentiate storage across different domains */ + readonly VITE_STORAGE_PREFIX?: string; } } diff --git a/src/utils/storage.ts b/src/utils/storage.ts index 0cc51687..2ab44c28 100644 --- a/src/utils/storage.ts +++ b/src/utils/storage.ts @@ -1,7 +1,9 @@ import { createLocalforage, createStorage } from '@sa/utils'; -export const localStg = createStorage('local'); +const storagePrefix = import.meta.env.VITE_STORAGE_PREFIX || ''; -export const sessionStg = createStorage('session'); +export const localStg = createStorage('local', storagePrefix); + +export const sessionStg = createStorage('session', storagePrefix); export const localforage = createLocalforage('local');