2023-04-07 16:02:47 +00:00
|
|
|
import { reactive, ref, shallowReactive, shallowRef } from 'vue'
|
2023-09-05 08:42:16 +00:00
|
|
|
import destr from 'destr'
|
2023-10-30 21:05:02 +00:00
|
|
|
import { definePayloadReviver, getNuxtClientPayload } from '../composables/payload'
|
|
|
|
import { createError } from '../composables/error'
|
|
|
|
import { defineNuxtPlugin, useNuxtApp } from '../nuxt'
|
2023-04-07 10:34:35 +00:00
|
|
|
|
2023-06-14 09:09:27 +00:00
|
|
|
// @ts-expect-error Virtual file.
|
|
|
|
import { componentIslands } from '#build/nuxt.config.mjs'
|
|
|
|
|
|
|
|
const revivers: Record<string, (data: any) => any> = {
|
|
|
|
NuxtError: data => createError(data),
|
2023-09-05 08:42:16 +00:00
|
|
|
EmptyShallowRef: data => shallowRef(data === '_' ? undefined : data === '0n' ? BigInt(0) : destr(data)),
|
|
|
|
EmptyRef: data => ref(data === '_' ? undefined : data === '0n' ? BigInt(0) : destr(data)),
|
2023-06-14 09:09:27 +00:00
|
|
|
ShallowRef: data => shallowRef(data),
|
|
|
|
ShallowReactive: data => shallowReactive(data),
|
|
|
|
Ref: data => ref(data),
|
|
|
|
Reactive: data => reactive(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (componentIslands) {
|
2023-12-19 12:21:29 +00:00
|
|
|
revivers.Island = ({ key, params, result }: any) => {
|
2023-06-14 09:09:27 +00:00
|
|
|
const nuxtApp = useNuxtApp()
|
|
|
|
if (!nuxtApp.isHydrating) {
|
2023-10-20 15:58:02 +00:00
|
|
|
nuxtApp.payload.data[key] = nuxtApp.payload.data[key] || $fetch(`/__nuxt_island/${key}.json`, {
|
2023-06-16 14:47:59 +00:00
|
|
|
responseType: 'json',
|
|
|
|
...params ? { params } : {}
|
|
|
|
}).then((r) => {
|
2023-06-14 09:09:27 +00:00
|
|
|
nuxtApp.payload.data[key] = r
|
|
|
|
return r
|
|
|
|
})
|
|
|
|
}
|
2023-12-19 12:21:29 +00:00
|
|
|
return {
|
|
|
|
html: '',
|
|
|
|
state: {},
|
|
|
|
head: {
|
|
|
|
link: [],
|
|
|
|
style: []
|
|
|
|
},
|
|
|
|
...result
|
|
|
|
}
|
2023-06-14 09:09:27 +00:00
|
|
|
}
|
2023-04-07 10:34:35 +00:00
|
|
|
}
|
|
|
|
|
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])
|
|
|
|
}
|
2023-05-03 10:02:07 +00:00
|
|
|
Object.assign(nuxtApp.payload, await nuxtApp.runWithContext(getNuxtClientPayload))
|
2023-04-11 11:58:43 +00:00
|
|
|
// For backwards compatibility - TODO: remove later
|
|
|
|
window.__NUXT__ = nuxtApp.payload
|
2023-04-07 10:34:35 +00:00
|
|
|
}
|
|
|
|
})
|