databaseclientnuxt3.js

How to Implement Efficient Data Fetching between Client, API, and Database?


Recently, a client asked me to add a chart to the dashboard of one of their systems. The problem is that the dashboard was already huge, with several charts and tables, and it took forever to load. Everything was loaded before the page was rendered to the client, so the data appeared right away without any "loading" icons. This ended up being terrible for those who didn’t want to see that screen and wanted to do other tasks in the system.

When I first saw it, I thought it was absurd, but I didn’t know what to do to improve it.

After thinking more about it, I came to some conclusions about how to fetch data, but I still have some doubts:

  1. Data fetching in the component:
    This would work for quick data in components that aren’t a priority.
    Example: a birthday table.
    But in the example I gave, it wouldn’t work. Imagine around 12 components making requests to the API and querying the database at the same time, for each user who enters the system, right on the initial screen.

  2. Data fetching (or real-time, depending on the case) from the parent component:
    Useful when the data is the main reason for the page.
    Example: a sales dashboard, competitions, or stock market data.
    This could work since all the data would be fetched at once, but without locking the system for users who don’t need to see the dashboard information.

  3. Load the data and wait for the entire page to load:
    When the data is essential for the page to function.
    Example: forms with data from an API.
    This is what’s happening right now, but until all the data is loaded, no one can interact with the system, even those who don’t want to see the dashboard.

  4. Fetch data on the first load and store it in the state:

    Useful for small amounts of data that don’t take up much space on the client for future loads.
    Example: forms with data from an API and frequently used data like dynamic buttons in navbars.
    This doesn't fit in this case because the data needs to refresh upon update, and there is too much data to keep in the state, which would take up a lot of RAM.

My doubts are:

The system is built in Nuxt3. If there were examples, I think it would be easier to understand and learn what can be done.


Solution

  • Yes, your thoughts about data fetching and performance issues make sense, and you are on the right track. I'll break down your points and add some options, best practices, and general insights on the topic.

    1. Data Fetching in the Component (for non-priority data)

    Use Case: Quick, low-priority data. Issue: If too many components fetch data at the same time, this can overwhelm the API or block the UI. Recommendation:

    2. Fetching Data from the Parent Component (when the data is central)

    Use Case: When the main focus of the page is on showing data (e.g., a dashboard). Recommendation:

    3. Loading Data Before Page Rendering

    Use Case: Data is essential for the page to function. Issue: It leads to blocking the UI. Recommendation:

    4. Fetch Once and Store in State

    Use Case: Small amounts of data, like form options. Recommendation:

    5. Additional Techniques and Options

    6. Is This Applicable Across Frameworks?

    Yes, these techniques are mostly universal:

    The exact method of implementation varies, but the principles are consistent across most modern frameworks.

    7. Best Practices for Data Fetching (Client-to-API)

    8. Detecting Performance Issues Early

    9. Example of Performance Issues I’ve Seen

    10. Resources for Learning More

    Conclusion

    You’ve identified good strategies already, and improving performance involves careful decisions about what data to load and when. I recommend focusing on lazy loading, batching requests, and SSR to improve your dashboard performance.