Skip to content

Architecture

Storybook Astro consists of two packages that work together to render Astro components in Storybook.

@storybook-astro/framework (Server/Framework)

Section titled “@storybook-astro/framework (Server/Framework)”

The core framework implementation that integrates Astro with Storybook’s build system:

  • Vite Plugin Integration — Configures Vite to handle .astro components during the Storybook build
  • Middleware Handler — Sets up an Astro Container that renders components server-side on demand
  • Framework Integrations — Manages UI framework renderers so Astro and framework components can be rendered in the same Storybook
  • Module Resolution — Handles special module resolution for Astro’s runtime

Key files:

  • src/preset.ts — Storybook framework configuration and Vite setup
  • src/middleware.ts — Astro Container setup and SSR handler, includes patchCreateAstroCompat for compiler bridging
  • src/integrations/ — Integration adapters for each supported framework
  • src/viteStorybookAstroMiddlewarePlugin.ts — Vite plugin for handling render requests (dev)
  • src/vitePluginAstroBuildPrerender.ts — Pre-renders Astro stories at build time
  • src/vitePluginAstroComponentMarker.ts — Patches Astro’s client-side .astro stubs (Astro 6+ and Astro 7’s Rust compiler)
  • src/vitePluginAstroFonts.ts — Resolves Astro’s Font Provider API in Storybook’s SSR context
  • src/portable-stories.ts — composeStories/composeStory for testing

This package is automatically installed as a dependency of @storybook-astro/framework — no separate installation is needed.

The client-side rendering package that manages how components display in Storybook’s preview:

  • Render Function — Determines how to render different component types
  • Communication Layer — Sends render requests from the browser to the Astro middleware via Vite’s HMR channel
  • Fallback Rendering — Delegates to framework-specific renderers when parameters.renderer is specified
  • Style Management — Keeps server-rendered stylesheet links in the iframe head. Astro’s scoped styles and the stylesheets imported by the components a story renders (and the modules they import) arrive as <style> elements in the rendered HTML, and the renderer moves them into the iframe head until Storybook removes the story. Stories that ship the same CSS share one copy, which stays until the last story using it is removed
  • Script Execution — Manages client-side scripts and hydration

Key files:

  • src/render.tsx — Main rendering logic and Canvas integration
  • src/storyStyles.ts — Moves each story’s scoped CSS and imported stylesheets into the iframe head and removes it, with the story’s markup, when Storybook removes the story, keeping CSS another story on the page still uses
  • src/preset.ts — Client-side preview annotations
Story File (.stories.jsx)
↓
@storybook-astro/renderer (render.tsx)
↓ [detects isAstroComponentFactory flag]
↓ [sends render request via Vite HMR]
@storybook-astro/framework (middleware.ts)
↓ [patchCreateAstroCompat wraps component]
↓ [Astro Container API renders to HTML]
@storybook-astro/renderer (render.tsx)
↓ [injects HTML into canvas]
↓ [scoped and imported-stylesheet <style> elements arrive with the HTML and move to the iframe head; executes client scripts]
Storybook Canvas (rendered component)
Story File (.stories.jsx)
↓
@storybook-astro/renderer (render.tsx)
↓ [checks parameters.renderer]
↓ [delegates to framework renderToCanvas BEFORE calling storyFn()]
Framework Renderer (e.g. @storybook/react-vite)
↓ [manages its own reactive root]
Storybook Canvas (rendered component)

Astro components are identified by the isAstroComponentFactory flag:

if (Component.isAstroComponentFactory) {
// Route to server-side rendering via Astro Container
}

Since Astro 6, the client-side Vite transform no longer sets this flag (Astro 7’s Rust compiler behaves the same way). The vitePluginAstroComponentMarker plugin patches the stub to restore it. See Version Compatibility for details.