@@ -21,6 +21,17 @@ export function docStore<T>(
2121) {
2222 let unsubscribe : ( ) => void ;
2323
24+ // Fallback for SSR
25+ if ( ! firestore || ! globalThis . window ) {
26+ console . warn ( 'Firestore is not initialized or not in browser' ) ;
27+ const { subscribe } = writable ( startWith ) ;
28+ return {
29+ subscribe,
30+ ref : null ,
31+ id : '' ,
32+ }
33+ }
34+
2435 const docRef = typeof ref === 'string' ? doc ( firestore , ref ) : ref ;
2536
2637 const { subscribe } = writable < T | null > ( startWith , ( set ) => {
@@ -51,6 +62,16 @@ export function collectionStore<T>(
5162) {
5263 let unsubscribe : ( ) => void ;
5364
65+ // Fallback for SSR
66+ if ( ! firestore || ! globalThis . window ) {
67+ console . warn ( 'Firestore is not initialized or not in browser' ) ;
68+ const { subscribe } = writable ( startWith ) ;
69+ return {
70+ subscribe,
71+ ref : null ,
72+ }
73+ }
74+
5475 const colRef = typeof ref === 'string' ? collection ( firestore , ref ) : ref ;
5576
5677 const { subscribe } = writable ( startWith , ( set ) => {
@@ -76,7 +97,15 @@ export function collectionStore<T>(
7697export function userStore ( auth : Auth ) {
7798 let unsubscribe : ( ) => void ;
7899
79- const { subscribe } = writable ( auth . currentUser , ( set ) => {
100+ if ( ! auth || ! globalThis . window ) {
101+ console . warn ( 'Auth is not initialized on not in browser' ) ;
102+ const { subscribe } = writable ( null ) ;
103+ return {
104+ subscribe,
105+ }
106+ }
107+
108+ const { subscribe } = writable ( auth ?. currentUser ?? null , ( set ) => {
80109 unsubscribe = onAuthStateChanged ( auth , ( user ) => {
81110 set ( user ) ;
82111 } ) ;
@@ -89,5 +118,5 @@ export function userStore(auth: Auth) {
89118 } ;
90119}
91120
92- // Key for context
93- export const key = Symbol ( ) ;
121+ // SDK store for FirebaseApp comopnent
122+ export const sdk = writable < { auth : Auth ; firestore : Firestore } > ( ) ;
0 commit comments