fix(nuxt): allow serialising undefined refs (#20828)

This commit is contained in:
Daniel Roe 2023-05-13 20:49:05 +01:00 committed by GitHub
parent 2d07e491a4
commit 7f0fe2fbe7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 7 additions and 4 deletions

View File

@ -5,8 +5,8 @@ import { defineNuxtPlugin } from '#app/nuxt'
const revivers = {
NuxtError: (data: any) => createError(data),
EmptyShallowRef: (data: any) => shallowRef(JSON.parse(data)),
EmptyRef: (data: any) => ref(JSON.parse(data)),
EmptyShallowRef: (data: any) => shallowRef(data === '_' ? undefined : JSON.parse(data)),
EmptyRef: (data: any) => ref(data === '_' ? undefined : JSON.parse(data)),
ShallowRef: (data: any) => shallowRef(data),
ShallowReactive: (data: any) => shallowReactive(data),
Ref: (data: any) => ref(data),

View File

@ -6,8 +6,8 @@ import { defineNuxtPlugin } from '#app/nuxt'
const reducers = {
NuxtError: (data: any) => isNuxtError(data) && data.toJSON(),
EmptyShallowRef: (data: any) => isRef(data) && isShallow(data) && !data.value && JSON.stringify(data.value),
EmptyRef: (data: any) => isRef(data) && !data.value && JSON.stringify(data.value),
EmptyShallowRef: (data: any) => isRef(data) && isShallow(data) && !data.value && (JSON.stringify(data.value) || '_'),
EmptyRef: (data: any) => isRef(data) && !data.value && (JSON.stringify(data.value) || '_'),
ShallowRef: (data: any) => isRef(data) && isShallow(data) && data.value,
ShallowReactive: (data: any) => isReactive(data) && isShallow(data) && toRaw(data),
Ref: (data: any) => isRef(data) && data.value,

View File

@ -380,6 +380,7 @@ describe('rich payloads', () => {
'Recursive objects: true',
'Shallow reactive: true',
'Shallow ref: true',
'Undefined ref: true',
'Reactive: true',
'Ref: true',
'Error: true'

View File

@ -6,6 +6,7 @@ if (process.server) {
state.value.ref = r
state.value.shallowReactive = shallowReactive({ nested: { ref: r } })
state.value.shallowRef = shallowRef(false)
state.value.undefined = shallowRef()
state.value.reactive = reactive({ ref: r })
state.value.error = createError({ message: 'error' })
state.value.date = new Date()
@ -19,6 +20,7 @@ if (process.server) {
Error: {{ isNuxtError(state.error) }} <hr>
Shallow reactive: {{ isReactive(state.shallowReactive) && isShallow(state.shallowReactive) }} <br>
Shallow ref: {{ isShallow(state.shallowRef) }} <br>
Undefined ref: {{ isRef(state.undefined) }} <br>
Reactive: {{ isReactive(state.reactive) }} <br>
Ref: {{ isRef(state.ref) }} <hr>
Recursive objects: {{ state.ref === state.shallowReactive.nested.ref }} <br>