Skip to content

Commit

Permalink
fix(ssr): fallback value in firestore
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Nov 15, 2022
1 parent 949e825 commit 57cdd82
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/firestore/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ export function _useFirestoreRef(

const data = options.target || ref<unknown | null>()
// set the initial value from SSR even if the ref comes from outside
data.value = getInitialValue('f', options.ssrKey, initialSourceValue)
data.value = getInitialValue(
'f',
options.ssrKey,
initialSourceValue,
data.value
)
// TODO: allow passing pending and error refs as option for when this is called using the options api
const pending = ref(true)
const error = ref<FirestoreError>()
Expand Down
5 changes: 3 additions & 2 deletions src/ssr/initialState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ type FirestoreDataSource =
export function getInitialValue(
type: 'f' | 'r',
ssrKey?: string | undefined,
dataSource?: _Nullable<FirestoreDataSource>
dataSource?: _Nullable<FirestoreDataSource>,
fallbackValue?: unknown
) {
const initialState: Record<string, unknown> = useSSRInitialState()[type] || {}
const key = ssrKey || getFirestoreSourceKey(dataSource)
Expand All @@ -63,7 +64,7 @@ export function getInitialValue(

// returns undefined if no key, otherwise initial state or undefined
// undefined should be treated as no initial state
return key && initialState[key]
return key && key in initialState ? initialState[key] : fallbackValue
}

export function setInitialValue(
Expand Down

0 comments on commit 57cdd82

Please sign in to comment.