Skip to content


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?


Failed to load latest commit information.
Latest commit message
Commit time


A minimal, yet powerful library that puts realtime Firebase data into Svelte stores.


<!-- 1. πŸ”₯ Firebase App -->
<FirebaseApp {auth} {firestore}>

    <!-- 2. πŸ‘€ Get the current user -->
    <SignedIn let:user>

        <p>Howdy, {user.uid}</p>

        <!-- 3. πŸ“œ Get a Firestore document owned by a user -->
        <Doc ref={`posts/${user.uid}`} let:data={post} let:ref={postRef}>

            <!-- 4. πŸ’¬ Get all the comments in its subcollection -->
            <Collection ref={postRef.path + '/comments'} let:data={comments}>
                {#each comments as comment}



Svelte makes it possible to dramatically simplify the way developers work with Firebase. Here are some problems the project solves:

  • Access users and realtime Firestore data as Svelte stores
  • Automatic subscription disposal to prevent memory/cost leaks
  • Better TypeScript experience for Firebase
  • Handle complex relational data between Auth and Firestore
  • Easily hydrate SvelteKit server data into a realtime Firebase stream

Quick Start

  1. Install Firebase npm i firebase v9+ and initialize it in a file like lib/firebase.js:
npm i sveltefire firebase
import { initializeApp } from 'firebase/app';
import { getFirestore } from 'firebase/firestore';
import { getAuth } from 'firebase/auth';

// Initialize Firebase
const app = initializeApp(/* your firebase config */);
export const db = getFirestore(app);
export const auth = getAuth(app);
  1. Get the Current user
  import { auth } from '$lib/firebase';
  import { userStore } from 'sveltefire';
  const user = userStore(auth);

Hello {$user?.uid}
  1. Listen to realtime data.

Use the $ as much as you want - it will only result in one Firebase read request. When all the subscriptions are removed, it will automatically unsubscribe.

  import { firestore } from '$lib/firebase';
  import { docStore } from 'sveltefire';

  const post = docStore(firestore, 'posts/test');


Or better yet, use the built in Doc and Collection components. See below.


Stores are the building blocks of SvelteFire.

Auth Store

Listen to the current user. Render UI conditionally based on the auth state:

  import { userStore } from 'sveltefire';

  const user = userStore(auth);

{#if $user}
    <p>Hi {$user.uid}</p>
    <p>Sign in...</p>

Firestore Stores

Subscribe to realtime data. The store will unsubscribe automatically to avoid unnecessary Firestore reads.

  import { docStore, collectionStore } from 'sveltefire';

  const post = docStore(firestore, 'posts/test');

  // OR 

  const posts = collectionStore(firestore, 'posts');


{#each $posts as post}


Cast Firebase data to a TS interface:

interface Post {
    id: string;
    title: string;
    content: string;
const post = docStore<Post>(firestore, 'posts/test');
const posts = collectionStore<Post>(firestore, 'posts'); 


SvelteFire is a client-side library, but allows you to hydrate server data into a realtime stream.

First, fetch data from a load function like so:

import { doc, getDoc } from 'firebase/firestore';

export const load = (async () => {
    const ref = doc(firestore, 'posts', 'first-post');
    const snapshot = await getDoc(ref);
    return {

Second, pass the server data as the startWith value to a store. This will bypass the loading state and ensure the data is rendered in the server HTML, then realtime listeners will be attached afterwards.

// Data fetched via server
export let data: PageData;

// Just give the store a startWith value 
const post = docStore(firestore, 'posts/test',;

Realtime Components

In addition to stores, SvelteFire provides a set of components that can build complex realtime apps without leaving the HTML.


The FirebaseApp component puts the FirebaseSDK into Svelte context. This avoids the need to pass auth and firestore down to every component. It is typically placed in root layout.

<!-- +layout.svelte -->
  // Initialize Firebase...
  const db = getFirestore(app);
  const auth = getAuth(app);

<FirebaseApp {auth} {firestore}>

    <User let:user></User>
    <!-- other sveltefire components here -->


You can use Svelte's context API to access the Firebase SDK in any component.

  import { getFirebaseContext } from "sveltefire";
  const { auth, firestore } = getFirebaseContext();


Get the current user.

<SignedIn let:user>
    Hello {user.uid}

    You need to sign in!


Fetch a single document and listen to data in realtime. The data slot prop provides access to the fetched data, while ref is the Firestore document reference.

<Doc ref="posts/test" let:data let:ref>

Slot props can be renamed:

<Doc ref="posts/test" let:data={post} let:ref={postRef}>

Firestore components can also handle loading states:

<Doc path="posts/test">
    <!-- data renders here in the default slot -->
    <div slot="loading">Loading.... This will disappear when data is defined</div>

Pass a startWith value to bypass the loading state. This is useful in SvelteKit when you need to hydrate server data into a realtime stream:

<Doc ref="posts/test" startWith={dataFromServer}>


Collections provides array of objects containing the document data, as well as the id and ref for each result. It also provides a count slot prop for number of docs in the query.

<Collection ref="posts" let:data let:count>
  <p>Fetched {count} documents</p>
  {#each data as post}

Collections can also take a Firestore Query instead of a path:

    const myQuery = query(collection(firestore, 'posts'), where('test', '==', 'test'));

<Collection ref={myQuery} let:data>


DownloadURL provides a link to download a file from Firebase Storage and its reference.

<DownloadURL ref={item} let:link let:ref>
    <a href={link} download>Download {ref?.name}</a>


StorageList provides a list of items and prefixes corresponding to the list of objects and sub-folders at a given Firebase Storage path.

<StorageList ref="/" let:list>
        {#if list === null}
        {:else if list.prefixes.length === 0 && list.items.length === 0}
          <!-- Listing the prefixes -->
          {#each list.prefixes as prefix}
          <!-- Listing the objects in the given folder -->
          {#each list.items as item}


Upload a file with progress tracking

<UploadTask ref="filename.txt" data={someBlob} let:progress let:snapshot>
  {#if snapshot?.state === "running"}
    {progress}% uploaded

  {#if snapshot?.state === "success"}
    <DownloadURL ref={snapshot?.ref} let:link>
      <a href={link} download>Download</a>

Using Components Together

These components can be combined to build complex realtime apps. It's especially powerful when fetching data that requires the current user's UID or a related document's path.

<FirebaseApp {auth} {firestore}>
  <SignedIn let:user>
      <p>UID: {user.uid}</p>
      <Doc ref={`posts/${user.uid}`} let:data={profile} let:ref={profileRef}>


        <Collection ref={profileRef.path + '/comments'} let:data={comments}>
          {#each comments as comment}

          <div slot="loading">Loading Comments...</div>

        <div slot="loading">Loading Profile...</div>

      <p>Sign in to see your profile</p>


  • Add support for Firebase Storage
  • Add support for Firebase RTDB
  • Add support for Firebase Analytics in SvelteKit
  • Find a way to make TS generics with with Doc/Collection components