Lostmyname / web-playbook

Create idiofantastic web apps
1 stars 0 forks source link

Play: declare data requirements at the component level #16

Closed djgrant closed 5 years ago

djgrant commented 7 years ago

Checklist

Problem

Applications which are rendered on the server as well as client require their data dependencies to be fulfilled during both server and browser execution.

Data dependencies include requests to APIs as well as translations, analytics and manifest data.

It is components that are actually dependent on data, yet our current solutions define the requirements at the route/controller level.

This impacts both developers and end users because defining data dependencies at the route level is impossible to maintain and cannot be optimised.

Solution

The ideal solution:

  1. Ensures components get the data they need
  2. Avoids fetching more data than necessary
  3. Avoids fetching the same data multiple times
  4. Ensures that when components are moved or reused that their data dependencies are still fulfilled

This PR attempts to solve 1, 3 & 4. react-jobs is used to declare data fetching tasks at the component level. Requests for cacheable (non-session) data are made on the server, stored in a LRU cache and passed to the client in the HTML response.

On point 2, it is currenlty assumed that all data from API endpoints will be used. However the solution also needs to solve how to avoid passing the same data in subsequent HTML responses during server-browser rehydration cycles. A separate spike is investigating how to rehydrate client state from local storage alternatively.

The solution will includes a data access object that as well as handling caching can be configured to always pass params required by an API e.g. locale, currency, session token etc.

Play doc

Todo