-
Notifications
You must be signed in to change notification settings - Fork 36
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
Cache handling error with cache-and-network policy in production #366
Comments
Hey @romain-hasseveldt 👋 Without seeing your setup and assuming you're using the app router, I'll provide you with a few things to keep in mind. The following is assuming you're running this in a client component, which is first rendered on the server in an SSR pass. React determines when to keep the stream open based on Suspense boundaries, which are triggered by pending promises. Once the promises resolve and the suspense fallback is no longer shown, there is no guarantee that React will keep the stream open to provide additional updates from the server to the browser. With a Because you're using a So in summary, you might be streaming an empty array from a server, then the browser executes the network request to fulfill the fetch policy. The result of the network request is what gives you the set of posts. This is pure speculation though since I don't have any code to go off of or understand your setup. I'll challenge the idea though that Let me know if this helps! |
Thank you for your thorough response, @jerelmiller, especially considering my issue, which I wrote quickly because it was late in France! To give you more context: I am indeed using the app router, and my API is served by Strapi. Here are the components in question: First, the forum page:
Second, the
I switched to a
As such, when I filter by keywords, by my posts, or by my commented posts, everything feeds into the same cache. This results, for example, in filtering by my posts, leaving the page, and returning to it, and Apollo hits the cache and always returns my posts, whereas I would prefer it to return all posts. I hope this clarifies things. Maybe I shouldn't be using Thanks again! |
@romain-hasseveldt that certainly helps! Can I ask you to clarify this part for me?
I think I'm missing what the difference is between "posts" and "all posts" here. Can you perhaps give me a quick example of data you're expecting to see vs the data you getting back from the cache?
Keep in mind that Suspense is what allows you to start the fetch on the server and stream the results to the browser. We disable fetches in
This is certainly possible, but I'd like to understand that first point a bit first. More than likely a combination of That said, I do think there is opportunity for some additional |
To give you an example @jerelmiller I land on a page that lists all posts in a forum. All posts (within the pagination limit) are displayed. Does that make it clearer? |
@romain-hasseveldt yes that helps a lot! What I suspect is happening is that because your If you're set on storing everything in the same list, you're going to need to do that filtering yourself client-side with a You can perform your own client-side filtering in a If you haven't already, I'd highly recommend taking a look at the pagination core API docs which give a great overview of how the read and merge functions work together to return the result you're looking for. This has some great examples on how these work together. That said, one big downside here is knowing whether or not your merged set of results contains the full result set from the server. The solution above can only perform client-side filtering. Unless you have a way to determine whether you've fetched the entire list of results from the server, your For this reason, I'd still encourage you to consider adding those filters as Does this help? |
Very helpful @jerelmiller , thank you for these precise answers. I was testing at the same time passing The problem, correct me if I'm wrong, is that this way I will create as many references in the cache as there are filters. Considering I have a filter on keywords, I will end up with a cache ref for 'm', 'my', 'mys', 'mysu', 'mysup', 'mysupe', 'mysuper', 'mysuperf', 'mysuperfi', 'mysuperfil', 'mysuperfilt', 'mysuperfilte', 'mysuperfilter'... Plus, when I'm creating a new post, should I be rewriting or refetching queries with all combinations of arguments? That's the reason why I wanted to use |
I am encountering a strange issue with Apollo Client when using the
cache-and-network
fetch policy.When utilizing
useSuspenseQuery
with keywords filters to retrieve posts, the query sometimes returns an empty array. However, despite this empty array, the frontend continues to display posts that should not be present.What's strange is that this behavior occurs only in production or when querying the production backend from my local frontend. In my local environment, everything functions as expected without this issue.
I'm looking for insights into why this cache handling error might be happening and how to address it.
The text was updated successfully, but these errors were encountered: