Skip to content

Firestore Documents

firekitDoc returns a reactive instance that subscribes to the document in real time. The UI updates automatically whenever the document changes.

<script lang="ts">
import { firekitDoc } from 'svelte-firekit';
interface Post {
title: string;
body: string;
published: boolean;
}
const post = firekitDoc<Post>('posts/my-post-id');
</script>
{#if post.loading}
<p>Loading…</p>
{:else if post.error}
<p>Error: {post.error.message}</p>
{:else if post.data}
<h1>{post.data.title}</h1>
<p>{post.data.body}</p>
{:else}
<p>Document not found.</p>
{/if}
PropertyTypeDescription
dataT | nullDocument data, or null if not found / not yet loaded
loadingbooleantrue while the initial snapshot is being fetched
errorError | nullSet if the subscription fails
existsbooleanWhether the document exists in Firestore
metadataSnapshotMetadata | nullRaw Firestore snapshot metadata

Call setPath to tear down the existing listener and subscribe to a different document:

// Switch to a different document
post.setPath('posts/another-post-id');

This is useful when the path depends on user interaction or route parameters:

<script lang="ts">
import { firekitDoc } from 'svelte-firekit';
let { postId } = $props<{ postId: string }>();
const post = firekitDoc<Post>(`posts/${postId}`);
$effect(() => {
post.setPath(`posts/${postId}`);
});
</script>

When you don’t need a live subscription, use firekitDocOnce. It fetches the document once and does not open a listener.

import { firekitDocOnce } from 'svelte-firekit';
const post = firekitDocOnce<Post>('posts/my-post-id');
// post.data is populated after the fetch resolves

firekitDocWithMetadata gives you access to Firestore snapshot metadata (e.g. fromCache, hasPendingWrites):

import { firekitDocWithMetadata } from 'svelte-firekit';
const post = firekitDocWithMetadata<Post>('posts/my-post-id');
// post.metadata.fromCache — true if data came from local cache
// post.metadata.hasPendingWrites — true if there are unsaved local writes

All firekitDoc* functions accept an optional options object as the third argument:

const post = firekitDoc<Post>('posts/id', [], {
includeMetadataChanges: true, // re-trigger on cache/pending-write metadata changes
});
OptionTypeDefaultDescription
includeMetadataChangesbooleanfalseListen to metadata changes as well as data changes

The <Doc> component wraps firekitDoc in a declarative template interface using Svelte snippets.

<script lang="ts">
import { Doc } from 'svelte-firekit';
</script>
<Doc path="posts/my-post-id">
{#snippet data(post)}
<h1>{post.title}</h1>
<p>{post.body}</p>
{/snippet}
{#snippet loading()}
<p>Loading…</p>
{/snippet}
{#snippet error(err)}
<p>Error: {err.message}</p>
{/snippet}
</Doc>
PropTypeRequiredDescription
pathstringYesFirestore document path
includeMetadataChangesbooleanNoForward to the underlying subscription
SnippetArgumentsDescription
dataTRendered when document data is available
loadingRendered while loading
errorErrorRendered when the subscription fails
missingRendered when the document does not exist