ConsoleTVs / sswr

🔥 Svelte stale while revalidate (SWR) data fetching strategy
MIT License
234 stars 11 forks source link

Expose a method to get the cache? #20

Closed frederikhors closed 2 years ago

frederikhors commented 3 years ago

In the classic example of list/detail if I click on the single detail element I wanna check the cache (under list key) to get data for initialData but now there is no way to get cache right?

Should we expose methods for get the cache?

frederikhors commented 3 years ago

I would like a method like: getKeyStartsWith(key: string) to find for example keys like: /players?page=1.

And then I can search values for the playerId I need in detail page.

What do you think?

ConsoleTVs commented 3 years ago

You've got:

https://github.com/ConsoleTVs/sswr/blob/master/src/index.ts#L105

and

https://github.com/ConsoleTVs/sswr/blob/master/src/index.ts#L115

frederikhors commented 3 years ago

It works. But.

As I said I'm using it for a "classic" list/detail and here what happens:

  1. I'm on the /players list page
  2. I have fetched data with /players key
  3. I move on the /players/1 detail page
  4. On detail page I use get("/players") to populate my initialData to speed up UX
  5. BUT during this navigation from /players to /players/1 the key /players is refetched in background hence the get("/players") does not return data, only a Promise, useless in this case

But if I need a Promise I can use getOrWait() instead.

So I think we should not "change" the cache during refetching because we need to get() something even if is stale.

What do you think?

frederikhors commented 3 years ago

I think we need a method like getStartingWith(key: string).

ConsoleTVs commented 3 years ago

Why not just use the mutate to populate other keys or just use useSWR with revalidation set to false? There is a bunch of options you can pass to it!

frederikhors commented 3 years ago

Why not just use the mutate to populate other keys

I cannot, because I'm checking for data already in cache during new fetch...

or just use useSWR with revalidation set to false

The players list is refetching because I'm using a variable, and it can depends on other usecases.

Cannot be this the solution.

benbender commented 3 years ago

What you are trying to achieve is probably not possible.

  1. You are fetching the endpoint for /players and retrieve one(!) object (most likely parsed from a json-string)
  2. You are navigating to /players/1 and want the cache to know which part of the data in the cache belongs to player with id 1. Problem is that this information isn't retrieved by the cache.

To achieve such behaviour you could either: a) parse your one object on the first fetch and populate the cache with individual objects b) use graphql which has such features because of fragments and strong typing on both ends (f.e. with urql's graphcache).

But there is no way to get this for free. You have to type and/or parse your data at some point.

frederikhors commented 3 years ago

It's possible.

I'm getting the players object which is like this:

{ players: [{id: "1", name: "Name"}, ...and others] }

and with a .find() I can find my player/1.

frederikhors commented 3 years ago

It's doable right now with svelte-query.

ConsoleTVs commented 3 years ago
{ players: [{id: "1", name: "Name"}, ...and others] }

Just find the player with id = 1 and put it in the cache:

mutate('player/1', {id: "1", name: "Name"})
frederikhors commented 3 years ago

Just find the player with id = 1

This is what I'm asking you!

If I use get() and the query is re-fetching I cannot find players (already fetched before) because I get a promise!

frederikhors commented 3 years ago

mutate or initialData is not a problem!

ConsoleTVs commented 3 years ago

You have to change step 4 from your list.

frederikhors commented 3 years ago

I populate the cache /players/${id} for each fetched player using mutate()

But I don't know if a use will need this.

I'm opening a new page and in background the previous list is refetching (for many reasons).

This is not a valid solution.

ConsoleTVs commented 3 years ago

How would this be done in https://swr.vercel.app otherwise?

frederikhors commented 3 years ago

I don't know, But it's doable in this project, the problem is we need to swap the data for that key only when the promise is resolved, not before.

IMO.

benbender commented 3 years ago

IMHO: a) your sentences make no sense, are incomplete, plain false or simply not understandable. b) no github-repo is your personal support-forum c) "This is what I'm asking you!"? Are you nuts or simply rude? @ConsoleTVs provides a lib, developed in his freetime, free of charge, he even provides support and you dare to demand anything? Be grateful and nothing else. Period. d) I'm out. I'll no longer answer questions or similar by you.

frederikhors commented 3 years ago

@benbender I love you. 😄

benbender commented 3 years ago

My verdict has fallen: you are plainly arrogant. For no obvious reason. #kthxbye #block

frederikhors commented 3 years ago

I really don't understand your behavior, @benbender.

Did you block me simply because I told you I love you? To avoid replying and wasting your time reading a reply to a useless message you wrote.

A message that doesn't make sense: I wasn't "demanding" anything from @ConsoleTVs; and among other things, as you well know and as @ConsoleTVs can tell, I am helping and collaborating with various PR on this splendid project.

You did not understand my question and you get angry, it seems like childish behavior to me.

Grow, live and let live.

Lots of little kisses.