nuxt icon indicating copy to clipboard operation
nuxt copied to clipboard

Middleware doesn't run at all in "layouts/error.vue" page

Open sam3d opened this issue 6 years ago • 15 comments

Version

v2.3.4

Steps to reproduce

  1. Create a simple Nuxt app with only an index.vue page
  2. Set the nuxt.config.js mode to spa (so the middleware can only get called on the client)
  3. Create a layouts/error.vue page with the following content:
<template></template>

<script>
export default {
  middleware() {
    console.log("Called!");
  }
};
</script>
  1. Navigate to any page that isn't "/"

What is expected ?

"Called!" will log to the browser console

What is actually happening?

Nothing gets logged to the browser console

This bug report is available on Nuxt community (#c8455)

sam3d avatar Jan 12 '19 00:01 sam3d

Also, if this is expected behaviour, please could this potentially be changed? There are many use cases for wanting to apply middleware on the error page. Just one example I can think of is redirecting away from the page in the case of any error (without having to put it in the created hook):

<template></template>

<script>
export default {
  middleware({ redirect }) {
    redirect("/");
  }
};
</script>

sam3d avatar Jan 12 '19 15:01 sam3d

where did you apply this error layout?

angus96 avatar Jan 31 '19 04:01 angus96

@angus96 I'm not applying it anywhere. In nuxt, the error page is not meant to be applied.

https://nuxtjs.org/guide/views/#error-page

The error page is a page component which is always displayed when an error occurs (that does not happen while server-side rendering). Warning: Though this file is placed in the layouts folder, it should be treated as a page. You can customize the error page by adding a layouts/error.vue file.

sam3d avatar Jan 31 '19 13:01 sam3d

This issue remains unresolved on my end.

sam3d avatar Feb 11 '19 01:02 sam3d

Thanks for your contribution to Nuxt.js! This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you would like this issue to remain open:

  1. Verify that you can still reproduce the issue in the latest version of nuxt-edge
  2. Comment the steps to reproduce it

Issues that are labeled as 🕐Pending will not be automatically marked as stale.

stale[bot] avatar Apr 20 '19 11:04 stale[bot]

Not stale, and the reproduction steps are noted in the original issue. This still occurs in the latest version.

sam3d avatar Apr 20 '19 11:04 sam3d

This issue is still occurring the latest version of Nuxt.js

sam3d avatar Aug 14 '19 07:08 sam3d

Bumped into the same issue, would be nice if middleware or asyncData worked.

ParallelUniv3rse avatar Sep 13 '19 12:09 ParallelUniv3rse

I'm running into the same issue. No middleware runs on 'layouts/error.vue'

konsvasi avatar Jan 16 '20 08:01 konsvasi

Bumping into this issue months later still. For the ones struggling - Last time around I've overcome the issue by:

  1. Creating a catch all route with custom middleware meta:
  {
      path: '*',
      meta: {
        middleware: myMiddlewareFunc,
      },
    },
  1. Rergistered a beforeEach hook on the router:
router.beforeEach((to, from, next) => {
    if (to.meta.middleware) {
      const middleware = [].concat(to.meta.middleware);

      // mock a fake context because the real one is not available
      const context = {
        from,
        redirect: (statusCode, url) => next(url),
        router,
        route: to,
        error: () => next(),
      };

      // support only single middleware function for now.
      return middleware[0](context);
    } else {
      next();
    }
  });
  1. Used myMiddlewareFunc as normal (middleware/myMiddlewareFunc.js)
export default function ({ route, redirect, error }, message) {
  // do whatever
}

I haven't really given a thought to how this might work if you're using the built-in router based on filesystem and not an external one. This was enough to intercept the error render, redirect if there was a redirect defined for the url in the cms or fall through to the error page otherwise.

ParallelUniv3rse avatar May 28 '20 10:05 ParallelUniv3rse

I am also having this issue on the latest version. Neither middleware nor asyncData run in my file layouts/error.vue. I would like to redirect depending on the error status code, and currently the only way I can figure out how is by using beforeMount() or mounted().

justinwride avatar Jun 24 '20 21:06 justinwride

Confirming this remains an issue in v2.14.6. Luckily I'm just adding some light custom tracking so was able to get away with using the beforeMount hook.

  beforeMount() {
    this.$trackEvent(`${this.error.statusCode} displayed`)
  }

ndemasie avatar Nov 27 '20 16:11 ndemasie

It is quite confusing that layouts/error.vue is actually a "Page". While being assumed as a page it:

  • does not share page specific nuxt hooks, like middleware and asyncData thus context.redirect can't be accessed;
  • it supports a layout property while being located inside layouts folder, so for example if I want to have an error layout for error page, the error "page" should reference itself
  • beforeRouteEnter, beforeRouteLeave are not being called Why can't nuxt conventionally support pages/error.vue with full page capabilities?

Gvade avatar Feb 19 '21 10:02 Gvade

I have the same issue.

Larsmyrup avatar Apr 24 '21 17:04 Larsmyrup

same here, issue still in progress?

OMantel1 avatar May 20 '22 09:05 OMantel1

export default async function ({ store, route, error}) { const data = await store.dispatch('services/getByCode', route.params.detail) if(!data.code) { error({ statusCode: 404, message: 'Post not found' }) } }

Morzpx avatar Nov 11 '22 09:11 Morzpx