Firestore Lite 是輕量級的獨立 Firestore SDK,僅支援 REST,但能以一小部分的 Web SDK 大小,支援單一文件擷取、查詢執行和文件更新。Firestore Lite 省略了延遲補償、離線快取、查詢續傳和快照監聽器,但對於特定用途,程式庫大小和啟動時間的縮減是絕佳的取捨。
匯入 Firestore Lite
Firestore Lite 可透過 npm 取得,是模組化 SDK 的一部分。因此完全模組化,且可進行樹狀結構修剪。
[null,null,["上次更新時間: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."]]