mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-16 02:44:51 +00:00
28 lines
829 B
TypeScript
28 lines
829 B
TypeScript
import { parseURL } from 'ufo'
|
|
import { defineNuxtPlugin } from '#app/nuxt'
|
|
import { loadPayload, isPrerendered } from '#app/composables/payload'
|
|
import { useRouter } from '#app/composables/router'
|
|
|
|
export default defineNuxtPlugin((nuxtApp) => {
|
|
// Only enable behavior if initial page is prerendered
|
|
// TOOD: Support hybrid and dev
|
|
if (!isPrerendered()) {
|
|
return
|
|
}
|
|
|
|
// Load payload into cache
|
|
nuxtApp.hooks.hook('link:prefetch', (url) => {
|
|
if (!parseURL(url).protocol) {
|
|
return loadPayload(url)
|
|
}
|
|
})
|
|
|
|
// 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)
|
|
if (!payload) { return }
|
|
Object.assign(nuxtApp.static.data, payload.data)
|
|
})
|
|
})
|