Skip to content

Commit

Permalink
clear cache on initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
tyiuhc committed Oct 16, 2024
1 parent 2c72aa2 commit a367544
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions packages/experiment-tag/src/experiment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ const appliedInjections: Set<string> = new Set();
const appliedMutations: MutationController[] = [];
let previousUrl: string | undefined = undefined;
// Cache to track exposure for the current URL, should be cleared on URL change
let urlExposureCache: { [url: string]: { [key: string]: string | undefined } } =
{};
let urlExposureCache: { [url: string]: { [key: string]: string | undefined } };

export const initializeExperiment = (apiKey: string, initialFlags: string) => {
// Clear the cache on initialization
urlExposureCache = {};
const globalScope = getGlobalScope();
if (globalScope?.webExperiment) {
return;
Expand Down Expand Up @@ -152,7 +153,6 @@ const applyVariants = (variants: Variants | undefined) => {

if (payloadIsArray) {
for (const action of variant.payload) {
exposureWithDedupe(key, variant);
if (action.action === 'redirect') {
handleRedirect(action, key, variant);
} else if (action.action === 'mutate') {
Expand Down Expand Up @@ -186,6 +186,7 @@ const handleRedirect = (action, key: string, variant: Variant) => {
globalScope.location.href,
redirectUrl,
);
exposureWithDedupe(key, variant);
// perform redirection
globalScope.location.replace(targetUrl);
};
Expand All @@ -199,6 +200,7 @@ const handleMutate = (action, key: string, variant: Variant) => {
mutations.forEach((m) => {
appliedMutations.push(mutate.declarative(m));
});
exposureWithDedupe(key, variant);
};

const revertMutations = () => {
Expand Down Expand Up @@ -277,6 +279,7 @@ const handleInject = (action, key: string, variant: Variant) => {
appliedInjections.delete(id);
},
});
exposureWithDedupe(key, variant);
};

export const setUrlChangeListener = () => {
Expand Down

0 comments on commit a367544

Please sign in to comment.