Skip to content

Commit

Permalink
Mute no_server_response error when back navigation is detected (#7342)
Browse files Browse the repository at this point in the history
- Mute no_server_response error when back navigation is detected. This
change addresses an issue with `handleRedirectPromise` that instruments
"no_server_response" error code when user navigates back to the app page
from account picker UX.
  • Loading branch information
konstantin-msft authored Sep 27, 2024
1 parent fafaada commit 471ea55
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "Mute no_server_response error when back navigation is detected #7342",
"packageName": "@azure/msal-browser",
"email": "kshabelko@microsoft.com",
"dependentChangeType": "patch"
}
26 changes: 25 additions & 1 deletion lib/msal-browser/src/interaction_client/RedirectClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ import { EventError } from "../event/EventMessage.js";
import { AuthenticationResult } from "../response/AuthenticationResult.js";
import * as ResponseHandler from "../response/ResponseHandler.js";

function getNavigationType(): NavigationTimingType | undefined {
if (
typeof window === "undefined" ||
typeof window.performance === "undefined" ||
typeof window.performance.getEntriesByType !== "function"
) {
return undefined;
}

const navigationEntries = window.performance.getEntriesByType("navigation");
const navigation = navigationEntries.length
? (navigationEntries[0] as PerformanceNavigationTiming)
: undefined;
return navigation?.type;
}

export class RedirectClient extends StandardInteractionClient {
protected nativeStorage: BrowserCacheManager;

Expand Down Expand Up @@ -223,7 +239,15 @@ export class RedirectClient extends StandardInteractionClient {
this.browserStorage.cleanRequestByInteractionType(
InteractionType.Redirect
);
parentMeasurement.event.errorCode = "no_server_response";

// Do not instrument "no_server_response" if user clicked back button
if (getNavigationType() !== "back_forward") {
parentMeasurement.event.errorCode = "no_server_response";
} else {
this.logger.verbose(
"Back navigation event detected. Muting no_server_response error"
);
}
return null;
}

Expand Down
44 changes: 44 additions & 0 deletions lib/msal-browser/test/interaction_client/RedirectClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1827,6 +1827,50 @@ describe("RedirectClient", () => {
});
redirectClient.handleRedirectPromise("", rootMeasurement);
});

it("mutes no_server_response error when back navigation is detected", async () => {
// @ts-ignore
window.performance.getEntriesByType = () => {
return [{ type: "back_forward" }];
};

browserStorage.setInteractionInProgress(true);
const loginRequestUrl = window.location.href;
window.location.hash = "";
window.sessionStorage.setItem(
`${Constants.CACHE_PREFIX}.${TEST_CONFIG.MSAL_CLIENT_ID}.${TemporaryCacheKeys.ORIGIN_URI}`,
loginRequestUrl
);
const res = await redirectClient.handleRedirectPromise(
"",
rootMeasurement
);
expect(res).toBeNull();
expect(rootMeasurement.event.errorCode).toBeUndefined();
});

it("does not mute no_server_response error when back navigation is not detected", async () => {
// @ts-ignore
window.performance.getEntriesByType = () => {
return [];
};

browserStorage.setInteractionInProgress(true);
const loginRequestUrl = window.location.href;
window.location.hash = "";
window.sessionStorage.setItem(
`${Constants.CACHE_PREFIX}.${TEST_CONFIG.MSAL_CLIENT_ID}.${TemporaryCacheKeys.ORIGIN_URI}`,
loginRequestUrl
);
const res = await redirectClient.handleRedirectPromise(
"",
rootMeasurement
);
expect(res).toBeNull();
expect(rootMeasurement.event.errorCode).toEqual(
"no_server_response"
);
});
});

describe("acquireToken", () => {
Expand Down

0 comments on commit 471ea55

Please sign in to comment.