Auth0 Home Blog Docs

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 https://auth0.com/docs/quickstart/spa/react/02-calling-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 https://github.com/auth0/auth0-spa-js/blob/2ace866d33cefc8140dceae7437e2a9e61469564/src/Auth0Client.ts#L314-L347) 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