2022-09-16 10:15:10 +00:00
|
|
|
import { defineNuxtPlugin, loadPayload, isPrerendered, useRouter } from '#app'
|
2022-09-10 13:57:16 +00:00
|
|
|
|
|
|
|
export default defineNuxtPlugin((nuxtApp) => {
|
|
|
|
// Only enable behavior if initial page is prerendered
|
2022-09-14 13:01:02 +00:00
|
|
|
// TOOD: Support hybrid and dev
|
2022-09-10 13:57:16 +00:00
|
|
|
if (!isPrerendered()) {
|
|
|
|
return
|
|
|
|
}
|
2022-09-16 10:15:10 +00:00
|
|
|
|
|
|
|
// Load payload into cache
|
|
|
|
nuxtApp.hooks.hook('link:prefetch', to => loadPayload(to))
|
|
|
|
|
|
|
|
// Load payload after middleware & once final route is resolved
|
|
|
|
useRouter().beforeResolve(async (to, from) => {
|
|
|
|
if (to.path === from.path) { return }
|
|
|
|
const payload = await loadPayload(to.path)
|
2022-09-13 20:20:23 +00:00
|
|
|
if (!payload) { return }
|
2022-09-10 13:57:16 +00:00
|
|
|
Object.assign(nuxtApp.payload.data, payload.data)
|
|
|
|
})
|
|
|
|
})
|