Skip to content

Commit b662d08

Browse files
committed
refactor: stricter ssr data
1 parent df57432 commit b662d08

File tree

5 files changed

+32
-11
lines changed

5 files changed

+32
-11
lines changed

src/database/useDatabaseRef.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
getCurrentInstance,
1313
onServerPrefetch,
1414
} from 'vue-demi'
15+
import { useFirebaseApp } from '../app'
1516
import {
1617
_MaybeRef,
1718
_Nullable,
@@ -62,7 +63,13 @@ export function _useDatabaseRef(
6263
}
6364

6465
// set the initial value from SSR even if the ref comes from outside
65-
data.value = getInitialValue(initialSourceValue, options.ssrKey, data.value)
66+
// TODO: firebase app name
67+
data.value = getInitialValue(
68+
initialSourceValue,
69+
options.ssrKey,
70+
data.value,
71+
useFirebaseApp()
72+
)
6673

6774
const error = ref<Error>()
6875
const pending = ref(true)

src/firestore/useFirestoreRef.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
onServerPrefetch,
1818
onScopeDispose,
1919
} from 'vue-demi'
20+
import { useFirebaseApp } from '../app'
2021
import {
2122
_MaybeRef,
2223
_Nullable,
@@ -80,7 +81,13 @@ export function _useFirestoreRef(
8081
}
8182

8283
// set the initial value from SSR even if the ref comes from outside
83-
data.value = getInitialValue(initialSourceValue, options.ssrKey, data.value)
84+
// TODO: allow passing firebase app name
85+
data.value = getInitialValue(
86+
initialSourceValue,
87+
options.ssrKey,
88+
data.value,
89+
useFirebaseApp()
90+
)
8491

8592
const pending = ref(true)
8693
const error = ref<FirestoreError>()

src/ssr/initialState.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ const initialStatesMap = new WeakMap<FirebaseApp, SSRStore>()
4141
* @returns the initial states for the current firebaseApp
4242
*/
4343
export function useSSRInitialState(
44-
initialState?: SSRStore,
45-
firebaseApp: FirebaseApp = useFirebaseApp()
44+
initialState: SSRStore | undefined,
45+
firebaseApp: FirebaseApp
4646
): SSRStore {
4747
// get initial state based on the current firebase app
4848
if (!initialStatesMap.has(firebaseApp)) {
@@ -60,15 +60,16 @@ export function getInitialValue(
6060
_FirestoreDataSource | DatabaseReference | DatabaseQuery | StorageReference
6161
>,
6262
ssrKey: string | undefined,
63-
fallbackValue: unknown
63+
fallbackValue: unknown,
64+
firebaseApp: FirebaseApp
6465
) {
6566
if (!dataSource) return fallbackValue
6667

6768
const [sourceType, path] = getDataSourceInfo(dataSource)
6869
if (!sourceType) return fallbackValue
6970

7071
const initialState: Record<string, unknown> =
71-
useSSRInitialState()[sourceType] || {}
72+
useSSRInitialState(undefined, firebaseApp)[sourceType] || {}
7273
const key = ssrKey || path
7374

7475
// TODO: warn for queries on the client if there are other keys and this is during hydration
@@ -82,14 +83,18 @@ export function deferInitialValueSetup(
8283
_FirestoreDataSource | DatabaseReference | DatabaseQuery | StorageReference
8384
>,
8485
ssrKey: string | undefined | null,
85-
promise: Promise<unknown>
86+
promise: Promise<unknown>,
87+
firebaseApp: FirebaseApp
8688
) {
8789
if (!dataSource) return
8890

8991
const [sourceType, path] = getDataSourceInfo(dataSource)
9092
if (!sourceType) return
9193

92-
const initialState: Record<string, unknown> = useSSRInitialState()[sourceType]
94+
const initialState: Record<string, unknown> = useSSRInitialState(
95+
undefined,
96+
firebaseApp
97+
)[sourceType]
9398
const key = ssrKey || path
9499

95100
if (key) {

src/ssr/plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function addPendingPromise(
3636
const pendingPromises = appPendingPromises.get(app)!
3737

3838
// TODO: skip this outside of SSR
39-
const key = deferInitialValueSetup(dataSource, ssrKey, promise)
39+
const key = deferInitialValueSetup(dataSource, ssrKey, promise, app)
4040
if (key) {
4141
pendingPromises.set(key, promise)
4242
} else {

src/storage/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ export function useStorageFileUrl(
5353
url.value = getInitialValue(
5454
initialSourceValue,
5555
undefined,
56-
url.value
56+
url.value,
57+
useFirebaseApp()
5758
) as string
5859
const promise = ref<Promise<string | null>>(Promise.resolve(null))
5960
// TODO: pending and error states?
@@ -108,7 +109,8 @@ export function useStorageFileMetadata(
108109
initialSourceValue,
109110
// 'm ' is a prefix to differentiate from urls since both are stored in the same object
110111
'm ' + initialSourceValue.toString(),
111-
metadata.value
112+
metadata.value,
113+
useFirebaseApp()
112114
) as FullMetadata
113115
}
114116
const promise = shallowRef<Promise<FullMetadata | null>>(

0 commit comments

Comments
 (0)