Skip to content

Commit 1fc34cc

Browse files
committed
feat(projects): Add prefix to local storage
1 parent c347528 commit 1fc34cc

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed

.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,6 @@ VITE_STATIC_SUPER_ROLE=R_SUPER
4343

4444
# sourcemap
4545
VITE_SOURCE_MAP=N
46+
47+
# Used to differentiate storage across different domains
48+
VITE_STORAGE_PREFIX=SOY_

packages/utils/src/storage.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import localforage from 'localforage';
33
/** The storage type */
44
export type StorageType = 'local' | 'session';
55

6-
export function createStorage<T extends object>(type: StorageType) {
6+
export function createStorage<T extends object>(type: StorageType, storagePrefix: string) {
77
const stg = type === 'session' ? window.sessionStorage : window.localStorage;
88

99
const storage = {
@@ -16,15 +16,15 @@ export function createStorage<T extends object>(type: StorageType) {
1616
set<K extends keyof T>(key: K, value: T[K]) {
1717
const json = JSON.stringify(value);
1818

19-
stg.setItem(key as string, json);
19+
stg.setItem(`${storagePrefix}${key as string}`, json);
2020
},
2121
/**
2222
* Get session
2323
*
2424
* @param key Session key
2525
*/
2626
get<K extends keyof T>(key: K): T[K] | null {
27-
const json = stg.getItem(key as string);
27+
const json = stg.getItem(`${storagePrefix}${key as string}`);
2828
if (json) {
2929
let storageData: T[K] | null = null;
3030

@@ -37,12 +37,12 @@ export function createStorage<T extends object>(type: StorageType) {
3737
}
3838
}
3939

40-
stg.removeItem(key as string);
40+
stg.removeItem(`${storagePrefix}${key as string}`);
4141

4242
return null;
4343
},
4444
remove(key: keyof T) {
45-
stg.removeItem(key as string);
45+
stg.removeItem(`${storagePrefix}${key as string}`);
4646
},
4747
clear() {
4848
stg.clear();

src/typings/env.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,7 @@ declare namespace Env {
101101
* @link https://docs.iconify.design/api/providers.html
102102
*/
103103
readonly VITE_ICONIFY_URL?: string;
104+
/** Used to differentiate storage across different domains */
105+
readonly VITE_STORAGE_PREFIX?: string;
104106
}
105107
}

src/utils/storage.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { createLocalforage, createStorage } from '@sa/utils';
22

3-
export const localStg = createStorage<StorageType.Local>('local');
3+
const storagePrefix = import.meta.env.VITE_STORAGE_PREFIX || '';
44

5-
export const sessionStg = createStorage<StorageType.Session>('session');
5+
export const localStg = createStorage<StorageType.Local>('local', storagePrefix);
6+
7+
export const sessionStg = createStorage<StorageType.Session>('session', storagePrefix);
68

79
export const localforage = createLocalforage<StorageType.Local>('local');

0 commit comments

Comments
 (0)