Dev Mode Rendering
In dev mode (storybook dev), Astro components are rendered on-demand through a request cycle between the browser and the Vite dev server.
Step-by-step flow
Section titled “Step-by-step flow”-
Story definition — Stories import Astro components and define variations with different props and slots.
-
Component detection — The renderer checks imported components for the
isAstroComponentFactoryflag to identify Astro components. -
Server rendering — When an Astro component is detected, a render request is sent to the Vite dev server middleware via HMR (Hot Module Replacement). If the render takes longer than 200 ms, the canvas shows “Rendering story…”, in place of an emptied canvas or over the story it still shows. When Storybook tears a story down, the canvas keeps its height until the next render writes its markup or fails, so the content below it doesn’t collapse in between. If you leave a story while it’s still rendering, the renderer stops waiting and drops the response when it arrives. The server still finishes that render.
-
Container rendering — The middleware uses Astro’s Container API to render the component with the provided props and slots. The
patchCreateAstroCompatwrapper bridges the Astro compiler v2/v3 calling convention difference. -
HTML injection — The rendered HTML string is sent back to the client and injected into Storybook’s canvas.
-
Hydration — Client-side scripts are re-executed to add interactivity. This handles framework islands (e.g. React components inside Astro with
client:load) and Alpine.js directives. -
Style application — The rendered HTML is preceded by
<style>elements holding the scoped CSS Astro compiled for every component the story rendered, one per<style>block, so styles are scoped the way Astro scopes them, and the stylesheets imported by those components and the modules they import, as an Astro page ships them. The renderer moves them into the preview’s<head>before injecting the HTML, and removes them, along with the story’s markup, when Storybook removes the story. Stories that ship the same CSS, such as a Docs page’s stories, share one copy of it, which stays until the last story using it is removed. -
Framework delegation — For non-Astro framework components (React, Solid, Vue, etc.), the renderer delegates directly to the framework-specific
renderToCanvasbefore callingstoryFn(). This ordering is critical — it lets reactive frameworks like Solid manage their own rendering lifecycle without orphaned effects. -
HMR updates — Changes to components, and to the stylesheets their frontmatter imports, trigger re-renders. The Vite HMR event listeners in
render.tsxhandle updates while preserving state when possible.
The Vite HMR channel
Section titled “The Vite HMR channel”Communication between the browser and server uses Vite’s built-in HMR channel:
- Client side (
render.tsx): Sendsastro:render:requestevents with the component module ID, props, and slots - Server side (
viteStorybookAstroMiddlewarePlugin.ts): Listens for requests, invokes the Container API, and responds withastro:render:responsecontaining the rendered HTML
This approach avoids additional HTTP endpoints and reuses Vite’s existing WebSocket connection.