storybook: [Bug]: Hot reload broken in Storybook 7 with Vite
Describe the bug
Hello, can you help me understand why Storybook may not reload (HRM) after making changes to a component that is used in the stories?
In the stories, if I make changes, it picks them up.
I repeat, the problem is that if you make changes in the component, not in the .stories.tsx
After creating a project from scratch, I already have a problem that I encountered.
Does anyone have any ideas why this might be happening?
To Reproduce
To reproduce, you only need two lines:
yarn create vite (choose preact + typescript)
npx storybook init --builder=vite
System
Environment Info:
System:
OS: macOS 13.3.1
CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
Binaries:
Node: 16.15.0 - /usr/local/bin/node
Yarn: 1.22.19 - /usr/local/bin/yarn
npm: 8.5.5 - /usr/local/bin/npm
npmPackages:
@storybook/addon-essentials: ^7.0.11 => 7.0.11
@storybook/addon-interactions: ^7.0.11 => 7.0.11
@storybook/addon-links: ^7.0.11 => 7.0.11
@storybook/blocks: ^7.0.11 => 7.0.11
@storybook/preact: ^7.0.11 => 7.0.11
@storybook/preact-vite: ^7.0.11 => 7.0.11
@storybook/testing-library: ^0.0.14-next.2 => 0.0.14-next.2
Additional context
package.json
{
"name": "storybookbug",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview",
"storybook": "storybook dev -p 6006",
"build-storybook": "storybook build"
},
"dependencies": {
"preact": "^10.13.1"
},
"devDependencies": {
"@preact/preset-vite": "^2.5.0",
"@storybook/addon-essentials": "^7.0.11",
"@storybook/addon-interactions": "^7.0.11",
"@storybook/addon-links": "^7.0.11",
"@storybook/blocks": "^7.0.11",
"@storybook/preact": "^7.0.11",
"@storybook/preact-vite": "^7.0.11",
"@storybook/testing-library": "^0.0.14-next.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"storybook": "^7.0.11",
"typescript": "^5.0.2",
"vite": "^4.3.2"
}
}
vite.config.ts
import { defineConfig } from 'vite'
import preact from '@preact/preset-vite'
export default defineConfig({
plugins: [preact()],
})
tsconfig.json
{
"compilerOptions": {
"target": "esnext",
"useDefineForClassFields": true,
"module": "ESNext",
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"skipLibCheck": true,
"esModuleInterop": true,
/* Bundler mode */
"moduleResolution": "node",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"jsxImportSource": "preact",
/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src"],
"exclude": ["node_modules"],
"references": [{ "path": "./tsconfig.node.json" }]
}
.storybook/main.ts
import type { StorybookConfig } from "@storybook/preact-vite";
const config: StorybookConfig = {
stories: ["../src/**/*.mdx", "../src/**/*.stories.@(js|jsx|ts|tsx)"],
addons: [
"@storybook/addon-links",
"@storybook/addon-essentials",
"@storybook/addon-interactions",
],
core: {
builder: "@storybook/builder-vite",
},
framework: {
name: "@storybook/preact-vite",
options: {},
},
viteFinal: (config, { configType }) => {
config.resolve.alias['react'] = 'preact/compat';
config.resolve.alias['react-dom'] = 'preact/compat';
return config;
},
};
export default config;
.storybook/preview.ts
import type { Preview } from "@storybook/preact";
const preview: Preview = {
parameters: {
actions: { argTypesRegex: "^on[A-Z].*" },
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/,
},
},
},
};
export default preview;
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 33
- Comments: 25 (6 by maintainers)
Same issue still persists on the latest versions of vite/storybook + react. I created a fresh react project with vite, then initialize storybook. The stories do not reload if I make changes to the component (e.g.: Button.jsx), even though vite detect the changes.
This is very annoying
I came back to try to resolve this again … and failed again. No amount of logging and debug flags seems to lead anywhere.
As far as I can tell, it only happens when JavaScript code HMR event happens and only for some stories.
What happen is that
<i> [webpack-dev-middleware] wait until bundle finished: /runtime_main.01156ca6bcc1452e80e2.hot-update.json
I use Preact and Storybook+Vite and had the exact same issue. I disabled Prefresh in my vite config and that fixed the issue for me. I imagine disabling react-refresh or similar might help with other cases too.
In your
vite.config.jsfile:plugins: [preact({ prefreshEnabled: false })],Same issue here --> https://github.com/storybookjs/storybook/issues/22124
In my case the fix seems to be similar, although
fastRefreshisn’t a valid config item it seems. What has worked for me on a nexjs based project is including react as a plugin to the vite config. I have in mymain.ts:By including react as a vite plugin rendering is now much faster and HMR works
One reliable workaround for us was to keep the actual stories in a separate file and just import and re-export them from .story.tsx files:
For some reason if you keep the actual stories inside .story.tsx vite can’t properly handle the named exports from .story.tsx and will do a full reload.
Recently @devongovett released https://github.com/parcel-bundler/storybook which seems to address this issue but I haven’t had luck with getting it work with my setup of 1000+ stories.
I’m on
8.3.0-alpha.8and changes to the components result inpage reloadalthough stories are reloaded withhmr update. I tried all suggested here solutions but no improvements.I’m also on 8.0.9 and sadly this doesn’t seem to have helped, wonder why we’re getting different outcomes
Same fix works for react as a work around
I’m new to storybook and working on a fresh setup using
@storybook/vue3-viterunning latest version7.4.5and HMR does not work with the generated demo files.Edit: I use storybook in a WordPress theme which already uses vite. Storybook is in the root of the project containing a vite.config.ts which seems to be used and in my case breaks hmr.