mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-29 23:02:42 +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()
|
||||
// Error function
|
||||
context.error = _app.$options._nuxt.error.bind(_app)
|
||||
// Keep asyncData for each matched component in context
|
||||
context.asyncData = {}
|
||||
|
||||
<%= (isDev ? 'const s = isDev && Date.now()' : '') %>
|
||||
let ctx = getContext(context, app)
|
||||
@ -144,7 +146,8 @@ export default async (context) => {
|
||||
let promise = promisify(Component.options.asyncData.bind(_this), ctx)
|
||||
// Call asyncData(context)
|
||||
promise.then((asyncDataResult) => {
|
||||
applyAsyncData(Component, asyncDataResult)
|
||||
context.asyncData[Component.options.name] = asyncDataResult
|
||||
applyAsyncData(Component)
|
||||
return asyncDataResult
|
||||
})
|
||||
promises.push(promise)
|
||||
|
@ -5,8 +5,16 @@ const noopData = () => ({})
|
||||
|
||||
export function applyAsyncData (Component, asyncData = {}) {
|
||||
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 () {
|
||||
const data = ComponentData.call(this)
|
||||
if(this.$ssrContext) {
|
||||
asyncData = this.$ssrContext.asyncData[Component.options.name]
|
||||
}
|
||||
return { ...data, ...asyncData }
|
||||
}
|
||||
if (Component._Ctor && Component._Ctor.options) {
|
||||
|
Loading…
Reference in New Issue
Block a user