How do you use Storybook to render via a Node.js server? #35091
Replies: 3 comments 1 reply
|
The error you're hitting ( Here's a working setup:
import type { StorybookConfig } from '@storybook/server-webpack5';
const config: StorybookConfig = {
stories: ['../stories/**/*.stories.@(json|yaml|yml)'],
framework: '@storybook/server-webpack5',
};
export default config;
export const parameters = {
server: {
url: 'http://localhost:3000/storybook_preview',
},
};Stories are defined in JSON (not JS) — e.g. {
"title": "Button",
"parameters": {
"server": { "id": "demo/button" }
}
}Your Node.js server then needs a route that maps the story app.get(/storybook_preview\/(.*)/, (req, res) => {
// req.params[0] = "demo/button"
// req.query = story args passed as query params
res.render(req.params[0], req.query); // render your .twig file here
});Storybook fetches HTML from There's a complete working example in the repo at To reply, just mention @dosu. Docs are dead. Just use Dosu. |
|
Provide guidance on creating a custom framework that sets server URL. Possibly mention that @storybook/server is not a framework; you need to use @storybook/server-webpack5 or create custom framework using @storybook/framework. Provide steps: create a folder my-framework with package.json, index.js exporting framework name, and using addons: maybe use @storybook/manager-api etc. There's @storybook/server which is a preview framework for static rendering? The error indicates that framework field expects a preset name that resolves to a framework package; @storybook/server is not a framework but a preview. So answer: you need to use @storybook/server-webpack5 or create your own framework that extends @storybook/framework and uses the server addon. Thus final answer: explain that you cannot set framework to @storybook/server; you need to use a framework like @storybook/server-webpack5 or @storybook/html etc. If you want just server rendering, you can use the static Storybook build and serve via your own Node server, or use Storybook's built-in static mode. Or you can create a custom framework that simply sets the preview parameters. } using addons: maybe use @storybook/addon-essential |
|
The exact error points at the framework value used in main.js, // .storybook/main.ts
import type { StorybookConfig } from '@storybook/server-webpack5'
const mainConfig: StorybookConfig = {
stories: ['../stories/**/*.stories.@(json|yaml|yml)'],
addons: ['@storybook/addon-docs', '@storybook/addon-a11y', '@storybook/addon-links'],
framework: '@storybook/server-webpack5',
}
export default mainConfigThe rest of the setup, per // .storybook/preview.js
export const parameters = {
server: {
url: `http://localhost:${port}/storybook_preview`,
},
}Stories are plain JSON (or YAML) files rather than CSF, since there's no client-side render function, Storybook just does a GET to {
"title": "Component",
"stories": [
{
"name": "Default",
"parameters": {
"server": { "id": "path/of/your/story" }
}
}
]
}Running |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Summary
I can't find current/working documentation on how to use storybook with a Node.js-based render server.
Some articles online reference https://www.npmjs.com/package/@storybook/server but that's undocumented and I can't get it to work. Even a basic setup with only
{ framework: '@storybook/server' }in the.storybook/main.jsfails withInvalid value of '@storybook/server' in the 'framework' field of Storybook configand the suggested command to auto-migrate doesn't do anything. I assume this package is deprecated on account of it not being documented anywhere on https://storybook.js.org/ in contrast to what its package readme says.So, how do I create a Storybook setup without any framework that let's me render HTML via a Node.js server? My goal is to render .twig files but I intentionally want to keep this agnostic from any particular templating language or framework so I'm free to choose something other than Twig.
More precisely, how do I make storybook render a story using the HTML returned from a server via a request of roughly the form
GET http://localhost:3000/render/button--default?label=Submit? The only thing included in the request that's strictly required is the story ID so I can find the relevant template file on disk. Other data like the story variant or args is useful and would allow me to extend this further.Additional information
No response
Create a reproduction
I can get a basic setup working because the
@storybook/server-webpack5happens to allow using a Node.js server like so:.storybook/main.js:.storybook/preview.js:However, I'd like to learn how I can write the storybook framework myself because I don't really need all that webpack stuff. The only thing I need is the ability to define the render server URL.
All reactions