firebase / firebase/firebase-js-sdk
Firestore indexDB persistence corrupted after user "Clear site data"
- Dominant language
- TypeScript
- Stars
- 5.1k
- Forks
- 1k
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 37
Description
### Operating System
Windows 11, Chrome, Edge, Android, iOS
### Environment (if applicable)
Chrome all major versions, Edge, Safari iOS tested on 16
### Firebase SDK Version
10.14
### Firebase SDK Product(s)
Firestore
### Project Tooling
Vanilla JS.
Also tried on other web frameworks.
### Detailed Problem Description
If Firestore persistence is active and a doc is cached. Then the user clears the site data, and and doc is updated on backend.
Cache gets corrupted and now the firestore listener returns **NULL document FROM CACHE**
I, leave some concrete steps for reproduction below.
**(You have to put your own firebase_api_key for security)**
https://github.com/user-attachments/assets/f22d0e00-6eee-4bc2-80f7-84c42e4c466d
### Steps and code to reproduce issue
```
Firebase Cache Bug
//For this sample Im using vanilla JS and firebase from CDN.
//But the same error was tested on sveltekit + vite and firebase js sdk.
import { initializeApp } from 'https://www.gstatic.com/firebasejs/10.14.1/firebase-app.js';
import {
initializeFirestore,
CACHE_SIZE_UNLIMITED,
persistentLocalCache,
persistentMultipleTabManager,
onSnapshot,
doc,
getDoc,
clearIndexedDbPersistence,
} from 'https://www.gstatic.com/firebasejs/10.14.1/firebase-firestore.js';
import {
signInWithEmailAndPassword,
onAuthStateChanged,
} from 'https://www.gstatic.com/firebasejs/10.14.1/firebase-auth.js';
const firebaseConfig = {
//Change this to your FIREBASE API KEYS
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const db = initializeFirestore(app, {
localCache: persistentLocalCache(),
});
// You can use this function to clear all the persistence on your IndexedDB.
// This will fix the problem
//
//
/*clearIndexedDbPersistence(db);*/
//Getting the documentData
const docRef = doc(db, 'tests', 'test_document');
const unsub = onSnapshot(docRef, (doc) => {
console.log('Current doc: ', doc);
if (doc.exists()) {
document.getElementById('display').innerHTML = doc.data().data;
}
document.getElementById('diplay-from-cache').innerHTML = JSON.stringify(
doc.metadata,
null,
2
);
});
Firebase BUG
If user clear site data and document is modified from outside, firestore
IndexedDB cache return null document from cache.
Setup:
- 1. Change your FIREBASE API KEYS
- 2. Create a document on tests/test_document {data:'Hello world'}
- 1. Refresh page (To cache the doc)
- 3. Clear site data
- 4. Modify the document on firebase console
- 5. Refresh
- 6. Refresh Again... (Doc return blank from cache:true)
DocData:
Metadata:
.card {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 1rem;
border-radius: 0.5rem;
box-shadow: 0 0 1rem rgba(0, 0, 0, 0.2);
}
.card-header {
font-weight: bold;
}
```
Contributor guide
Assessment
This issue has not been assessed yet.