Reputation: 23
I'm new to Firebase and I found getDocsFromCache. I heard that firebase updated it's version to version 9, (modular) and i have to use more than just query.get({source: "cache"})
. But getDocsFromCache
didn't work for me. Every time i call getDocsFromCache
, it does not throw any errors but snapshot.empty
is always true so i can't access to my documents(collection).
If i have to cache manually, how? If not, what am i missing?
Thank you.
import {
collection,
getDocs,
getDocsFromCache,
query,
} from 'firebase/firestore';
import { db } from '../firebase-config';
export const getReviews = async () => {
const q = query(collection(db, 'review'));
try {
const snapshot = await getDocsFromCache(q);
console.log(snapshot.empty); // always true but not throwing any error
snapshot.forEach((doc) => {
/* ... */
});
} catch (e) {
// never reach here
const snapshot = await getDocs(q);
/* ... */
}
};
Upvotes: 2
Views: 2262
Reputation: 31
I had the same issue, This doc resolved my problem. you can try this one as a example.
const db = initializeFirestore(app, {
localCache: persistentLocalCache({
cacheSizeBytes: 200000000,
useFetchStreams: true,
tabManager: persistentMultipleTabManager(),
}),
});
Upvotes: 1
Reputation: 2309
enableIndexedDbPersistence
is deprecated (at least in firebase v9). Consider using the following approach (I took it here):
import { initializeFirestore, memoryLocalCache } from "firebase/firestore";
initializeFirestore(app, {
localCache: memoryLocalCache(),
});
you may also import and use persistentLocalCache
instead of memoryLocalCache
if you like.
Upvotes: 4
Reputation: 598765
From the documentation on configuring offline persistence:
For the web, offline persistence is disabled by default. To enable persistence, call the
enablePersistence
method.
So make sure to enable the cache by calling this right after initializing Firebase and Firestore:
import { enableIndexedDbPersistence } from "firebase/firestore";
enableIndexedDbPersistence(db);
Upvotes: 3