firebase-js-sdk icon indicating copy to clipboard operation
firebase-js-sdk copied to clipboard

Collection snapshot queries with no documents don't store anything in cache. Always requires server snapshot.

Open InputNeuron opened this issue 4 years ago • 6 comments

[REQUIRED] Describe your environment

  • Operating System version: MacOS 12.1
  • Browser version: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36
  • Firebase SDK version: 8.10.0
  • Firebase Product: firestore

[REQUIRED] Describe the problem

If a collection snapshot query doesn't have any documents, nothing is stored in the cache.

Each time we try to wait for an empty collection it has to wait for a server snapshot.

That should be a quick fix.

It's either not from cache or fromCache in the metadata is wrong.

Here's our code:

https://gist.github.com/burtonator/b9535237fd1c726960fb0fc77430160a

Maybe what's happening is that since no documents at ALL are found, the code assumes nothing is in the cache.

You should store a negative cache entry for that use case.

This is SUPER important for us because the latency is high when waiting for a server snapshot and this breaks our offline support.

Steps to reproduce:

Relevant Code:

Pseudocode is here:

https://gist.github.com/burtonator/b9535237fd1c726960fb0fc77430160a

InputNeuron avatar Jan 07 '22 23:01 InputNeuron

@burtonator thank you for reporting this issue. I'll look into a way to fix it.

dconeybe avatar Jan 10 '22 16:01 dconeybe

Just a heads up that I like to document my findings and progress as I go along so feel free to ignore my status updates :)

Here is the line where the empty snapshot is not raised:

https://github.com/firebase/firebase-js-sdk/blob/4ddf5566ca52b1f9ef776dd647c7d514e3b3f9f5/packages/firestore/src/core/event_manager.ts#L374-L375

Currently a snapshot with no documents could either mean that the cached query contained no documents or there is no cache for the query. We need to add a way to distinguish between these two cases to fix this.

dconeybe avatar Jan 11 '22 16:01 dconeybe

What is normally done is to store a 'negative cache' entry meaning we did fetch the results, but none were found, so you store this negative cache entry then, when you fetch again you emit the empty /null result.

Thanks for the updates! Appreciate it!

InputNeuron avatar Jan 11 '22 17:01 InputNeuron

Just a quick update. I'm working on the fix for this but it is a low priority relative to other work that I have on my plate. At the moment I have a hacky, proof-of-concept solution with some broken edge cases that needs to be cleaned up and have improved test coverage: https://github.com/firebase/firebase-js-sdk/pull/5897. You can watch that PR to see the progress but I don't have an ETA for getting the fix merged.

dconeybe avatar Jan 17 '22 15:01 dconeybe

Update: My colleague has merged a fix for this bug: https://github.com/firebase/firebase-js-sdk/pull/6624. It will be included in the next release, which is scheduled for mid-October 2022. The fix will also be ported to Android and iOS (but no ETA for those fixes).

dconeybe avatar Oct 07 '22 19:10 dconeybe

FYI the fix was released in version 9.12.0 on October 11, 2022: https://firebase.google.com/support/release-notes/js#version_9120_-_october_11_2022

dconeybe avatar Oct 12 '22 19:10 dconeybe