Architecture
Storybook Astro consists of two packages that work together to render Astro components in Storybook.
Two-package system
Section titled “Two-package system”@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
.astrocomponents 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 setupsrc/middleware.ts— Astro Container setup and SSR handler, includespatchCreateAstroCompatfor compiler bridgingsrc/integrations/— Integration adapters for each supported frameworksrc/viteStorybookAstroMiddlewarePlugin.ts— Vite plugin for handling render requests (dev)src/vitePluginAstroBuildPrerender.ts— Pre-renders Astro stories at build timesrc/vitePluginAstroComponentMarker.ts— Patches Astro’s client-side.astrostubs (Astro 6+ and Astro 7’s Rust compiler)src/vitePluginAstroFonts.ts— Resolves Astro’s Font Provider API in Storybook’s SSR contextsrc/portable-stories.ts—composeStories/composeStoryfor testing
@storybook-astro/renderer (Client)
Section titled “@storybook-astro/renderer (Client)”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.rendereris 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 integrationsrc/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 usessrc/preset.ts— Client-side preview annotations
Data flow
Section titled “Data flow”Astro components (server-side rendered)
Section titled “Astro components (server-side rendered)”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)Framework components (delegated)
Section titled “Framework components (delegated)”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)Component detection
Section titled “Component detection”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.