23
loading...
This website collects cookies to deliver better user experience
GET
request when it is loaded. This adds an extra dimension to the issue. A dashboard where each card requires different data is a good example. Many solutions cannot handle such a situation and result in many refresh attempts that happen at the same time.401
HTTP error (unauthorized) from the server. This would create a hit on user experience only once within the expiration timeframe. But this hit can be significant. First, we have to do a request to the server. We get back a 401
. Now we have to refresh the tokens and execute the requests again. Three requests instead of one.queue
and an extra check, this can be solved. All requests that need to be sent out while the application is refreshing are put in the queue. Once refreshing is complete, the queue is emptied.fetchOrRefresh
request. An example implementation can be seen in the code block below.const cache = { refreshing: null, expiresOn: '2021-11-01T00:00:00.000Z' };
export default async function fetchOrRefresh(...args) {
try {
if (new Date() > new Date(cache.expiresOn)) {
if (!cache.refreshing) cache.refreshing = refresh();
await cache.refreshing;
cache.refreshing = null;
}
return await fetch(...args);
} catch (e) {
console.log(e);
}
}
axios
and redux
.