Skip to content

Commit

Permalink
update pushState and cache initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
tyiuhc committed Oct 17, 2024
1 parent 3096cee commit 1ad2630
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions packages/experiment-tag/src/experiment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ let previousUrl: string | undefined = 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 All @@ -42,7 +40,8 @@ export const initializeExperiment = (apiKey: string, initialFlags: string) => {
if (!isLocalStorageAvailable() || !globalScope) {
return;
}

// Clear the cache on initialization
urlExposureCache = {};
const experimentStorageName = `EXP_${apiKey.slice(0, 10)}`;
let user: ExperimentUser;
try {
Expand Down Expand Up @@ -306,13 +305,14 @@ export const setUrlChangeListener = () => {

// Wrapper for pushState
history.pushState = function (...args) {
previousUrl = globalScope.location.href;
// Call the original pushState
const result = originalPushState.apply(this, args);
// Revert mutations and apply variants after pushing state
revertMutations();
applyVariants(globalScope.webExperiment.all());

if (previousUrl && previousUrl !== globalScope.location.href) {
revertMutations();
applyVariants(globalScope.webExperiment.all());
}
previousUrl = globalScope.location.href;
return result;
};

Expand Down

0 comments on commit 1ad2630

Please sign in to comment.