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

Commit b859e22

Browse files
committed
use nuxt.static to store payloads
1 parent 33eb38b commit b859e22

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

packages/nuxt/src/app/composables/asyncData.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,14 @@ export function useAsyncData<
106106
// Setup nuxt instance payload
107107
const nuxt = useNuxtApp()
108108

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

111112
// Create or use a shared asyncData entity
112113
if (!nuxt._asyncData[key]) {
113114
nuxt._asyncData[key] = {
114-
data: ref(usePayloadData() ? nuxt.payload.data[key] : options.default?.() ?? null),
115-
pending: ref(!usePayloadData()),
115+
data: ref(getCachedData() ?? options.default?.() ?? null),
116+
pending: ref(!hasCachedData()),
116117
error: ref(nuxt.payload._errors[key] ? createError(nuxt.payload._errors[key]) : null)
117118
}
118119
}
@@ -128,8 +129,8 @@ export function useAsyncData<
128129
(nuxt._asyncDataPromises[key] as any).cancelled = true
129130
}
130131
// Avoid fetching same key that is already fetched
131-
if (opts._initial && usePayloadData()) {
132-
return nuxt.payload.data[key]
132+
if (opts._initial && hasCachedData()) {
133+
return getCachedData()
133134
}
134135
asyncData.pending.value = true
135136
// TODO: Cancel previous promise
@@ -202,7 +203,7 @@ export function useAsyncData<
202203
}
203204
}
204205

205-
if (fetchOnServer && nuxt.isHydrating && key in nuxt.payload.data) {
206+
if (fetchOnServer && nuxt.isHydrating && hasCachedData()) {
206207
// 1. Hydration (server: true): no fetch
207208
asyncData.pending.value = false
208209
} else if (instance && ((nuxt.payload.serverRendered && nuxt.isHydrating) || options.lazy) && options.immediate) {

packages/nuxt/src/app/nuxt.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ interface _NuxtApp {
8989
} | null
9090
[key: string]: any
9191
}
92+
static: {
93+
data: Record<string, any>
94+
}
9295

9396
provide: (name: string, value: any) => void
9497
}
@@ -118,6 +121,9 @@ export function createNuxtApp (options: CreateOptions) {
118121
_errors: {},
119122
...(process.client ? window.__NUXT__ : { serverRendered: true })
120123
}),
124+
static: {
125+
data: {}
126+
},
121127
isHydrating: process.client,
122128
deferHydration () {
123129
if (!nuxtApp.isHydrating) { return () => {} }

packages/nuxt/src/app/plugins/payload.client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ export default defineNuxtPlugin((nuxtApp) => {
2020
if (to.path === from.path) { return }
2121
const payload = await loadPayload(to.path)
2222
if (!payload) { return }
23-
Object.assign(nuxtApp.payload.data, payload.data)
23+
Object.assign(nuxtApp.static.data, payload.data)
2424
})
2525
})

0 commit comments

Comments
 (0)