2

Context

I have a webapp using Firestore and React where I fetch data that has this structure:

worksTodo:
  title
  status
  createdAt

I have a page where I fetch a list of data from firestore, and I query these data with the orderBy() and limit() methods:

todosRef.orderBy('status', 'desc')
        .orderBy('createdAt', 'desc')
        .limit(n)
        .onSnapshot((querySnapshot) => {
            // getting data and setState here...
        });

I also use react-router v4 with history to redirect pages after event.

I have made a codepen to reproduce the bug here. (the code above are line 82 to 108)

Issue

After updating few documents status field, all data that is superior than my limit query (n = 5) gets deleted from cache one by one, which is not expected.

I update the document with this code (line 189 to 195):

todosRef.doc(match.params.id).update({
            title,
            status,
        }).then(() => {
            console.log('document updated');
            history.push('/');
        }).catch((err) => console.error(err));

Reproduction steps

To reproduce the bug, please follow these steps on the codepen page:

  1. Click on Todo List on the header
  2. Click on "reset data" to ensure that all status are set to 'in progress'
  3. Click on the first task
  4. Modify the status of this task from 'in progress' to 'done' with the select input field
  5. Submit
  6. Repeat steps 2, 3 and 4 until the bug trigger
  7. The tasks from the list disappears one by one until 4 tasks remain.
  8. You need to delete the firestore indexedDB from the Application tab of chrome dev tools, and then refresh to get back all data

What I've tried

When I disable offline persistence, the bug totally disappears. So I am sure that it is related to offline persistence.

I contacted the support team of firebase first, but as I use a 3rd-party framework (react), they could not help me, and recommended me to ask for help here.

I also consulted this stackoverflow question : Firestore - Using cache until online content updates But the answer mentions:

"After you get the cached result, Firestore will check with the server to see if there are any changes to your query result. If yes you will get another snapshot with the changes."

However it seems that firestore find that data from server are not different than data from cache in this case.

Question

How can I ensure that the data from cache are exactly the same as the data from the server ?

Edit

I've just found that there is already a very similar case question on SO:

Error when limiting items with Firestore

In this case, the bug happens with angular, which consolidate the idea that the issue comes from the firebase SDK.

Nenu
  • 2,637
  • 1
  • 18
  • 24
  • 1
    Unless I'm misreading the question, it sounds like you have a bug report more than anything else. If you think a Firebase SDK is not working the way it's documented, please file a bug report. https://firebase.google.com/support/contact/troubleshooting/ – Doug Stevenson Jan 16 '18 at 18:25
  • I forgot to mention that I first asked my problem as a bug report on the firebase support page, but the team was not able to help me in anyway as I use a 3rd party framework (react). They encouraged me to ask my question in StackOverflow instead. – Nenu Jan 17 '18 at 06:47
  • In that case, I will encourage you to eliminate the framework from your code and provide an MCVE based on nothing more than the bare minimum to reproduce. – Doug Stevenson Jan 17 '18 at 07:26
  • @DougStevenson I followed your recommendation and sent a MCVE to Firebase support team. They told me that it is effectively a Firebase Web SDK bug. As i'm still new to SO, what should I do to this question ? Should I delete it as there is no solution for it (except disable offline persistence for the moment) ? – Nenu Jan 26 '18 at 15:02
  • Leave it for now, so others can find it if they're searching for the same thing. – Doug Stevenson Jan 26 '18 at 16:14
  • Alright, thank you Doug for your help :) – Nenu Jan 26 '18 at 16:21

0 Answers0