---
title: Visual Preview in Astro
description: Learn how to connect your Astro project to Storyblok’s Visual Editor for a seamless, intuitive content editing experience.
url: https://www.storyblok.com/docs/quickstarts/astro/visual-preview
---

# Visual Preview in Astro

Enhance your editorial and development experience by connecting your local project with Storyblok’s Visual Editor.

## Set the default environment

Open **Settings** → **Visual Editor** and set the default environment to the URL of the local development server, for example, Astro’s default `https://localhost:4321/`.

> [!WARNING]
> The preview area requires an `https` secure connection. Learn more in the [Visual Editor concept](/docs/concepts/visual-editor).

For Astro, install the `vite-plugin-mkcert` package.

```bash
npm install vite-plugin-mkcert --save-dev
```

Add the plugin to your Astro config file.

astro.config.mjs

```javascript
import { defineConfig } from "astro/config";
import mkcert from "vite-plugin-mkcert";

export default defineConfig({
  output: "server",
  vite: {
    plugins: [mkcert()],
  },
});
```

Restart the development server if necessary.

To render the home story correctly in the Visual Editor, select the **Config** section and type `/` in the **Real path** field.

You may have to open the URL in a separate browser tab and accept the certificate first.

## Bridge with the Visual Editor

`StoryblokBlock` passes an `editable` prop to every block component. Spread it on the root element of the component to connect it with its Storyblok counterpart.

src/components/Feature.astro

```astro
---
const { block } = Astro.props;
const { block, editable } = Astro.props;
---

<div class="feature">
<div class="feature" {...editable}>
  <span>{block.name}</span>
</div>
```

Repeat this step for the `Page`, `Grid`, and `Teaser` components.

Next, add the `StoryblokPreview` component to the `src/pages/index.astro` file. It loads the Storyblok Bridge, which connects the page to the Visual Editor and reloads it when you save or publish content.

src/pages/index.astro

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

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

const story = data?.story;

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

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

Now, select a component in the preview and its corresponding block opens up in the editor.

## Enable live preview in the Visual Editor

Live preview updates the page while you edit content in the Visual Editor.

While you edit, the Visual Editor sends the unsaved content to the page. To store this content in `Astro.locals`, create a `src/middleware.ts` file.

src/middleware.ts

```ts
import { sequence } from "astro:middleware";
import { storyblokPreviewMiddleware } from "@storyblok/astro";

export const onRequest = sequence(storyblokPreviewMiddleware);
```

To use the live preview data, update the `src/pages/index.astro` file.

src/pages/index.astro

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

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

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

// Use live preview data or fall back to the API
const story = payload.story ?? (await apiClient.stories.get("home", { query: { version: "draft" } })).data?.story;

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

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

The `getPayload` function provides access to live preview story data from `Astro.locals`. If a payload exists, the index page uses the story from the live preview. If a payload doesn’t exist, it falls back to the Content Delivery API and fetches the `home` story in draft version.

With the `liveUpdate` property, the `StoryblokPreview` component also updates the page while you type.

> [!NOTE]
> Learn more about [getPayload](/docs/libraries/js/astro-sdk#getpayload) and the available [StoryblokPreview](/docs/libraries/js/astro-sdk#storyblokpreview) options in the package reference.

## Deploy the preview environment

Make sure to fetch the `draft` version of the content and use a preview access token. To reflect content updates dynamically upon saving content, deploy in server-side rendering mode.

For the production environment, fetch the `published` version of the content and use a public access token.

> [!NOTE]
> Learn more about preview and production environments in this [tutorial](https://www.storyblok.com/tp/create-preview-production-environments-and-deploy).

## Related resources

[@storyblok/astro Package Reference](/docs/libraries/js/astro-sdk)

[Concept: Visual Editor](/docs/concepts/visual-editor)

[mkcert Homebrew Formula](https://formulae.brew.sh/formula/mkcert#default)

[mkcert GitHub Repository](https://github.com/FiloSottile/mkcert)

## Pagination

-   [Previous: Integrate Astro with Storyblok](/docs/quickstarts/astro)
-   [Next: Dynamic Routing in Astro](/docs/quickstarts/astro/dynamic-routing)
