Skip to content

feat(runtime): cache queries with useSWR #828

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

Closed
wants to merge 7 commits into from
Closed

feat(runtime): cache queries with useSWR #828

wants to merge 7 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Apr 27, 2021

Implements client side caching with useswr (https://swr.vercel.app). See https://jira.dhis2.org/browse/LIBS-166 for the related epic.

Notes:

  • Some types need to be fixed, ignored some of them for the moment
  • Currently not cancelling requests. useSWR doesn't have an explicit API for this yet: Cancel requests vercel/swr#129, though there are possibilities apparently
  • We're not using the arguments passed by swr to the queryFn (engine.query in our case) at the moment. Though it should be possible to use the arguments that swr passes us.
  • Refetch does not return a promise currently, so this is incompatible with how our refetch used to work. We'll need to see if we want to either drop this behaviour, or implement it with useSWR with the new refetch.
  • onComplete and onError will fire for each request. Even when swr is serving cached data. Could be problematic. It might mess with app logic when swr returns (stale) data and an error callback fires.
  • SWR also supplies onSuccess and onError hooks to pass a callback to. We could use those as well.
  • The cache key for a group of queries could change if a single query of the group of queries changes, even though it should only affect the cache key of the changed query. This means that currently caching isn't as efficient with multiple queries for one hook. There doesn't seem to be anything native to swr that allows multiple queries easily like react-query's useQueries.
  • Unlike react-query, useSWR uses referential equality checks to check whether the cache key has changed.

Tests:

  • onComplete, onError callbacks
  • variables (initial)
  • multiple queries
  • refetch (for lazy and non-lazy)

API docs:

Follow-up (unrelated to swr):

  • Remove waitForElement
  • Address act warnings (everything in renderHook should already be wrapped in act)

@ghost ghost changed the title feat(query): add query caching with useSWR feat(runtime): cache queries with useSWR Apr 27, 2021
@ghost ghost force-pushed the use-swr branch from 65625c9 to 1bb09d7 Compare April 27, 2021 14:53
@ghost ghost force-pushed the use-swr branch from 1bb09d7 to 60af4c9 Compare April 27, 2021 15:15
@ghost
Copy link
Author

ghost commented Jun 24, 2021

Closed in favor of #824

@ghost ghost closed this Jun 24, 2021
@ghost ghost deleted the use-swr branch June 24, 2021 14:05
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants