Request Deduplication
See original GitHub issueCore Library
MSAL.js v2 (@azure/msal-browser)
Wrapper Library
Not Applicable
Description
Hi!
I have a React project which uses @azure/msal-browser
. When we call our API, we use a custom fetcher that calls acquireTokenSilent
before the request. We use the custom fetcher through a library (SWR
) that includes fetching hooks with request deduplication etc.
So if I use the fetching hook from the library multiple places to fetch data from the same endpoint, our custom fetcher will only be called once. However, if the hook is used multiple times in the same page, to different endpoints, our custom fetcher gets called multiple times, and therefore also acquireTokenSilent
. This results in multiple network calls for configurations and tokens, and often so many that the browser limits other requests.
(I know i can skip the metadata retrieval by manually providing it, but it still happens for tokens)
I might be implementing the whole thing wrong, and should decouple the token request from the api request in my code. But it would be nice if there was some kind of request deduplication functionality in the package.
Source
External (Customer)
Issue Analytics
- State:
- Created a year ago
- Reactions:4
- Comments:5 (3 by maintainers)
Top GitHub Comments
@h3rmanj Thanks. What you observe is currently expected behavior. When you login, the login call will initiate the first call to teh metadata endpoints, which gets cached and reused for the
acquireTokenSilent
calls. When you are already logged in (after refreshing), the login call doesn’t happen again, and since the twoacquireTokenSilent
calls happen in parallel, they’ll both make a metadata network call.As mentioned, we have started working on improvements to how we fetch metadata, which I think will address the concerns here (i.e. by not making the network calls at all in most scenarios). You can follow #4536 to see our progress there.
The metadata call should only be made once per page load per
PublicClientApplication
instance. Can you make sure you are not reinstantiatingPublicClientApplication
? When using React you should not instantiatePublicClientApplication
inside a React component due to re-renders potentially causing reinstantiation.