Firestore Lite 是一种轻量级独立式 REST 专用 Firestore SDK,支持单一文档提取、查询执行和文档更新,并且大小比常规 Web SDK 小得多。Firestore Lite 舍弃了延迟补偿、离线缓存、查询接续和快照侦听器功能,但对于特定的使用场景,库大小和启动时间方面的缩减所带来的益处足以盖过这些不足。
导入 Firestore Lite
Firestore Lite 通过 npm 作为模块化 SDK 的一部分的提供。因此,它是完全模块化且可摇树。
[null,null,["最后更新时间 (UTC):2025-08-16。"],[],[],null,["\u003cbr /\u003e\n\nFirestore is a good scalable database solution to keep data in sync across Web\nclients.\n\nFor many apps, Firestore's managed offline support is especially important,\nletting you build responsive apps that work regardless of network latency or\nInternet connectivity. But feature-rich SDKs come at a size cost. What\ndoes Firebase offer for apps that only need to use basic create, read, update\nand delete operations, and don't need managed offline support?\n| **Note:** Before using Firestore Lite, be sure you understand the standard Firestore Web API and Firestore's offline capabilities as part of the full [feature set](../index.html). We recommend Firestore Lite for developers who have experience building with Firestore and can evaluate the tradeoffs of using a lightweight version.\n\nSolution: Firestore Lite\n\nFirestore Lite is a lightweight, standalone REST-only Firestore SDK that\nsupports single document fetches, query execution, and document updates, at a\nfraction of the regular Web SDK size. Firestore Lite omits latency compensation,\noffline caching, query resumption and snapshot listeners, but for particular\nuse cases, the reductions in library size and startup time make a great tradeoff.\n\nImport Firestore Lite\n\nFirestore Lite is available via npm as part of the\n[modular SDK](//firebase.google.com/docs/web/learn-more#modular-version). It is\nthus fully modular and tree-shakeable.\n\nThe following import style is supported. \n\n import { initializeApp } from \"firebase/app\";\n import {\n getFirestore,\n getDoc,\n updateDoc\n } from 'firebase/firestore/lite';\n\nAPI features not supported by Firestore Lite\n\nFor size and speed, Firestore Lite omits these features from the standard\nFirestore SDK:\n\n- **DocumentSnapshot event handlers** . The `onSnapshot` method and `DocumentChange`, `SnapshotListenerOptions`, `SnapshotMetadata`, `SnapshotOptions` and `Unsubscribe` objects are not included.\n- **Persistence helpers** . The `enableIndexedDBPersistence`, `enableMultiTabIndexedDbPersistence`, and `clearIndexedDbPersistence` methods are not included.\n- **Firestore bundles** . The `loadBundle` method and related methods, and the `LoadBundleTask` and `LoadBundleTaskProgress` objects are not included.\n\nImplement document fetches, queries and updates\n\nAfter importing Firestore Lite, you can make all of the familiar API get\nand update calls. The use cases for [adding data](../manage-data/add-data)\nand [getting data](../query-data/get-data) all apply. \n\n import {\n getFirestore,\n getDoc,\n updateDoc,\n doc\n } from '@firebase/firestore/lite';\n\n const firestore = getFirestore(app);\n const docRef = doc(firestore, 'collection/doc');\n const docSnap = await getDoc(docRef);\n await updateDoc(docRef, \"field\", 'value');\n\nWhen to use Firestore Lite\n\nIt can be tricky to decide when to let go of the standard Firestore SDK's\noffline persistence and caching features. You should understand these features\nbefore deciding to trade them away for the lower overhead of Firestore Lite. In\ngeneral, weigh these factors when deciding whether to use Firestore Lite:\n\n- **Online status** - Firestore Lite is good for apps that do not need live updates and have connectivity.\n- **Size constraints** - Firestore Lite is great if you want to reduce your overall JavaScript bundle size."]]