From aaf924f49dda07e338a3b8e9df2592c7a4bff5d6 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 7 Jul 2017 02:03:11 +0430 Subject: [PATCH] fix(asyncData): keep results in ssrContext --- lib/app/server.js | 5 ++++- lib/app/utils.js | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/app/server.js b/lib/app/server.js index dbf770840c..ee38ab566a 100644 --- a/lib/app/server.js +++ b/lib/app/server.js @@ -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) diff --git a/lib/app/utils.js b/lib/app/utils.js index edd88defb2..8260b383d4 100644 --- a/lib/app/utils.js +++ b/lib/app/utils.js @@ -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) {