Skip to content

Commit 57cdd82

Browse files
committed
fix(ssr): fallback value in firestore
1 parent 949e825 commit 57cdd82

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/firestore/index.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,12 @@ export function _useFirestoreRef(
8080

8181
const data = options.target || ref<unknown | null>()
8282
// set the initial value from SSR even if the ref comes from outside
83-
data.value = getInitialValue('f', options.ssrKey, initialSourceValue)
83+
data.value = getInitialValue(
84+
'f',
85+
options.ssrKey,
86+
initialSourceValue,
87+
data.value
88+
)
8489
// TODO: allow passing pending and error refs as option for when this is called using the options api
8590
const pending = ref(true)
8691
const error = ref<FirestoreError>()

src/ssr/initialState.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ type FirestoreDataSource =
5454
export function getInitialValue(
5555
type: 'f' | 'r',
5656
ssrKey?: string | undefined,
57-
dataSource?: _Nullable<FirestoreDataSource>
57+
dataSource?: _Nullable<FirestoreDataSource>,
58+
fallbackValue?: unknown
5859
) {
5960
const initialState: Record<string, unknown> = useSSRInitialState()[type] || {}
6061
const key = ssrKey || getFirestoreSourceKey(dataSource)
@@ -63,7 +64,7 @@ export function getInitialValue(
6364

6465
// returns undefined if no key, otherwise initial state or undefined
6566
// undefined should be treated as no initial state
66-
return key && initialState[key]
67+
return key && key in initialState ? initialState[key] : fallbackValue
6768
}
6869

6970
export function setInitialValue(

0 commit comments

Comments
 (0)