Skip to content

Dynamic Routing in Nuxt

Set up a catch-all route in the Nuxt project to render new stories dynamically.

Create a app/pages/[…slug].vue file to fetch all stories in the space.

app/pages/[...slug].vue
<script setup>
const slug = useRoute().params.slug;
const { story } = await useAsyncStoryblok(slug && slug.length > 0 ? slug.join("/") : "home", {
api: {
version: "draft",
},
});
</script>
<template>
<StoryblokComponent v-if="story" :blok="story.content" />
</template>

Get the slug from the current route, making an exception for the home story to be /.

Since a catch-all route resolves at runtime, Nuxt’s crawler can’t find links when it renders the website. To deploy in SSG mode, use the Content Delivery API’s links endpoint and add the routes to nitro.prerender.routes with the nitro:config hook.

Learn more about Prerendering in the Nuxt documentation.

Was this page helpful?

What went wrong?

This site uses reCAPTCHA and Google's Privacy Policy (opens in a new window).Terms of Service (opens in a new window) apply.