Skip to content

Commit eb00e3e

Browse files
committed
fix(ssr): apply multiple reducers for complex data
1 parent 70793dc commit eb00e3e

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed
Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Timestamp, GeoPoint } from 'firebase/firestore'
2+
import { markRaw } from 'vue'
23
import {
34
definePayloadPlugin,
45
definePayloadReducer,
@@ -20,35 +21,40 @@ export default definePayloadPlugin(() => {
2021
const parsed = JSON.parse(data)
2122

2223
if ('seconds' in parsed && 'nanoseconds' in parsed) {
23-
return new Timestamp(parsed.seconds, parsed.nanoseconds)
24+
return markRaw(new Timestamp(parsed.seconds, parsed.nanoseconds))
2425
}
2526

2627
if ('latitude' in parsed && 'longitude' in parsed) {
27-
return new GeoPoint(parsed.latitude, parsed.longitude)
28+
return markRaw(new GeoPoint(parsed.latitude, parsed.longitude))
2829
}
2930

3031
return parsed
3132
})
33+
3234
// to handle the `id` non-enumerable property
33-
definePayloadReducer(
35+
definePayloadReducer('DocumentData', (data: any) => {
36+
if (data && typeof data === 'object') {
37+
const idProp = Object.getOwnPropertyDescriptor(data, 'id')
38+
// we need the non enumerable id property as it's likely used
39+
if (idProp && !idProp.enumerable) {
40+
return {
41+
...data,
42+
id: data.id,
43+
}
44+
}
45+
}
46+
})
47+
definePayloadReviver(
3448
'DocumentData',
35-
(data: any) =>
36-
data &&
37-
typeof data === 'object' &&
38-
'id' in data &&
39-
JSON.stringify({
40-
...data,
41-
id: data.id,
49+
(data: string | Record<string, unknown>) => {
50+
const parsed = typeof data === 'string' ? JSON.parse(data) : data
51+
// preserve the non-enumerable property
52+
// we need to delete it first
53+
const idValue = parsed.id
54+
delete parsed.id
55+
return Object.defineProperty(parsed, 'id', {
56+
value: idValue,
4257
})
58+
}
4359
)
44-
definePayloadReviver('DocumentData', (data: string) => {
45-
const parsed = JSON.parse(data)
46-
// preserve the non-enumerable property
47-
// we need to delete it first
48-
const idValue = parsed.id
49-
delete parsed.id
50-
return Object.defineProperty(parsed, 'id', {
51-
value: idValue,
52-
})
53-
})
5460
})

0 commit comments

Comments
 (0)