Skip to content

Visual Preview in Astro

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

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/.

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

Terminal window
npm install vite-plugin-mkcert --save-dev

Add the plugin to your Astro config file.

astro.config.mjs
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.

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
---
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
---
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.

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
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
---
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.

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.

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.