question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Performance issues after msal-react update caused by a lot of hook changes

See original GitHub issue

Core Library

MSAL.js v2 (@azure/msal-browser)

Core Library Version

2.22.1

Wrapper Library

MSAL React (@azure/msal-react)

Wrapper Library Version

1.3.1

Description

Since upgrading from @azure/msal-browser v2.22.0 to v2.22.1 and from @azure/msal-react v1.3.0 to v1.3.1 my React App is terribly slow on each navigation. With some investigation (using why-did-you-render) I saw that a top-level component does a lot of re-renderings because the useIsAuthenticated-hook changes several times (I counted 45 times). The actual value of the hook does not change (only the ref of the object stored in the MsalContext changes). The same is true for other subcomponents that are using useMsal. Before updating those packages each of those hooks only changed one time per navigation (I should mention that I am using react-router(-dom) for routing).

Here the diff output reported by why-did-you-render (as mentioned such a ref change without value change happens several times): image

Error Message

No response

Msal Logs

No response

MSAL Configuration

{
  auth: {
    authority: `https://login.microsoftonline.com/${TENANT_ID}`,
    clientId: CLIENT_ID,
    navigateToLoginRequestUrl: false,
    postLogoutRedirectUri: '/auth/logout',
    redirectUri: '/',
  },
  cache: {
    cacheLocation: 'localStorage',
  },
};

Relevant Code Snippets

export function Pages() {
  const isAuthenticated = useIsAuthenticated();

  return (
    <Routes>
      <Route element={<LogoutPage />} path="auth/logout" />
      <Route element={<LoginPage />} path="auth" />
      <Route element={<LandingPage />} path="welcome" />
      <Route element={isAuthenticated ? <AuthorizedPagesWithSubRoutes /> : <LandingPage />} path="/*" />
    </Routes>
  );
}

Reproduction Steps

  1. Navigate on a page that uses a @azure/msal-react-hook

Expected Behavior

The hooks of @azure/msal-react should not change so many times.

Identity Provider

Azure AD / MSA

Browsers Affected (Select all that apply)

Chrome, Edge

Regression

@azure/msal-browser 2.22.0 @azure/msal-react 1.3.0

Source

External (Customer)

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jasonnuttercommented, Apr 12, 2022

@matzehecht Thanks, we’ll investigate and follow up if we need more information.

0reactions
matzehechtcommented, Apr 16, 2022

Thanks for the quick response. I am happy to test this as soon as the fix is released.

Read more comments on GitHub >

github_iconTop Results From Across the Web

microsoft-authentication-library-for-js/performance.md at dev
Currently there is one scenario where msal. js will navigate from one page in your application to another.
Read more >
Why custom react hooks could destroy your app performance
Every state change in a hook, whether it affects its return value or not, will cause the “host” component to re-render.
Read more >
Best practices for working with Microsoft Graph
Apply these best practices to improve your Microsoft Graph application's performance and make your app more reliable for end users.
Read more >
Understanding common frustrations with React Hooks
React Hooks can be frustrating despite their popularity and widespread use. Learn about some of the drawbacks to using React Hooks.
Read more >
Custom hook cause poor performance, although optimizing ...
After some further reading, I encountered the following FAQ section in the official React docs: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found