diff --git a/examples/vuex-store/README.md b/examples/vuex-store/README.md index 9fdf8a9d2c..0932cd9eb0 100644 --- a/examples/vuex-store/README.md +++ b/examples/vuex-store/README.md @@ -73,3 +73,19 @@ export default { ## Context To see the list of available keys in `context`, take a look at [this documentation](https://github.com/nuxt/nuxt.js/tree/master/examples/async-data#context). + +## Action `nuxtServerInit` + +If you define the action `nuxtServerInit` in your store, nuxt.js will call it with the context, it can be useful when having some data on the server you want to give to the client-side, for example, the authenticated user: +```js +// store/index.js +actions: { + nuxtServerInit ({ commit }, { req }) { + if (req.authUser) { + commit('user', req.authUser) + } + } +} +``` + +The context given to `nuxtServerInit` is the same as the `data` of `fetch` method except `context.redirect()` and `context.error()` are omitted. diff --git a/lib/app/server.js b/lib/app/server.js index f66462ea92..a40ae2dc07 100644 --- a/lib/app/server.js +++ b/lib/app/server.js @@ -3,7 +3,7 @@ const debug = require('debug')('nuxt:render') import Vue from 'vue' import { stringify } from 'querystring' -import { pick } from 'lodash' +import { omit } from 'lodash' import { app, router<%= (store ? ', store' : '') %> } from './index' import { getMatchedComponents, getContext, urlJoin } from './utils' @@ -50,7 +50,7 @@ export default context => { <%= (isDev ? 'const s = isDev && Date.now()' : '') %> <% if (store) { %> - let promise = (store._actions && store._actions.nuxtServerInit ? store.dispatch('nuxtServerInit', pick(getContext(context), 'req', 'res', 'params', 'query')) : null) + let promise = (store._actions && store._actions.nuxtServerInit ? store.dispatch('nuxtServerInit', omit(getContext(context), 'redirect', 'error')) : null) if (!(promise instanceof Promise)) promise = Promise.resolve() <% } else { %> let promise = Promise.resolve() diff --git a/package.json b/package.json index 122068f2bb..308a1dcb36 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nuxt", - "version": "0.4.5", + "version": "0.4.6", "description": "A minimalistic framework for server-rendered Vue.js applications (inspired by Next.js)", "main": "index.js", "license": "MIT",