diff --git a/lib/app/server.js b/lib/app/server.js index 65d7cb2c55..f66462ea92 100644 --- a/lib/app/server.js +++ b/lib/app/server.js @@ -49,34 +49,45 @@ export default context => { context.error = _app.error.bind(_app) <%= (isDev ? 'const s = isDev && Date.now()' : '') %> - // Call data & fecth hooks on components matched by the route. - let Components = getMatchedComponents(context.route) - if (!Components.length) { - context.nuxt.error = _app.error({ statusCode: 404, message: 'This page could not be found.', url: context.route.path }) - <%= (store ? 'context.nuxt.state = store.state' : '') %> - return Promise.resolve(_app) - } - return Promise.all(Components.map((Component) => { - let promises = [] - if (Component.data && typeof Component.data === 'function') { - Component._data = Component.data - var promise = Component.data(getContext(context)) - if (!(promise instanceof Promise)) promise = Promise.resolve(promise) - promise.then((data) => { - Component.data = () => data - if (Component._Ctor) { - Component._Ctor = Vue.extend(Component) - } - }) - promises.push(promise) - } else { - promises.push(null) + <% if (store) { %> + let promise = (store._actions && store._actions.nuxtServerInit ? store.dispatch('nuxtServerInit', pick(getContext(context), 'req', 'res', 'params', 'query')) : null) + if (!(promise instanceof Promise)) promise = Promise.resolve() + <% } else { %> + let promise = Promise.resolve() + <% } %> + return promise + .then(() => { + // Call data & fecth hooks on components matched by the route. + let Components = getMatchedComponents(context.route) + if (!Components.length) { + context.nuxt.error = _app.error({ statusCode: 404, message: 'This page could not be found.', url: context.route.path }) + <%= (store ? 'context.nuxt.state = store.state' : '') %> + return Promise.resolve(_app) } - if (Component.fetch) { - promises.push(Component.fetch(getContext(context))) - } - return Promise.all(promises) - })) + return Promise.all(Components.map((Component) => { + let promises = [] + if (!Component.options) { + Component = Vue.extend(Component) + Component._Ctor = Component + } + if (Component.options.data && typeof Component.options.data === 'function') { + Component._data = Component.options.data + var promise = Component._data(getContext(context)) + if (!(promise instanceof Promise)) promise = Promise.resolve(promise) + promise.then((data) => { + Component.options.data = () => data + Component._Ctor.options.data = Component.options.data + }) + promises.push(promise) + } else { + promises.push(null) + } + if (Component.options.fetch) { + promises.push(Component.options.fetch(getContext(context))) + } + return Promise.all(promises) + })) + }) .then((res) => { <% if (isDev) { %> debug('Data fetching ' + context.req.url + ': ' + (Date.now() - s) + 'ms') diff --git a/package.json b/package.json index 5c0f8b1552..122068f2bb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nuxt", - "version": "0.4.4", + "version": "0.4.5", "description": "A minimalistic framework for server-rendered Vue.js applications (inspired by Next.js)", "main": "index.js", "license": "MIT",