Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
use nuxt.static to store payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Nov 10, 2022
1 parent 33eb38b commit b859e22
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
13 changes: 7 additions & 6 deletions packages/nuxt/src/app/composables/asyncData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,14 @@ export function useAsyncData<
// Setup nuxt instance payload
const nuxt = useNuxtApp()

const usePayloadData = () => nuxt.isHydrating && nuxt.payload.data[key] !== undefined
const getCachedData = () => nuxt.isHydrating ? nuxt.payload.data[key] : nuxt.static.data[key]
const hasCachedData = () => getCachedData() !== undefined

// Create or use a shared asyncData entity
if (!nuxt._asyncData[key]) {
nuxt._asyncData[key] = {
data: ref(usePayloadData() ? nuxt.payload.data[key] : options.default?.() ?? null),
pending: ref(!usePayloadData()),
data: ref(getCachedData() ?? options.default?.() ?? null),
pending: ref(!hasCachedData()),
error: ref(nuxt.payload._errors[key] ? createError(nuxt.payload._errors[key]) : null)
}
}
Expand All @@ -128,8 +129,8 @@ export function useAsyncData<
(nuxt._asyncDataPromises[key] as any).cancelled = true
}
// Avoid fetching same key that is already fetched
if (opts._initial && usePayloadData()) {
return nuxt.payload.data[key]
if (opts._initial && hasCachedData()) {
return getCachedData()
}
asyncData.pending.value = true
// TODO: Cancel previous promise
Expand Down Expand Up @@ -202,7 +203,7 @@ export function useAsyncData<
}
}

if (fetchOnServer && nuxt.isHydrating && key in nuxt.payload.data) {
if (fetchOnServer && nuxt.isHydrating && hasCachedData()) {
// 1. Hydration (server: true): no fetch
asyncData.pending.value = false
} else if (instance && ((nuxt.payload.serverRendered && nuxt.isHydrating) || options.lazy) && options.immediate) {
Expand Down
6 changes: 6 additions & 0 deletions packages/nuxt/src/app/nuxt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ interface _NuxtApp {
} | null
[key: string]: any
}
static: {
data: Record<string, any>
}

provide: (name: string, value: any) => void
}
Expand Down Expand Up @@ -118,6 +121,9 @@ export function createNuxtApp (options: CreateOptions) {
_errors: {},
...(process.client ? window.__NUXT__ : { serverRendered: true })
}),
static: {
data: {}
},
isHydrating: process.client,
deferHydration () {
if (!nuxtApp.isHydrating) { return () => {} }
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt/src/app/plugins/payload.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ export default defineNuxtPlugin((nuxtApp) => {
if (to.path === from.path) { return }
const payload = await loadPayload(to.path)
if (!payload) { return }
Object.assign(nuxtApp.payload.data, payload.data)
Object.assign(nuxtApp.static.data, payload.data)
})
})

0 comments on commit b859e22

Please sign in to comment.