GithubHelp home page GithubHelp logo

Comments (7)

duytq94 avatar duytq94 commented on May 5, 2024 2

Hi @jamesreggio , thanks for your response. But few problem I can tell you, hope these help.

  1. I just initialize one instance (InMemoryCache, ApolloClient, ...).
  2. About option trigger: 'background', it quite odd, If I set option to 'background' and let my app to background state, it's work, the log tell that '[apollo-cache-persist]', 'Persisted cache of size 2'. But what the number 2 of size mean, 2 kilobyte, 2 byte or something else? And when I turn off network or change fetchPolicy="cache-only" to the query to test, no data can't get from the local.
  3. And with option default ('write') - can't persist data again :( .
    This is my demo project with very simple (just 1 screen to test - App/Containers/Main/Main.Screen.js).
    https://github.com/duytq94/test
    Maybe you can try to clear for help, thanks a lot.

from apollo-cache-persist.

lokey avatar lokey commented on May 5, 2024 2

@duytq94 I had a similar issue where log says 'Persisted cache of size 2'. But nothing is actually persisted. It seems to be caused by importing ApolloClient from apollo-boost. I fixed it by changing my import to
import { ApolloClient } from 'apollo-client';

from apollo-cache-persist.

jamesreggio avatar jamesreggio commented on May 5, 2024 1

Hi @duytq94 — sorry for the slow response.

I'm going to assume from storage: AsyncStorage that you're using this with a React Native app. Let me know if I'm mistaken.

The code you've written above looks correct to me. One problem I've seen before is that developers will initialize two or more instances of new InMemoryCache() (or two or more instances of new ApolloClient). When this happens, the cache persistor ends up attached to a cache that never contains any data. Double-check your code to make sure you're only initializing once. (Perhaps add a console.log statement before each of block of code you pasted above and make sure it only logs once.)

If you pass debug: true into persistCache, apollo-cache-persist should log when it goes to persist the cache. You mentioned that it logs that there's 'No stored cache to persist', but does it ever log anything like 'Persisted cache of size XXX' while your app is running? If it does, the problem may lie with AsyncStorage, and not with apollo-cache-persist.

My last thought is that you may be using trigger: 'background'. If you're using this (which can be helpful for performance on React Native — more info here), you'll need to be sure to send your app to the background to trigger the cache persistence. If you're using the iOS simulator, that means you'll need to press CMD-H to go back to the iOS home screen, and then open your app again, to trigger the cache persistence. If you just press CMD-R to reload your app without doing this, nothing will be persisted, because the app has never gone to the background.

Please let me know if any of this helped — or didn't! — and we can dig deeper.

from apollo-cache-persist.

AndreasGalster avatar AndreasGalster commented on May 5, 2024 1

Hmm ... I have the same issue of the cache not working. I've tried localStorage and sessionStorage so far. The cache actually ends up in the storage but apollo client is not using it ... so in effect the app just fails to retrieve the cached data and everything fails.

The app doesn't give me the "Persistet cache of xxxx" when it launches, though as I said it's actually available in the chosen storage method.

/edit: Sorry, my bad. After setting debug: true it tells me that it actually persisted the cache. However the app still tries to fetch from the API and then fails. So it's not actually using the data that it received / has in the store :/

Does the order in which you apply links matter? Like this:

const httpLinkWithAuthToken = ApolloLink.from([
  middlewareAuthLink,
  retryLink,
  httpLink
]);

See sample gist here: https://gist.github.com/AndreasGalster/50d1e331388288008b556618626e0420

from apollo-cache-persist.

jamesreggio avatar jamesreggio commented on May 5, 2024

@AndreasGalster, it sounds like everything is working as expected.

If you have persisted, cached data, apollo-cache-persist will restore it and your query will initially resolve with that cached data. If the query is configured to also hit the network (perhaps with fetchPolicy: 'cache-and-network) and it gets an error, the error should be passed up to your app, and the results should be cleared.

If you want to ignore errors, you can use an errorPolicy of all or ignore.

And yes, the order of Apollo Links does matter, but it seems like your links are in a reasonable ordering.

from apollo-cache-persist.

duytq94 avatar duytq94 commented on May 5, 2024

Thanks @lokey I'll check it again.

from apollo-cache-persist.

wtrocki avatar wtrocki commented on May 5, 2024

@duytq94 Since Apollo boost is out of the way this issue may no longer be relevant.
Can you confirm?

from apollo-cache-persist.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.