I use AngularFire
(6.1.4) in my Angular (11.1.1) project with offline persistence and I was surprised to see that on load, the data is provided twice: once from the cache and once from the server (I use the variable fromCache
to proof the source).
app.module.ts
import { AngularFirestoreModule } from '@angular/fire/firestore';
import { AngularFireModule } from '@angular/fire';
import { AngularFireAuthModule } from '@angular/fire/auth';
@NgModule({
imports: [
AngularFireModule.initializeApp(Config),
AngularFirestoreModule.enablePersistence(),
AngularFireAuthModule
]
})
export class AppModule { ...}
DB Service
this.firestoreRef.collection(collectionName, query)
.snapshotChanges()
.pipe(
map(actions => actions.map(a => {
console.log('fromCache -> ', a.payload.doc.metadata.fromCache);
}))
).subscribe(/* Just for TEST purposes */);
I was thinking the same logic for the get()
options would apply:
Setting to default (or not setting at all), causes Firestore to try to retrieve an up-to-date (server-retrieved) snapshot, but fall back to returning cached data if the server can't be reached.
Why also the cached data is returned if the user is online?
Of course I can listen offline/online events (though they are not reliable) or even a SW to check if the client is online or not, and use or skip the cached results accordingly, but shouldn't Firestore detect online/offline state?