getTokenSilently() is slower than expected

Hi,

I use the @auth0/auth0-spa-js library version 1.6.2 in my React app. I followed this tutorial Auth0 React SDK Quickstarts: Call an API and the authentication flow works fine.
However there’s a thing which bugs me a little bit. During data fetching I follow the pattern mentioned in the tutorial:

  const token = await getTokenSilently();

  const response = await fetch("/api/external", {
    headers: {
      Authorization: `Bearer ${token}`
    }
  });

I get the token as expected without a network request in normal cases, however it takes longer than I expected. I checked it with various tools and it usually takes about 50ms which seems a too long to me. I did this test on a 2018 MacBookPro, I guess it might take longer on weaker devices.

Do you know any workaround?

Regards,
Laszlo

1 Like

Hi @lkiss

The code for getTokenSilently is fairly simple when there’s a cache hit (code at auth0-spa-js/Auth0Client.ts at 2ace866d33cefc8140dceae7437e2a9e61469564 · auth0/auth0-spa-js · GitHub) but there’s a usage of browser-tabs-lock to ensure multiple tabs play nicely together. This could be getting in the way of optimal performance.
If you have written tests or have other supporting information on this, I suggest starting a thread at the auth0-spa-js repository issues section, so that the team can look into this.

1 Like