Skip to content

firestore: fix undefined document snapshot data after "clear site data" #8871

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/slow-students-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@firebase/firestore': patch
'firebase': patch
---

Fix issue where Firestore would produce `undefined` for document snapshot data if using IndexedDB persistence and "clear site data" (or equivalent) button was pressed in the web browser.
29 changes: 29 additions & 0 deletions packages/firestore/src/local/simple_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ export class SimpleDbTransaction {
*/
export class SimpleDb {
private db?: IDBDatabase;
private lastClosedDbVersion: number | null = null;
private versionchangelistener?: (event: IDBVersionChangeEvent) => void;

/** Deletes the specified database. */
Expand Down Expand Up @@ -344,6 +345,24 @@ export class SimpleDb {
event.oldVersion
);
const db = (event.target as IDBOpenDBRequest).result;
if (
this.lastClosedDbVersion !== null &&
this.lastClosedDbVersion !== event.oldVersion
) {
// This thrown error will get passed to the `onerror` callback
// registered above, and will then be propagated correctly.
throw new Error(
`refusing to open IndexedDB database due to potential ` +
`corruption of the IndexedDB database data; this corruption ` +
`could be caused by clicking the "clear site data" button in ` +
`a web browser; try reloading the web page to re-initialize ` +
`the IndexedDB database: ` +
`lastClosedDbVersion=${this.lastClosedDbVersion}, ` +
`event.oldVersion=${event.oldVersion}, ` +
`event.newVersion=${event.newVersion}, ` +
`db.version=${db.version}`
);
}
this.schemaConverter
.createOrUpgrade(
db,
Expand All @@ -359,11 +378,21 @@ export class SimpleDb {
});
};
});

this.db.addEventListener(
'close',
event => {
const db = event.target as IDBDatabase;
this.lastClosedDbVersion = db.version;
},
{ passive: true }
);
}

if (this.versionchangelistener) {
this.db.onversionchange = event => this.versionchangelistener!(event);
}

return this.db;
}

Expand Down
Loading