Nuxt.js: Layout not applied when middleware redirects to a page with different layout

Created on 15 Mar 2017  Â·  9Comments  Â·  Source: nuxt/nuxt.js

Hi,

I have an application with an authentication middleware that redirects user to a login page whenever she tries to access a restricted page. When I click on a link for a first time (from the default layout) then I am redirected to login page with a correct layout, but when I click on the link again (from the login layout), then I am redirected to login page with default layout applied, but it should use login layout.

Simplified version of middleware/auth.js:

export default function ({ route, redirect }) {
  if (route.name === 'secret') {
    redirect('/login');
  }
});

layout/default.vue:

<template>
  <div>
    <p>This is default layout</p>
    <router-link to="secret">Secret</router-link>
  </div>
</template>

layout/login.vue:

<template>
  <div>
    <p>This is login layout</p>
    <router-link to="secret">Secret</router-link>
  </div>
</template>

pages/login.js:

<template>
  <div>This is login page</div>
</template>

<script>
  export default {
    layout: 'login'
  };
</script>

pages/secret.vue:

<template>
  <div>
    <!-- uses default layout -->
    <p>Secret</p>
  </div>
</template>

pages/index.vue:

<template>
  <div>
    <p>Index</p>
  </div>
</template>

This bug report is available on Nuxt.js community (#c339)
available soon bug-confirmed

Most helpful comment

The 0.10 release it out ✋

All 9 comments

Here is a screencast describing the current behavior: https://www.screencast.com/t/79ZUqxAXc59H

I'm not called middleware when redirect to pages with another layout from error layout

Yes, you are right that seems to be an another bug - middleware is not called when navigating from the error layout to restricted page using nuxt-link

Hi @JiriChara @skilFullGH

The middleware not called when navigating from the error layout will be fixed in the next release.

I could not reproduce your error @JiriChara

Do you mind forking https://gomix.com/#!/project/nuxt-hello-world and trying to reproduce your behavior?

I am not able to make layouts to work here: https://glitch.com/edit/#!/brainy-apparatus

However here is a repo where you can reproduce the bug: https://github.com/JiriChara/nuxt-middleware Just click on the link to the secret page 2 times. Login page will have a wrong layout then

Thank you for the reproduction @JiriChara, indeed, it does not work as expected in the last release.

Good news, I tried your example with the 0.10 and everything works as expected 🎊

screenflow

I'll keep this issue open until the 0.10 release is out!

Awesome! Can't wait for the next release :tada:

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

VincentLoy picture VincentLoy  Â·  3Comments

maicong picture maicong  Â·  3Comments

gary149 picture gary149  Â·  3Comments

pehbehbeh picture pehbehbeh  Â·  3Comments

lazycrazy picture lazycrazy  Â·  3Comments