@@ -21,6 +21,17 @@ export function docStore<T>(
21
21
) {
22
22
let unsubscribe : ( ) => void ;
23
23
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
+
24
35
const docRef = typeof ref === 'string' ? doc ( firestore , ref ) : ref ;
25
36
26
37
const { subscribe } = writable < T | null > ( startWith , ( set ) => {
@@ -51,6 +62,16 @@ export function collectionStore<T>(
51
62
) {
52
63
let unsubscribe : ( ) => void ;
53
64
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
+
54
75
const colRef = typeof ref === 'string' ? collection ( firestore , ref ) : ref ;
55
76
56
77
const { subscribe } = writable ( startWith , ( set ) => {
@@ -76,7 +97,15 @@ export function collectionStore<T>(
76
97
export function userStore ( auth : Auth ) {
77
98
let unsubscribe : ( ) => void ;
78
99
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 ) => {
80
109
unsubscribe = onAuthStateChanged ( auth , ( user ) => {
81
110
set ( user ) ;
82
111
} ) ;
@@ -89,5 +118,5 @@ export function userStore(auth: Auth) {
89
118
} ;
90
119
}
91
120
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