Comments (7)
Hi @jamesreggio , thanks for your response. But few problem I can tell you, hope these help.
- I just initialize one instance (InMemoryCache, ApolloClient, ...).
- 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.
- 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.
@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.
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.
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.
@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.
Thanks @lokey I'll check it again.
from apollo-cache-persist.
@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)
- Feature Request: Associated time that given fields/entire cache was last updated HOT 1
- PersistMapper/PersistLink doesn't support nested cache item reference
- [Discussion] Allow for individual cache items to be saved instead of current single-key implementation HOT 1
- Don't persist @client fields? HOT 3
- AsyncStorage or window.Local storage should I use on Web? live LocalStore HOT 2
- Lazy loading cache HOT 1
- "this" has been rewritten to "undefined" HOT 1
- Why was the persistLink and persistenceMapper example removed? HOT 2
- [Request] - Please update code for MMKVStorage instance creation as per their new implementation HOT 1
- Duplication of entire / partial cache object in MMKV storage HOT 7
- [question] What's the recommended way to handle render exceptions caused by stale persisted cached data? HOT 1
- [Question] Collaborative / multiplayer caching / syncing engine? HOT 2
- Calling client.clearStore() does not reset the persistence layer HOT 4
- this.fire is not used in this case
- useLazyQuery not returning the cache value when I request on click or with delay. HOT 1
- Help for adding support apollo client next js HOT 2
- The UI is not updated when mutating the cache with writeFragment
- Is it possible not to persist data to storage if it's undefined ? HOT 4
- Garbage collection isn't working with cache persistance HOT 2
- MMKVStorage: 'Loader' is deprecated
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from apollo-cache-persist.