---
title: Dynamic Routing in Astro
description: Learn how to enable dynamic rendering of new stories by setting up a catch-all route in Astro.
url: https://www.storyblok.com/docs/quickstarts/astro/dynamic-routing
---

# Dynamic Routing in Astro

To render new stories dynamically, set up a catch-all route in the Astro project.

## Fetch and render a story dynamically

Create a `src/pages/[...slug].astro` file to fetch all stories in the space.

src/pages/\[...slug\].astro

```astro
---
import { getPayload, StoryblokPreview } from "@storyblok/astro";
import { apiClient, StoryblokBlock } from "../storyblok";
import Layout from "../layouts/Layout.astro";

const { slug } = Astro.params;

const payload = await getPayload({ locals: Astro.locals });

const story =
  payload.story ??
  (
    await apiClient.stories.get(slug || "home", {
      query: {
        version: "draft",
      },
    })
  ).data?.story;

if (!story) {
  return new Response(null, { status: 404 });
}
---

<Layout>
  <StoryblokBlock block={story.content} />
  <StoryblokPreview liveUpdate />
</Layout>
```

Get the `slug` from the current route parameters, making an exception for the home story to be `/`. If no story matches the slug, the page returns a 404 response.

With this approach, your project can automatically handle new stories you add to your space.

> [!TIP]
> Remove the `index.astro` file from the project.

> [!NOTE]
> To deploy in SSG mode, define the dynamic routes manually. Use `apiClient.links.list()` to call the [links endpoint](/docs/api/content-delivery/v2/links/retrieve-multiple-links) of the Content Delivery API and Astro’s `getStaticPaths()` to provide all routes. On routes that Astro renders at build time, `StoryblokPreview` reloads the page on save or publish, but `liveUpdate` doesn’t work: it requires on-demand rendering.
> 
> Learn more about [dynamic routes in the Astro documentation](https://docs.astro.build/en/guides/routing/#dynamic-routes).

## Related resources

[Content Delivery API: Retrieve a Single Story](/docs/api/content-delivery/v2/stories/retrieve-a-single-story)

[Content Delivery API: Retrieve Multiple Stories](/docs/api/content-delivery/v2/stories/retrieve-multiple-stories)

## Pagination

-   [Previous: Visual Preview in Astro](/docs/quickstarts/astro/visual-preview)
-   [Next: Content Modeling in Astro](/docs/quickstarts/astro/content-modeling)
