Skip to content

Commit c5b4f9d

Browse files
authored
Merge 3c75c6d into 670eba6
2 parents 670eba6 + 3c75c6d commit c5b4f9d

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

.changeset/slow-students-fry.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@firebase/firestore': patch
3+
'firebase': patch
4+
---
5+
6+
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.

packages/firestore/src/local/simple_db.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ export class SimpleDbTransaction {
158158
*/
159159
export class SimpleDb {
160160
private db?: IDBDatabase;
161+
private lastClosedDbVersion: number | null = null;
161162
private versionchangelistener?: (event: IDBVersionChangeEvent) => void;
162163

163164
/** Deletes the specified database. */
@@ -344,6 +345,24 @@ export class SimpleDb {
344345
event.oldVersion
345346
);
346347
const db = (event.target as IDBOpenDBRequest).result;
348+
if (
349+
this.lastClosedDbVersion !== null &&
350+
this.lastClosedDbVersion !== event.oldVersion
351+
) {
352+
// This thrown error will get passed to the `onerror` callback
353+
// registered above, and will then be propagated correctly.
354+
throw new Error(
355+
`refusing to open IndexedDB database due to potential ` +
356+
`corruption of the IndexedDB database data; this corruption ` +
357+
`could be caused by clicking the "clear site data" button in ` +
358+
`a web browser; try reloading the web page to re-initialize ` +
359+
`the IndexedDB database: ` +
360+
`lastClosedDbVersion=${this.lastClosedDbVersion}, ` +
361+
`event.oldVersion=${event.oldVersion}, ` +
362+
`event.newVersion=${event.newVersion}, ` +
363+
`db.version=${db.version}`
364+
);
365+
}
347366
this.schemaConverter
348367
.createOrUpgrade(
349368
db,
@@ -359,11 +378,21 @@ export class SimpleDb {
359378
});
360379
};
361380
});
381+
382+
this.db.addEventListener(
383+
'close',
384+
event => {
385+
const db = event.target as IDBDatabase;
386+
this.lastClosedDbVersion = db.version;
387+
},
388+
{ passive: true }
389+
);
362390
}
363391

364392
if (this.versionchangelistener) {
365393
this.db.onversionchange = event => this.versionchangelistener!(event);
366394
}
395+
367396
return this.db;
368397
}
369398

0 commit comments

Comments
 (0)