Skip to content Skip to sidebar Skip to footer

Nuxtjs Set Cookie In Middleware

I'm building a nuxtjs app and try to set a cookie from a global middleware. I found this contribution on GitHub which shows a method to do this. So I implemented my middleware like

Solution 1:

You should use cookie-universal-nuxt.

Add this in your module section in nuxt.config.js: ['cookie-universal-nuxt', { alias: 'cookiz' }],

You can use it directly in the store with nuxtServerInit:

asyncnuxtServerInit({ commit, state, dispatch },
    { app, store, route, req, res, error, redirect }
) {
    app.$cookiz.set('lang', route.query.lang)
})

Or in a middleware:

exportdefaultfunction ({ app, res, query }) {
  if (query.lang) {
    app.$cookiz.set('lang', query.lang)
  }
}

Post a Comment for "Nuxtjs Set Cookie In Middleware"