firebase / firebase-js-sdk

Firebase Javascript SDK
https://firebase.google.com/docs/web/setup
Other
4.83k stars 890 forks source link

getDocsFromCache returning deleted documents #8304

Closed WillBishop closed 2 months ago

WillBishop commented 4 months ago

Operating System

macOS 14.2.1 (23C71)

Browser Version

Chrome Version 125.0.6422.142 and Safari Version 17.2.1 (19617.1.17.11.12)

Firebase SDK Version

10.11.1

Firebase SDK Product:

Firestore

Describe your project's tooling

Vue app with Vite

Describe the problem

I'm attempting to use the same cache strategies from iOS on the web but am running into difficulties. My method is this:

The onShapshot uses a filter to query documents with a dateModified greater than that of the last received snapshot.

When a recipe is deleted, it's ID is stored and in an attempt to clear it from the local cache, I call getDoc. Unfortunately, subsequent calls to getDocsFromCache still return these deleted documents, and it's not until I call getDocsFromServer (without a where clause), that the cache stops returning these deleted recipes.

I'd like to find a way to remove a document from a cache, as I'd expect doing a getDoc (or deleteDoc which I also tried) would do, and as it does on iOS.

Steps and code to reproduce issue

To reproduce this issue, create a project which does the following.

Creates 5 documents and calls getDocsFromServer on the collection at least once.

Afterwards only using getDocsFromCache.

Delete one of the documents using deleteDoc(), and observe that the getDocsFromCache method still returns these documents.

cherylEnkidu commented 3 months ago

Hi @WillBishop ,

Could you please tell me are you calling deleteDoc(documentRef2) without await in front of it?

WillBishop commented 3 months ago

No, I'm using await

public async deleteDoc(id: string) {
    const docRef = doc(firestore, `/mydocpath/${id}`);
    await deleteDoc(docRef);
}
cherylEnkidu commented 3 months ago

The following is the code I used to reproduce this issue and it seems to be working fine, do you mind checking my code and tell me what I miss?

  let db = initializeFirestore(app, { host: hostName , localCache: persistentLocalCache(/*settings*/{})});

  const collectionRef = createEmptyCollection(db, 'web-demo-');
  const documentRef1 = await createDocument(collectionRef, 'TestDoc', {
    foo: generateValue()
  });
  const documentRef2 = await createDocument(collectionRef, 'TestDoc', {
    foo: generateValue()
  });
  const documentRef3 = await createDocument(collectionRef, 'TestDoc', {
    foo: generateValue()
  });

  const serverResult = await getDocsFromServer(collectionRef);

  const cacheResult = await getDocsFromCache(collectionRef);

  log(`serverResult has ${String(serverResult.docs.length)} documents)`);
  log(`cacheResult has ${String(cacheResult.docs.length)} documents)`);

  await deleteDoc(documentRef2)

  const cacheResultAfterDelete = await getDocsFromCache(collectionRef);

  log(`cacheResultAfterDelete has ${String(cacheResultAfterDelete.docs.length)} cacheResultAfterDelete)`);
  });
WillBishop commented 3 months ago

That looks correct, I'll need to verify my code, but I do believe I'm basically doing the same thing.

google-oss-bot commented 3 months ago

Hey @WillBishop. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

WillBishop commented 3 months ago

This does appear to be exactly what I am doing. Are there some logs I could collect to help here?

jbalidiong commented 2 months ago

It would help a lot if you could provide a sample project that we can use and run locally to replicate the issue. This allows us to identify any discrepancies between code implementation and project configuration.

google-oss-bot commented 2 months ago

Hey @WillBishop. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot commented 2 months ago

Since there haven't been any recent updates here, I am going to close this issue.

@WillBishop if you're still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.