Skip to content

Commit

Permalink
feat(firestore): force once option during SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Nov 24, 2022
1 parent 485420c commit 397a8de
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/firestore/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
import { useFirebaseApp } from '../app'
import {
isDocumentRef,
isSSR,
noop,
OperationsType,
ResetOption,
Expand Down Expand Up @@ -67,6 +68,10 @@ export function _useFirestoreRef(
const options = Object.assign({}, firestoreOptionsDefaults, localOptions)
const initialSourceValue = unref(docOrCollectionRef)

if (isSSR()) {
options.once = true
}

const data = options.target || ref<unknown | null>()
// set the initial value from SSR even if the ref comes from outside
data.value = getInitialValue(initialSourceValue, options.ssrKey, data.value)
Expand Down
15 changes: 15 additions & 0 deletions src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Timestamp,
} from 'firebase/firestore'
import { StorageReference } from 'firebase/storage'
import { inject, ssrContextKey } from 'vue-demi'
import type { Ref, ShallowRef } from 'vue-demi'

export const noop = () => {}
Expand Down Expand Up @@ -272,3 +273,17 @@ export type _Mutable<T> = {
export interface _ResolveRejectFn {
(value: unknown): void
}

/**
* Check if we are in an SSR environment within a composable. Used to force `options.once` to `true`.
*
* @internal
*/
export function isSSR(): boolean {
return (
!!inject(ssrContextKey, null) ||
//
// @ts-expect-error
import.meta.env?.SSR
)
}

0 comments on commit 397a8de

Please sign in to comment.