2023-04-07 16:02:47 +00:00
|
|
|
import { reactive, ref, shallowReactive, shallowRef } from 'vue'
|
2023-04-07 10:34:35 +00:00
|
|
|
import { definePayloadReviver, getNuxtClientPayload } from '#app/composables/payload'
|
|
|
|
import { createError } from '#app/composables/error'
|
|
|
|
import { callWithNuxt, 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)),
|
|
|
|
ShallowRef: (data: any) => shallowRef(data),
|
|
|
|
ShallowReactive: (data: any) => shallowReactive(data),
|
|
|
|
Ref: (data: any) => ref(data),
|
|
|
|
Reactive: (data: any) => reactive(data)
|
|
|
|
}
|
|
|
|
|
2023-04-11 11:58:43 +00:00
|
|
|
export default defineNuxtPlugin({
|
|
|
|
name: 'nuxt:revive-payload:client',
|
|
|
|
order: -30,
|
|
|
|
async setup (nuxtApp) {
|
|
|
|
for (const reviver in revivers) {
|
|
|
|
definePayloadReviver(reviver, revivers[reviver as keyof typeof revivers])
|
|
|
|
}
|
|
|
|
Object.assign(nuxtApp.payload, await callWithNuxt(nuxtApp, getNuxtClientPayload, []))
|
|
|
|
// For backwards compatibility - TODO: remove later
|
|
|
|
window.__NUXT__ = nuxtApp.payload
|
2023-04-07 10:34:35 +00:00
|
|
|
}
|
|
|
|
})
|