index.vue
pagenuxt.config.js
mode
to spa
(so the middleware can only get called on the client)layouts/error.vue
page with the following content:<template></template>
<script>
export default {
middleware() {
console.log("Called!");
}
};
</script>
"Called!" will log to the browser console
Nothing gets logged to the browser console
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>
where did you apply this error layout?
@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.
This issue remains unresolved on my end.
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:
Issues that are labeled as 馃晲Pending
will not be automatically marked as stale.
Not stale, and the reproduction steps are noted in the original issue. This still occurs in the latest version.
This issue is still occurring the latest version of Nuxt.js
Bumped into the same issue, would be nice if middleware or asyncData worked.
I'm running into the same issue. No middleware runs on 'layouts/error.vue'
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,
},
},
2) 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();
}
});
3) 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.
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()
.
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`)
}
Most helpful comment
Bumped into the same issue, would be nice if middleware or asyncData worked.