You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SvelteFire is a client-side library, but allows you to hydrate server data into a realtime stream.
155
+
156
+
First, fetch data from a load function like so:
157
+
158
+
```ts
159
+
import { doc, getDoc } from'firebase/firestore';
160
+
161
+
exportconst load = (async () => {
162
+
const ref =doc(firestore, 'posts', 'first-post');
163
+
const snapshot =awaitgetDoc(ref);
164
+
return {
165
+
post: snapshot.data();
166
+
};
167
+
});
143
168
```
144
169
145
-
Hydrate server-fetched data from SvelteKit into a realtime feed:
170
+
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.
@@ -158,7 +183,8 @@ In addition to stores, SvelteFire provides a set of components that can build co
158
183
159
184
### FirebaseApp
160
185
161
-
Technically optional, this component puts Firebase into Svelte context. This avoids the need to pass `auth` and `firestore` down to every component. All other components should be nested below it.
186
+
The `FirebaseApp` component puts the FirebaseSDK into Svelte context. This avoids the need to pass `auth` and `firestore` down to every component/store. It is typically placed in root layout.
187
+
162
188
```svelte
163
189
<script>
164
190
// Initialize Firebase...
@@ -174,18 +200,27 @@ Technically optional, this component puts Firebase into Svelte context. This avo
174
200
</FirebaseApp>
175
201
```
176
202
177
-
Note: Components outside a FirebaseApp will need the auth/firestore prop, i.e `<User auth={auth}>`
203
+
You can easily access the Firebase SDK in any component via context. This is useful when using the Firebase SDK directly, which requires the SDK as an argument.
204
+
205
+
```svelte
206
+
<script>
207
+
import { getFirebaseContext } from "sveltefire";
208
+
const { auth, firestore } = getFirebaseContext();
209
+
</script>
210
+
```
178
211
179
212
### User
180
213
181
214
Get the current user.
182
215
183
216
```svelte
184
-
<User let:user>
217
+
<SignedIn let:user>
185
218
Hello {user.uid}
219
+
</SignedIn>
186
220
187
-
<div slot="signedOut">You are signed out</div>
188
-
</User>
221
+
<SignedOut>
222
+
You need to sign in!
223
+
</SignedOut>
189
224
```
190
225
191
226
### Doc
@@ -208,7 +243,7 @@ Slot props can be renamed:
208
243
</Doc>
209
244
```
210
245
211
-
All Firestore components can also handle loading states:
246
+
Firestore components can also handle loading states:
212
247
213
248
```svelte
214
249
<Doc path="posts/test">
@@ -220,7 +255,7 @@ All Firestore components can also handle loading states:
220
255
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:
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.
269
291
270
292
271
293
```svelte
272
294
<FirebaseApp {auth} {firestore}>
273
-
<User let:user>
295
+
<SignedIn let:user>
274
296
<p>UID: {user.uid}</p>
275
297
276
298
@@ -293,14 +315,14 @@ These components can be combined to build complex realtime apps. It's especially
293
315
</Doc>
294
316
295
317
<div slot="signedOut">Signed out</div>
296
-
</User>
318
+
</SignedIn>
297
319
</FirebaseApp>
298
320
```
299
321
300
322
301
-
## Notes
323
+
## Roadmap
302
324
303
-
-This library should only run the the client, it is not for server-side data fetching.
304
-
-Requires Firebase v9 or greater.
305
-
-I've have not been able to get TS generics to work right in the components yet, so no intellisense on the `data` slot prop.
306
-
-How should we bundle it properly?
325
+
-Add support for Firebase Storage
326
+
-Add support for Firebase RTDB
327
+
-Add support for Firebase Analytics in SvelteKit
328
+
-Find a way to make TS generics with with Doc/Collection components
0 commit comments