-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement tab activity detection to optimize API calls #112
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Could you show me where the frequency of calls is coded? It is probably hard-coded somewhere, but we should make it an environment variable. |
@ceilican Actually, I explored the Infura stats dashboard manually, and it became evident that 10 requests were being made if the user wasn't on the window, reference is in the first picture.If it is hardcoded somewhere then I will search for it and then make it an env variable. |
@Tanya-ruby , I found the place that is causing the repeated calls. Have a look on lines 209 and 225 here (
Consider the following: With your current PR, AppProvider is calling getCoinDetails repeatedly and getCoinDetails is having to check whether it should have been called. This ounds strange, doesn't it? Intuitively, it should be the responsibility of AppProvider to check whether it should call getCoinDetails. |
@ceilican Yes, this makes sense ! I can create an useEffect hook to monitor changes in the document's visibility status. I can then modify the interval condition to |
@Tanya-ruby , the hook idea seems to make sense. Let's try it. I didn't fully understand the redundant check idea. If it is really redundant, then why is it necessary? Anyway, if you submit a PR, it will be easier for me to understand. |
The inner check provides an extra layer of safety to ensure data fetching only occurs if visibility is still favorable. |
OK. I understood now. I think we don't need this redundant check. When the behavior you described occurs, it will occur only once, and that's OK. What we really need is to prevent the recurring API calls. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very elegant now!
Overview:
This resolves #105
Previously, the application was fetching the price at regular intervals, regardless of whether the user's tab was active or not. This approach led to reaching the daily limit of the free Infura API when deployed for Sepolia, as a single user could leave a tab open, consuming the limit.
Implemented a solution that detects whether the tab is active or not. The price update logic now only executes when the tab is active, significantly reducing the number of API calls and preventing the daily limit from being reached.
This change ensures that the application remains functional and efficient under the constraints of the free Infura API, while also improving the user experience by reducing unnecessary data fetching when the application is not in use.
Detailed Description:
As shown in the images ,Previously, even when the tab was not active and the user had switched to another tab, there were 10 request methods being called every minute. This behaviour could easily lead to exceeding the API rate limit.
implementing a solution that involves detecting whether the tab is active. As a result, no requests (zero) are being made when the tab is not active.