nuxt: Middleware doesn't run at all in "layouts/error.vue" page
Version
Steps to reproduce
- Create a simple Nuxt app with only an
index.vuepage - Set the
nuxt.config.jsmodetospa(so the middleware can only get called on the client) - Create a
layouts/error.vuepage with the following content:
<template></template>
<script>
export default {
middleware() {
console.log("Called!");
}
};
</script>
- 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
<div align="right">This bug report is available on Nuxt community (#c8455)</div>About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 17
- Comments: 16
Bumped into the same issue, would be nice if middleware or asyncData worked.
It is quite confusing that
layouts/error.vueis actually a “Page”. While being assumed as a page it:middlewareandasyncDatathuscontext.redirectcan’t be accessed;layoutproperty while being located inside layouts folder, so for example if I want to have anerrorlayout for error page, the error “page” should reference itselfbeforeRouteEnter,beforeRouteLeaveare not being called Why can’t nuxt conventionally supportpages/error.vuewith full page capabilities?@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
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’ }) } }