Docs
Storybook Docs

previewHead

父级:main.js|ts 配置

¥Parent: main.js|ts configuration

类型:(head: string) => string

¥Type: (head: string) => string

以编程方式调整 Storybook 的 预览 <head>。最常由 插件作者 使用。

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

如果你不需要以编程方式调整预览头,则可以将脚本和样式添加到 preview-head.html

¥If you don't need to programmatically adjust the preview head, you can add scripts and styles to preview-head.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)'],
  previewHead: (head) => `
    ${head}
    ${
      process.env.ANALYTICS_ID ? '<script src="https://cdn.example.com/analytics.js"></script>' : ''
    }
  `,
};
 
export default config;