mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-30 15:22:39 +00:00
fix(asyncData): keep results in ssrContext
This commit is contained in:
parent
a2f62b3bbd
commit
aaf924f49d
@ -53,6 +53,8 @@ export default async (context) => {
|
|||||||
context.meta = _app.$meta()
|
context.meta = _app.$meta()
|
||||||
// Error function
|
// Error function
|
||||||
context.error = _app.$options._nuxt.error.bind(_app)
|
context.error = _app.$options._nuxt.error.bind(_app)
|
||||||
|
// Keep asyncData for each matched component in context
|
||||||
|
context.asyncData = {}
|
||||||
|
|
||||||
<%= (isDev ? 'const s = isDev && Date.now()' : '') %>
|
<%= (isDev ? 'const s = isDev && Date.now()' : '') %>
|
||||||
let ctx = getContext(context, app)
|
let ctx = getContext(context, app)
|
||||||
@ -144,7 +146,8 @@ export default async (context) => {
|
|||||||
let promise = promisify(Component.options.asyncData.bind(_this), ctx)
|
let promise = promisify(Component.options.asyncData.bind(_this), ctx)
|
||||||
// Call asyncData(context)
|
// Call asyncData(context)
|
||||||
promise.then((asyncDataResult) => {
|
promise.then((asyncDataResult) => {
|
||||||
applyAsyncData(Component, asyncDataResult)
|
context.asyncData[Component.options.name] = asyncDataResult
|
||||||
|
applyAsyncData(Component)
|
||||||
return asyncDataResult
|
return asyncDataResult
|
||||||
})
|
})
|
||||||
promises.push(promise)
|
promises.push(promise)
|
||||||
|
@ -5,8 +5,16 @@ const noopData = () => ({})
|
|||||||
|
|
||||||
export function applyAsyncData (Component, asyncData = {}) {
|
export function applyAsyncData (Component, asyncData = {}) {
|
||||||
const ComponentData = Component.options.data || noopData
|
const ComponentData = Component.options.data || noopData
|
||||||
|
// Prevent calling this method for each request on SSR context
|
||||||
|
if(!asyncData && Component.options.hasAsyncData) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
Component.options.hasAsyncData = true
|
||||||
Component.options.data = function () {
|
Component.options.data = function () {
|
||||||
const data = ComponentData.call(this)
|
const data = ComponentData.call(this)
|
||||||
|
if(this.$ssrContext) {
|
||||||
|
asyncData = this.$ssrContext.asyncData[Component.options.name]
|
||||||
|
}
|
||||||
return { ...data, ...asyncData }
|
return { ...data, ...asyncData }
|
||||||
}
|
}
|
||||||
if (Component._Ctor && Component._Ctor.options) {
|
if (Component._Ctor && Component._Ctor.options) {
|
||||||
|
Loading…
Reference in New Issue
Block a user