Stale data in the with-Apollo example setup using getServerSideProps #25080
Comments
|
I would like to get started contributing to open source NextJS is my preferable ReactJS framework can I try this issue? How to check if the task is already taken? |
Do you mean to change the apollo query in the getServerSideProps?
Does refreshing the page means hot load refresh? If you refreshing the page proactively, it will always execute |
I mean to update that entity from the client side mutation. But the query that ran in the getServerSideProps doesn't know anything about the update that happened from client side.
It's just browser refresh and not hot load refresh. Which makes a call to the server and runs the getServerSideProps func. But at this point the earlier request to the page had executed the query and the response is stored in the cache. Even though we have updated the entity from the client mutation. This query would still return the cached response. If it is in hot load refresh(dev mode) it would be fine. But this issue occurs when you are running it in production build. Hope that info helps! I have currently updated it evict the entity from the cache on every request so that if fetches info every time. This isn't an ideal solution in my opinion. |
This comment was marked as spam.
This comment was marked as spam.
What I did are:
after that, I got the latest value from |
I think the apollo client is caching your query response but it is probably fetching new items since the query you are making doesn't really have an identifier to it. It is more prominent if you were actually querying a single item instead. For eg: Try something like this, you'd be able to reproduce it. You might know but just as an FYI the Hope this helps and thank you for taking time to reproduce this. |
|
How did you solve the same problem that keeps bothering me? The default policy of Apollo Client should be cache first. It will query data from the cache first. If your data has been updated but the ID in the cache is the same, it will not fetch data from the server again The first solution is: The second solution is: I tried to change the policy to cache-and-network in getServersideProps, but Apollo gave an error and could only use cache-first or network-only How can we solve this problem so that we can get the latest data and make the SSR effective? In addition, I tried the method of clearing the cache after changing the data, but it didn't work, like this When I tried to clean it up, I ran this function, and strangely it didn't work,It's not the same origin policy issue, I'm doing it on the same domain name |
|
My code looks like this The issues with this implementation is that it will get the fresh data every single time the request is triggered to this page. You will have SSR but the item is cleared from the cache and added to the cache though it won't affect other items in the cache. I wasn't able to come up with a better solution for this as of now. Hope that helps. |
|
@ravipula Thank you for the code example. Have you ever thought about using subscriptions for this purpose |
I am actually not all that familiar with subscriptions and haven't thought about using since the issue is somewhat resolved for now. I'm not how that will fit into this setup though with all the inclusions of websockets and the apolloclient being on the server itself in this instance. Have to think in through. |
What example does this report relate to?
with-Apollo
What version of Next.js are you using?
10.0.0
What version of Node.js are you using?
10.14.2
What browser are you using?
chrome
What operating system are you using?
macOS
How are you deploying your application?
Other platform
Describe the Bug
The Apollo client query response is stale when used in getServerSideProps. I think the issue is caused because of the usage of the line below this comment reference doesn't create a new client instead uses the existing one.
next.js/examples/with-apollo/lib/apolloClient.js
Line 53 in 288984b
if (typeof window === 'undefined') return _apolloClientExpected Behavior
Should create a new instance of the Apollo Client
To Reproduce
As in the above code block the initializeApollo function is not creating a new instance of Apollo Client instead using the existing client. Which is why stale data is being return until a rebuild is done.
If this is expected behavior, How would i be able to update the Apollo Cache on server side when the client updates an item? OR do i need to clear the cache when i make a new query in getServerSideProps?
Thank you! appreciate any help.
The text was updated successfully, but these errors were encountered: