question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting stale data from cache

See original GitHub issue

I’ve got queries and mutations working as hooks, and the mutations are even updating the cache correctly. However, the components using the updated queries get a stale result on the next render and see the update in subsequent renders.

I have a simple list-detail set of components with one route showing all items and another route showing the selected item. This sequence of operations will show stale data for the item when it’s loaded in a list but not when it’s loaded by itself:

  1. load /#/items
  2. click the fifth one to go to /#/item/5
  3. activate a mutation on the item, e.g. a Disable button
  4. the item will correctly show as “disabled” (up-to-date when single)
  5. click the nav link to go back to /#/items
  6. the fifth will still erroneously show as “enabled” (stale when in a list)
  7. click any item
  8. click the nav link to go back to /#/items again
  9. the fifth will now correctly show as “disabled” (up-to-date on second viewing, even in a list)

Something similar happens when using client.resetStore() and client.clearStore(). Forcing an update on a visible component with a query gets the cached results without hitting the server. Navigating away and back so the component with the stale query data gets destroyed and recreated finally loads the data from the server.

I’m following all the examples I’ve found, but here’s the relevant code just in case:

// List.js

export default function Items() {
    const { data, error, loading } = useQuery(...);
    ... check loading and error ...
    return <ul>
        {data.items.map(item => 
            <li><a href={`/#/item/${item.id}`}>{item.name}</a></li>
        )};
    </ul>;
}

// Detail.js

export default function Detail({ params: { match: { id } } }) {
    const mutate = useMutation(...);
    return <button onClick={mutate}>Disable</button>;
}

I’d greatly appreciate any help or pointers on how to track down this issue in case I’m doing something wrong. Everything else is working fine so I think I’ve set it all up correctly. Any thoughts?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:2
  • Comments:20

github_iconTop GitHub Comments

4reactions
G1itchercommented, Jun 13, 2019

we’re seeing the exact same thing.

Triggering mutation and moving to a new page, at which point we call useQuery to get the new data, which is out of date at that point. By going back and forward again we can see that it gets the correct data.

2reactions
bdeterlingcommented, Aug 2, 2019

I encountered this too and had to use the metadata hack to work around it. It seems like a pretty basic use case to query for something, edit it in a modal, update it, then expect the underlying page to show the updates. The Apollo cache is correct and I can’t think of a scenario where anyone would want useQuery to return something different than is in the cache. Perhaps it could run with cache-only any time skip is false and use the default fetchPolicy only when the variables change.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dealing with stale data in in-memory caches - Stack Overflow
Reading: client will first try to read from the cache. On cache miss, read from the database and put the data in the...
Read more >
How to tackle stale cached responses? - DevGenius.io
First, let's understand the different types of response that we store in cache and the possibility of it getting stale. Responses that rarely ......
Read more >
Stale-while-revalidate Data Fetching with React Hooks: A Guide
Leveraging the stale-while-revalidate HTTP Cache-Control extension is a popular technique. It involves using cached (stale) assets if they are found in the ...
Read more >
8 Understanding Caching - Oracle Help Center
About Handling Stale Data. About Explicit Query Refreshes. About Cache Indexes. Database Event Notification and Oracle CQN. About Query Results Cache.
Read more >
What is stale cache? - Quora
Sometimes a proxy server, site host or other Web handler may deliver dynamic data that is stored locally on its server instead of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found