[Bug]: Angular (angular-vite): switching a toolbar global on a docs page logs NG05104 once per story
Describe the bug
With @storybook/angular-vite 10.6.0, switching any toolbar global (for example a theme) while a docs page is open logs NG05104 once per Angular story on the page:
ERROR RuntimeError: NG05104: The selector "greeting--world[greeting-world-3]" did not match any elements.
ERROR RuntimeError: NG05104: The selector "greeting--storybook[greeting-storybook-1]" did not match any elements.The page renders correctly afterwards; only the errors are wrong. Story mode is not affected. It happens with autodocs and with MDX pages (<Meta of>), with the default docs container and with a custom one. 11.0.0-alpha.1 has the same renderer code.
Reproduction link
https://github.com/nuriddin2468/storybook-angular-vite-docs-ng05104
Reproduction steps
npm install && npm run storybookin the reproduction (Angular 22.1.7, Storybook 10.6.0, one component, two stories, autodocs, onethemetoolbar global).- Open
http://localhost:6006/?path=/docs/greeting--docs. Both stories render, no error. - Switch Theme from
lighttodark. - The console shows one
NG05104per story.
Expected: no error.
System
Storybook Environment Info:
System:
OS: macOS 15.7.7
CPU: (11) arm64 Apple M3 Pro
Shell: 5.9 - /bin/zsh
Binaries:
Node: 24.21.0
npm: 11.19.0 <----- active
pnpm: 10.26.1
Browsers:
Chrome: 153.0.8010.53
Safari: 26.2
npmPackages:
@storybook/addon-docs: 10.6.0 => 10.6.0
@storybook/angular-vite: 10.6.0 => 10.6.0
storybook: 10.6.0 => 10.6.0Additional context
Cause, traced in the browser with the channel events and a mutation observer:
Preview.onUpdateGlobalsre-renders every story render (storyRenders.map((r) => r.rerender())). Without waiting for those,PreviewWithSelection.onUpdateGlobalsre-renders the docs page, and addon-docs'DocsRendererremounts the whole page (key: Math.random()), which removes the story containers.AbstractRenderer.renderin@storybook/angular-vitewrites the story's host element into its container, then queuesbootstrapApplication(queueBootstrapping, one bootstrap at a time).- The re-renders from the first step are still in the queue when the remount removes their containers. When the queue reaches them,
bootstrapApplicationlooks for a host that is no longer in the document: NG05104. The remount's own renders then bootstrap normally, which is why the page ends up correct.
React stories render synchronously, so the double render has no visible effect there.
A fix we apply locally as a pnpm patch of 10.6.0 (dist/_browser-chunks/chunk-UJ3I56EP.js, AbstractRenderer.render): keep the host element written before queueing, and skip the bootstrap when it is no longer connected, since a newer render owns the new host.
- let applicationRef = await queueBootstrapping(() => bootstrapApplication(application, {
+ let hostElement = targetDOMNode.querySelector(componentSelector);
+ let applicationRef = await queueBootstrapping(() => hostElement?.isConnected ? bootstrapApplication(application, {
...storyFnAngular.applicationConfig,
providers
- }));
- applicationRefs.set(targetDOMNode, applicationRef);
+ }) : Promise.resolve(void 0));
+ applicationRef !== void 0 && applicationRefs.set(targetDOMNode, applicationRef);With it applied to the reproduction, three theme switches on the docs page log no NG05104 and every story renders. Fixing the double render in onUpdateGlobals (re-render either the stories or the docs page, not both) would address the root cause for every renderer.
Source: storybookjs/storybook