Nuxt.js: store initialization in plugin

Created on 24 Mar 2017  Â·  3Comments  Â·  Source: nuxt/nuxt.js

it appears there is some asynchronous activity happening with loading the SSR state into the store. In my sentry/raven plugin, I need to set UserContext with the store. However, due to some delay on the store being available after import store from ~store, access to the store immediately after results in a null state.

I was able to work around this by accessing the state from within the window.onNuxtReady callback but I soon ran into another issue. In the case of sentry, if an error occurs during initializing of nuxt, the user context is lost - as this is set post nuxt being ready.

The last workaround was reading directly from window.__NUXT__.state. This is what I'm sticking to now. Would like to hear your thoughts on if this is wrong and why the ~store seems to not be immediately synced to the window.__NUXT__.state when imported.

sentry.js: setUserContext

if (window.__NUXT__.state.current_user) {
  Raven.setUserContext( window.__NUXT__.state.current_user )
}

This feature request is available on Nuxt.js community (#c372)
available soon enhancement

Most helpful comment

Hi @uptownhr

It's an excellent question and to be honest, I had no example why it was placed there.

I just made a commit to replace the store before importing the plugins (nuxt.config.js): https://github.com/nuxt/nuxt.js/commit/4034801fc9f6d79b13568b2137602b52dbfd3b7f

In the upcoming release (0.10), you will be able to do:

nuxt.config.js:

module.exports = {
  plugins: [{
    ssr: false, // only included on client-side
    src: '~plugins/sentry'
  }]
}

plugins/sentry.js

import Raven from 'raven-js'
import store from '~store'

// Use store.state.current_user will work here
Raven.config('https://<key>@sentry.io/<project>').install()

I keep the thread open until the release is out!

All 3 comments

Hi @uptownhr

It's an excellent question and to be honest, I had no example why it was placed there.

I just made a commit to replace the store before importing the plugins (nuxt.config.js): https://github.com/nuxt/nuxt.js/commit/4034801fc9f6d79b13568b2137602b52dbfd3b7f

In the upcoming release (0.10), you will be able to do:

nuxt.config.js:

module.exports = {
  plugins: [{
    ssr: false, // only included on client-side
    src: '~plugins/sentry'
  }]
}

plugins/sentry.js

import Raven from 'raven-js'
import store from '~store'

// Use store.state.current_user will work here
Raven.config('https://<key>@sentry.io/<project>').install()

I keep the thread open until the release is out!

The 0.10 release it out ✋

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mikekidder picture mikekidder  Â·  3Comments

ghost picture ghost  Â·  3Comments

msudgh picture msudgh  Â·  3Comments

bimohxh picture bimohxh  Â·  3Comments

jaredreich picture jaredreich  Â·  3Comments