Skip to content

Commit

Permalink
Fixing window unavailable bugs for PCA and PBA (#7355)
Browse files Browse the repository at this point in the history
Currently, the PCA constructor in 1P and some functions for PCA (both 1P
and 3P) and PBA throw when the window is unavailable. This creates
problems with server-side rendering. Additionally, some functions that
should throw a non_browser_environment error when the window is
unavailable (such as the acquireToken functions) throw a different
error.

This PR makes changes to prevent the PCA and PBA constructor and certain
functions from throwing solely due to the window being unavailable and
to ensure that the non_browser_environment error is thrown when
appropriate. It also adds corresponding unit tests.

---------

Co-authored-by: Hector Morales <[email protected]>
  • Loading branch information
shylasummers and hectormmg authored Oct 7, 2024
1 parent f85b567 commit 243b68b
Show file tree
Hide file tree
Showing 3 changed files with 342 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Bug fixes for windowless PCA",
"packageName": "@azure/msal-browser",
"email": "[email protected]",
"dependentChangeType": "patch"
}
13 changes: 12 additions & 1 deletion lib/msal-browser/src/controllers/StandardController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,13 @@ export class StandardController implements IController {
return;
}

if (!this.isBrowserEnvironment) {
this.logger.info("in non-browser environment, exiting early.");
this.initialized = true;
this.eventHandler.emitEvent(EventType.INITIALIZE_END);
return;
}

const initCorrelationId =
request?.correlationId || this.getRequestCorrelationId();
const allowNativeBroker = this.config.system.allowNativeBroker;
Expand Down Expand Up @@ -354,7 +361,6 @@ export class StandardController implements IController {

this.initialized = true;
this.eventHandler.emitEvent(EventType.INITIALIZE_END);

initMeasurement.end({ allowNativeBroker, success: true });
}

Expand Down Expand Up @@ -1336,6 +1342,10 @@ export class StandardController implements IController {
* @param logoutRequest
*/
async clearCache(logoutRequest?: ClearCacheRequest): Promise<void> {
if (!this.isBrowserEnvironment) {
this.logger.info("in non-browser environment, returning early.");
return;
}
const correlationId = this.getRequestCorrelationId(logoutRequest);
const cacheClient = this.createSilentCacheClient(correlationId);
return cacheClient.logout(logoutRequest);
Expand Down Expand Up @@ -1716,6 +1726,7 @@ export class StandardController implements IController {
* @returns {string}
*/
addPerformanceCallback(callback: PerformanceCallbackFunction): string {
BrowserUtils.blockNonBrowserEnvironment();
return this.performanceClient.addPerformanceCallback(callback);
}

Expand Down
Loading

0 comments on commit 243b68b

Please sign in to comment.