N
Nuxt3mo ago
kb

Error pages

Am I doing this right? It used to be that we had error.vue in layouts but now were on nuxt 3, ive made error.vue in the top level, and then ive got
<template>
<NuxtLayout />
<p>{{ error.statusCode }} </p>
// etc
<template>
<NuxtLayout />
<p>{{ error.statusCode }} </p>
// etc
in that file, so it uses the default layout - but only the layout renders. If i remove the layout part, i see my error code and stuff
5 Replies
kapa.ai
kapa.ai3mo ago
Hi @kb. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the information provided in the knowledge sources, it seems you're on the right track, but there's a small adjustment needed to make your error page work correctly with a layout in Nuxt 3. Let me explain: 1. You're correct that in Nuxt 3, the error.vue file should be placed in the root directory of your project, not in the layouts folder. This is the correct approach as mentioned in the Nuxt documentation (https://nuxt.com/docs/getting-started/error-handling#error-page). 2. The issue you're experiencing where only the layout renders when you use <NuxtLayout /> is a common problem. The solution is to use the <NuxtLayout> component as a wrapper for your error content, not as a standalone component. Here's how you should structure your error.vue file:
<template>
<NuxtLayout name="default">
<div>
<h1>{{ error.statusCode }}</h1>
<p>{{ error.message }}</p>
<!-- Add more error content here -->
</div>
</NuxtLayout>
</template>

<script setup lang="ts">
import type { NuxtError } from '#app'

const props = defineProps({
error: Object as () => NuxtError
})
</script>
<template>
<NuxtLayout name="default">
<div>
<h1>{{ error.statusCode }}</h1>
<p>{{ error.message }}</p>
<!-- Add more error content here -->
</div>
</NuxtLayout>
</template>

<script setup lang="ts">
import type { NuxtError } from '#app'

const props = defineProps({
error: Object as () => NuxtError
})
</script>
This approach is described in the Nuxt documentation on layouts (https://nuxt.com/docs/migration/pages-and-layouts#layouts), which states: "If you want to ensure that this page uses a layout, you can use <NuxtLayout> directly within error.vue" 3. Make sure your default layout (or any layout you want to use) has a <slot /> instead of <NuxtPage />. This is crucial for the error page to render correctly within the layout as mentioned in a GitHub issue comment (https://github.com/nuxt/nuxt/issues/29284). By making these adjustments, you should be able to see both your layout and the error content rendered correctly. Remember, if you need to customize the layout specifically for the error page, you can create a separate layout file (e.g., layouts/error.vue) and specify it in the name prop of <NuxtLayout> in your error.vue file. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3mo ago
<@310160452221140996> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
kb
kbOP3mo ago
@kapa.ai I did that already and it doesnt work, it just shows the layout. the docs also say You can use <NuxtLayout /> component to activate the default layout on app.vue or error.vue. so 🤷‍♂️
kapa.ai
kapa.ai3mo ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- I apologize for the confusion. You're right to point out that discrepancy, and I appreciate you bringing this to my attention. Let's take a closer look at the issue based on the most recent and relevant information from the knowledge sources. The problem you're experiencing is actually a known issue that has been discussed in the Nuxt community. According to a Stack Overflow post (https://stackoverflow.com/questions/78749907), the solution is to modify your layout file, not the error.vue file. Here's what you need to do: 1. In your layout file (e.g., layouts/default.vue), replace <NuxtPage /> with <slot />. This is crucial because error.vue is not considered a Nuxt page. Here's an example of how your layout file should look:
<template>
<div id="root">
<div class="main-layout pb-10">
<CustomHeader />
<main class="main-container">
<slot />
</main>
<CustomFooter/>
</div>
</div>
</template>
<template>
<div id="root">
<div class="main-layout pb-10">
<CustomHeader />
<main class="main-container">
<slot />
</main>
<CustomFooter/>
</div>
</div>
</template>
2. In your error.vue file, you can then use the <NuxtLayout> component as you were doing before:
<template>
<NuxtLayout name="default">
<div class="error-container">
<h1>Error {{ error.statusCode || 'Unknown' }}</h1>
<p>{{ error.message || 'An unexpected error occurred' }}</p>
<!-- Add more error content here -->
</div>
</NuxtLayout>
</template>

<script setup lang="ts">
import type { NuxtError } from '#app'

const props = defineProps({
error: Object as () => NuxtError
})
</script>
<template>
<NuxtLayout name="default">
<div class="error-container">
<h1>Error {{ error.statusCode || 'Unknown' }}</h1>
<p>{{ error.message || 'An unexpected error occurred' }}</p>
<!-- Add more error content here -->
</div>
</NuxtLayout>
</template>

<script setup lang="ts">
import type { NuxtError } from '#app'

const props = defineProps({
error: Object as () => NuxtError
})
</script>
This approach should allow your error content to appear within the specified layout. The discrepancy in the documentation you mentioned highlights a point of confusion in the Nuxt community. It's possible that the documentation needs to be updated to reflect the correct implementation. If this solution doesn't work for you, it might be worth reporting the issue to the Nuxt team for clarification. Github comment on layouts (https://github.com/nuxt/nuxt/issues/29284) also confirms this approach, stating: "Layouts should not have <NuxtPage> inside - instead you use <slot />" I apologize again for any confusion in my previous response. Thank you for your patience as we worked through this issue. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3mo ago
<@310160452221140996> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?