Skip to content

Commit f5f600d

Browse files
committed
move json parse into storage interface
1 parent ba67693 commit f5f600d

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

packages/browser/src/client.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,25 @@ export class BrowserClient extends BaseClient<BrowserClientOptions> {
129129
}
130130

131131
return {
132-
get(key: string) {
133-
return localStorage.getItem(getKey(key));
132+
get(key: string): unknown {
133+
const value = WINDOW.localStorage.getItem(getKey(key));
134+
135+
if (value === null) {
136+
return value;
137+
}
138+
139+
try {
140+
return JSON.parse(value);
141+
} catch {
142+
return null;
143+
}
134144
},
135-
set(key: string, value: string) {
136-
return localStorage.setItem(getKey(key), value);
145+
set(key: string, value: any): void {
146+
try {
147+
WINDOW.localStorage.setItem(getKey(key), JSON.stringify(value));
148+
} catch {
149+
DEBUG_BUILD && logger.error('Unable to serialize configuration and save to localStorage');
150+
}
137151
},
138152
};
139153
}

packages/core/src/remoteconfig.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function remoteConfig({
3030
// Use cached configuration if it exists
3131
const cachedValue = storage.get(defaultConfigName);
3232
if (cachedValue) {
33-
_activeConfig = JSON.parse(cachedValue);
33+
_activeConfig = cachedValue;
3434
_source = 'CACHED';
3535
_state = 'INITIALIZED';
3636
}
@@ -67,7 +67,7 @@ export function remoteConfig({
6767
_pendingConfig = data;
6868
_hasUpdate = true;
6969

70-
storage.set(defaultConfigName, JSON.stringify(data));
70+
storage.set(defaultConfigName, data);
7171
storage.set(`${defaultConfigName}_lastFetch`, +_lastFetch);
7272
// _state = "SUCCESS";
7373
resolve();

0 commit comments

Comments
 (0)