getDocFromCache
import { doc, getDocFromCache } from "firebase/firestore";
const docRef = doc(db, "cities", "SF");
// Get a document, forcing the SDK to fetch from the offline cache.
try {
const doc = await getDocFromCache(docRef);
// Document was found in the cache. If no cached document exists,
// an error will be returned to the 'catch' block below.
console.log("Cached document data:", doc.data());
} catch (e) {
console.log("Error getting cached document:", e);
}
For platforms with offline support, you can set the source
option to control how a get
call uses the offline cache.
By default, a get
call will attempt to fetch the latest document snapshot from your database. On platforms with offline support, the client library will use the offline cache if the network is unavailable or if the request times out.
You can specify the source
option in a get()
call to change the default behavior. You can fetch from only the database and ignore the offline cache, or you can fetch from only the offline cache. For example:
Last updated
Was this helpful?