importJsSdkCache function
- FirebaseFirestore firestore, {
- bool includeDocuments = true,
- bool allowMemoryCache = false,
- String? jsAppName,
Imports the cache of the official Firebase JS SDK into the cache of firestore, once, so that a web app that used
the JS SDK with persistence can switch to this SDK on the same browser profile without losing its pending writes.
It reads the IndexedDB database the JS SDK keeps for the same app and database,
firestore/<app name>/<project id>[.<database id>]/main (the app name is [DEFAULT] for the default app; pass
jsAppName when the JS app had another name than FirebaseFirestore.app), and imports:
- the pending writes of every user, in their order, with their local write times, into the mutation queues of this cache, whatever it is (memory, SQLite, the key-value cache, or the IndexedDB cache of another app). The documents the app reads locally include them, and they are sent to the backend when the client is online. Each imported write is then removed from the JS database;
- with
includeDocuments, the cached documents with their versions and read times, the first time only, so that the first start offline after the switch still shows data. A document that this cache holds in a newer version is kept.
Targets, resume tokens, index configuration, bundles and named queries are not imported: listeners read their results from the backend again.
Call it once at every start of the app, right after creating firestore and before any write: it runs on the
queue of the client, in order with the operations of the app. When there is nothing left to import it only opens
the JS database, and when there is no JS database it returns JsSdkCacheImportStatus.noJsDatabase. On other
platforms than the web it does nothing and returns JsSdkCacheImportStatus.notOnWeb.
A crash at any point, followed by a restart and another call, neither loses a pending write nor imports one twice:
the cache records what it imported in the transaction that adds the writes, and the next call finishes their
removal from the JS database. A memory cache does not keep the imported writes across a reload, while the JS
database no longer has them: pending writes are imported into a memory cache only with allowMemoryCache (a
write imported that way is lost when the page closes before the backend acknowledged it). Documents alone (no
pending writes in the JS database) are imported into a memory cache without it.
It throws a JsSdkCacheImportException and imports nothing when:
- a JS tab may still use the database (JsSdkCacheImportRefusal.jsClientActive): the JS SDK's primary lease was refreshed less than 5 seconds ago, or one of its clients refreshed its metadata less than 30 minutes ago and did not mark itself as closed. A JS tab and a page of this SDK can not share the cache; close the JS tabs (a tab that closes normally marks itself) or wait;
- the database has a schema version this SDK does not read (JsSdkCacheImportRefusal.unsupportedSchemaVersion): versions 5 to 18 are read (the pending writes have been stored in the same shape since version 5; the cached documents are read from version 13 on);
- this cache already holds pending writes of a user who has pending writes in the JS database (JsSdkCacheImportRefusal.pendingWritesInCache): the imported writes are older and would be sent after them. This happens when the app wrote before calling this function;
- the cache is in memory, the JS database has pending writes and
allowMemoryCacheis false (JsSdkCacheImportRefusal.memoryCache); - a pending write can not be read (JsSdkCacheImportRefusal.unreadableWrites).
The JS database is never deleted: call deleteJsSdkCache when the app no longer needs it.
On an instance whose IndexedDB cache (indexedDbCacheSettings) is the JS database itself there is nothing to
import: that cache reads the JS SDK's pending writes as its own (JsSdkCacheImportStatus.nothingToImport).
Tested in Chrome with databases written by the JS SDK 13.3.0 and with hand-built ones of every schema version from 5 on: the backend ends up as when the JS SDK sends the same writes itself (see "Switching from the official JS SDK" in the web guide).
Implementation
Future<JsSdkCacheImportResult> importJsSdkCache(
FirebaseFirestore firestore, {
bool includeDocuments = true,
bool allowMemoryCache = false,
String? jsAppName,
}) {
return runJsSdkCacheImport(
firestore,
includeDocuments: includeDocuments,
allowMemoryCache: allowMemoryCache,
jsAppName: jsAppName,
);
}