2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【nuxtjs】middlewareからredirectするとnavigation guardに阻まれて遷移できない

Posted at

前提

  • nuxt.js(2.15.8)
    • SPAのアプリ
    • ログインページ以外は全ページに認証かけていた

やりたかったこと

  • アプリではAPIサーバとのやりとりにaxiosを使っている
  • APIサーバから503が返ってきたら、メンテナンスページを表示したかった
if (code === 503) {
  redirect('maintenance') // こんな感じのことをしたい
}

やったこと

  • maintenance.vueを作成
  • axiosのpluginで応答エラー503が返ってきたらメンテナンスページにリダイレクト(上記コードのイメージ)
  • デバッグ用にapi側は全レスポンスを503返すように設定

動かしてみた結果

chrome console
Redirected when going from "/login" to "/maintenance" via a navigation guard.

原因と解決方法

  • 原因
    • 単純な話で、メンテナンスページにも認証をかけてしまっていて、ログイン済みじゃないと表示できないようになっていた
    • なので、とあるページでAPIアクセス→メンテナンスページ→ログインページ→(APIアクセス)→メンテナンスページ... みたいな感じで無限ダイレクトみたいになってたっぽい
  • 解決
    • メンテナンスページで auth: falseを設定してやる
<script lang="ts">

export default defineComponent({
  auth: false, // <-- これ
  setup() {},
})
</script>

感想

  • 最初はnuxtの不具合みたいのかと思ったけど普通に設定ミスでした。。
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?