mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-26 23:52:06 +00:00
Add nuxtServerInit action in the store
This commit is contained in:
parent
2677afb46f
commit
c9250b4d07
@ -49,34 +49,45 @@ export default context => {
|
|||||||
context.error = _app.error.bind(_app)
|
context.error = _app.error.bind(_app)
|
||||||
|
|
||||||
<%= (isDev ? 'const s = isDev && Date.now()' : '') %>
|
<%= (isDev ? 'const s = isDev && Date.now()' : '') %>
|
||||||
// Call data & fecth hooks on components matched by the route.
|
<% if (store) { %>
|
||||||
let Components = getMatchedComponents(context.route)
|
let promise = (store._actions && store._actions.nuxtServerInit ? store.dispatch('nuxtServerInit', pick(getContext(context), 'req', 'res', 'params', 'query')) : null)
|
||||||
if (!Components.length) {
|
if (!(promise instanceof Promise)) promise = Promise.resolve()
|
||||||
context.nuxt.error = _app.error({ statusCode: 404, message: 'This page could not be found.', url: context.route.path })
|
<% } else { %>
|
||||||
<%= (store ? 'context.nuxt.state = store.state' : '') %>
|
let promise = Promise.resolve()
|
||||||
return Promise.resolve(_app)
|
<% } %>
|
||||||
}
|
return promise
|
||||||
return Promise.all(Components.map((Component) => {
|
.then(() => {
|
||||||
let promises = []
|
// Call data & fecth hooks on components matched by the route.
|
||||||
if (Component.data && typeof Component.data === 'function') {
|
let Components = getMatchedComponents(context.route)
|
||||||
Component._data = Component.data
|
if (!Components.length) {
|
||||||
var promise = Component.data(getContext(context))
|
context.nuxt.error = _app.error({ statusCode: 404, message: 'This page could not be found.', url: context.route.path })
|
||||||
if (!(promise instanceof Promise)) promise = Promise.resolve(promise)
|
<%= (store ? 'context.nuxt.state = store.state' : '') %>
|
||||||
promise.then((data) => {
|
return Promise.resolve(_app)
|
||||||
Component.data = () => data
|
|
||||||
if (Component._Ctor) {
|
|
||||||
Component._Ctor = Vue.extend(Component)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
promises.push(promise)
|
|
||||||
} else {
|
|
||||||
promises.push(null)
|
|
||||||
}
|
}
|
||||||
if (Component.fetch) {
|
return Promise.all(Components.map((Component) => {
|
||||||
promises.push(Component.fetch(getContext(context)))
|
let promises = []
|
||||||
}
|
if (!Component.options) {
|
||||||
return Promise.all(promises)
|
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) => {
|
.then((res) => {
|
||||||
<% if (isDev) { %>
|
<% if (isDev) { %>
|
||||||
debug('Data fetching ' + context.req.url + ': ' + (Date.now() - s) + 'ms')
|
debug('Data fetching ' + context.req.url + ': ' + (Date.now() - s) + 'ms')
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "nuxt",
|
"name": "nuxt",
|
||||||
"version": "0.4.4",
|
"version": "0.4.5",
|
||||||
"description": "A minimalistic framework for server-rendered Vue.js applications (inspired by Next.js)",
|
"description": "A minimalistic framework for server-rendered Vue.js applications (inspired by Next.js)",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
Loading…
Reference in New Issue
Block a user