fix(asyncData): keep results in ssrContext

This commit is contained in:
Pooya Parsa 2017-07-07 02:03:11 +04:30
parent a2f62b3bbd
commit aaf924f49d
2 changed files with 12 additions and 1 deletions

View File

@ -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)

View File

@ -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) {