Skip to content

Commit

Permalink
fix: Handle localStorage errors from iframe (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
tyiuhc authored Nov 15, 2023
1 parent aefd56b commit 159947e
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions packages/experiment-browser/src/experimentClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,12 @@ export class ExperimentClient implements Client {
storage,
);
this.flags = getFlagStorage(this.apiKey, this.config.instanceName, storage);
this.flags.load();
this.variants.load();
try {
this.flags.load();
this.variants.load();
} catch (e) {
console.warn('Failed to load flags and variants from localStorage', e);
}
}

/**
Expand Down Expand Up @@ -322,7 +326,11 @@ export class ExperimentClient implements Client {
*/
public clear(): void {
this.variants.clear();
void this.variants.store();
try {
void this.variants.store();
} catch (e) {
console.warn('Failed to store variants in localStorage', e);
}
}

/**
Expand Down Expand Up @@ -664,7 +672,11 @@ export class ExperimentClient implements Client {
});
this.flags.clear();
this.flags.putAll(flags);
this.flags.store();
try {
this.flags.store();
} catch (e) {
console.warn('Failed to store flags in localStorage', e);
}
}

private async storeVariants(
Expand All @@ -683,7 +695,11 @@ export class ExperimentClient implements Client {
for (const key in failedFlagKeys) {
this.variants.remove(key);
}
this.variants.store();
try {
this.variants.store();
} catch (e) {
console.warn('Failed to store variants in localStorage', e);
}
this.debug('[Experiment] Stored variants: ', variants);
}

Expand Down

0 comments on commit 159947e

Please sign in to comment.