Static GitHub Issues

[542] asyncData executed if middleware redirects (client-only)

prev: fetching the store in a middleware
next: Converting circular structure to JSON

Consider this example:

secret.vue

<template>
  <div>Hello, {{user.email}}!</div>
</template>

<script>
import Vue from 'vue'

export default {
  middleware: 'authenticated',
  async asyncData ({ req }) {
    // executes even after the middleware redirects
    let { body } = await Vue.http.get('user')
    return {
      title: 'Secret Page',
      user: body
    }
  },
  head () {
    return {
      title: this.title
    }
  }
}
</script>

<style>
</style>

middleware/authenticated.js

export default function ({ store, redirect }) {
  if (!store.getters.isAuthenticated) {
    return redirect('/auth/sign-in')
  }
}
<!--cmty--><!--cmty_prevent_hook--><div align="right"><sub><em>This feature request is available on <a href="https://nuxtjs.cmty.io">Nuxt.js</a> community (<a href="https://nuxtjs.cmty.io/nuxt/nuxt.js/issues/c465">#c465</a>)</em></sub></div>