Join live session: Top 8 Storybook myths holding your team back
Docs
Storybook Docs

previewBody

Parent: main.js|ts configuration

Type: (body: string) => string

Programmatically adjust the preview <body> of your Storybook. Most often used by addon authors.

If you don't need to programmatically adjust the preview body, you can add scripts and styles to preview-body.html instead.

For example, you can conditionally add scripts or styles, depending on the environment:

.storybook/main.ts
// Replace your-framework with the framework you are using (e.g., react-webpack5, vue3-vite)
import type { StorybookConfig } from '@storybook/your-framework';
 
const config: StorybookConfig = {
  framework: '@storybook/your-framework',
  stories: ['../src/**/*.mdx', '../src/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
  previewBody: (body) => `
    ${body}
    ${
      process.env.ANALYTICS_ID ? '<script src="https://cdn.example.com/analytics.js"></script>' : ''
    }
  `,
};
 
export default config;