DEV Community

Cover image for Quick Tip: Nuxt & Storyblok Error Handling (e.g. 404)
Anton Reindl
Anton Reindl

Posted on

Quick Tip: Nuxt & Storyblok Error Handling (e.g. 404)

Welcome to my new Quick Tip series:

Today we are looking at Error Handling when building websites with Nuxt and Storyblok as CMS. If you haven't tried the two tools, go check out one of the awesome tutorials. It's a perfect match for all your projects.

Today we are looking into one specific use case:

  1. Build a blog post detail page with a dynamic slug in the URL param
  2. Fetch the content from Storyblok CMS
  3. If the content cannot be found (Storyblok returns 404), Nuxt should show an error Page
  4. The solution must work with Server-Side-Rendering and Server-Side-Generation

Solution

<template>
    <YourArticleDetailPage :story="story" />
</template>

<script lang="ts" setup>

// Get the Slug from the Route
const route = useRoute()
const slug = route.params.slug

// Load the Content from /blog
const story = await useAsyncStoryblok(`blog/${slug}`)


// Handle the Error
if (!story.value) {
    throw createError({
        statusCode: 404,
        statusMessage: 'Page Not Found',
        fatal: true,
    })
}
</script>

Enter fullscreen mode Exit fullscreen mode

Noteworthy:

The most important part of the solution is fatal: true in the error object. This will trigger a full-screen error page in Nuxt and redirect the user to the error page.

Now you can go ahead and create the error page with error.vue - as described here. Remember: the error.vue must be kept in root, not /pages

That's it for today's quick tip.

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Cloudinary image

Video API: manage, encode, and optimize for any device, channel or network condition. Deliver branded video experiences in minutes and get deep engagement insights.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay