Skip to content

Context Helpers

After <FirebaseApp> mounts, it sets raw Firebase service instances in Svelte context. Use these helpers to access them in any descendant component.

import {
getFirebaseAppContext,
getAuthContext,
getFirestoreContext,
getStorageContext,
getRTDBContext,
getFunctionsContext,
getAppCheckContext,
getAnalyticsContext,
getMessagingContext,
} from 'svelte-firekit';
HelperReturnsFirebase SDK type
getFirebaseAppContext()FirebaseAppThe initialized Firebase app instance
getAuthContext()AuthFirebase Auth instance
getFirestoreContext()FirestoreFirestore instance
getStorageContext()FirebaseStorageStorage instance
getRTDBContext()DatabaseRealtime Database instance
getFunctionsContext()FunctionsCloud Functions instance
getAppCheckContext()AppCheck | nullApp Check instance (or null if not enabled)
getAnalyticsContext()Analytics | nullAnalytics instance (or null if not initialized)
getMessagingContext()Messaging | nullFCM instance (or null if not initialized)

Access Firestore directly to run a custom query not covered by firekitCollection:

<script lang="ts">
import { getFirestoreContext } from 'svelte-firekit';
import { collection, query, where, getDocs } from 'firebase/firestore';
const db = getFirestoreContext();
async function customQuery() {
const q = query(
collection(db, 'posts'),
where('authorId', '==', userId),
where('tags', 'array-contains', 'svelte')
);
const snap = await getDocs(q);
return snap.docs.map(d => ({ id: d.id, ...d.data() }));
}
</script>
<script lang="ts">
import { getFirestoreContext, firekitMutations } from 'svelte-firekit';
import { doc } from 'firebase/firestore';
const db = getFirestoreContext();
async function incrementCounter() {
const ref = doc(db, 'counters/clicks');
await firekitMutations.transaction(async (tx) => {
const snap = await tx.get(ref);
const count = snap.data()?.count ?? 0;
tx.update(ref, { count: count + 1 });
});
}
</script>
<script lang="ts">
import { getStorageContext } from 'svelte-firekit';
import { ref, listAll } from 'firebase/storage';
const storage = getStorageContext();
async function listUserFiles(uid: string) {
const dirRef = ref(storage, `users/${uid}/uploads`);
const result = await listAll(dirRef);
return result.items;
}
</script>