previewHead
Parent: main.js|ts configuration
Type: (head: string) => string
Programmatically adjust the preview <head> of your Storybook. Most often used by addon authors.
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-vite, nextjs, nextjs-vite)
import { defineMain } from '@storybook/your-framework/node';
export default defineMain({
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>' : ''
}
`,
});