Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Decorators in Svelte are executed twice #24549

Closed
JReinhold opened this issue Oct 23, 2023 · 0 comments · Fixed by #24921
Closed

[Bug]: Decorators in Svelte are executed twice #24549

JReinhold opened this issue Oct 23, 2023 · 0 comments · Fixed by #24921

Comments

@JReinhold
Copy link
Contributor

Describe the bug

Decorator in Svelte are always executed twice during story rendering. However Svelte components in decorators are (correctly) only mounted once.

To Reproduce

  1. Open https://stackblitz.com/edit/sveltejs-kit-template-default-ys5aaj?file=.storybook%2Fpreview.ts (SvelteKit sandbox)
  2. npm run storybook
  3. Navigate to a Button story
  4. Open the Console
  5. See that in the logs, "log in decorator" is logged twice, while "TestDecorator onMount" and "Button onMount" are logged once.

System

Storybook Environment Info:

  System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.18.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 9.4.2 - /usr/local/bin/npm <----- active
    pnpm: 8.9.2 - /usr/local/bin/pnpm
  npmPackages:
    @storybook/addon-essentials: ^7.5.1 => 7.5.1 
    @storybook/addon-interactions: ^7.5.1 => 7.5.1 
    @storybook/addon-links: ^7.5.1 => 7.5.1 
    @storybook/blocks: ^7.5.1 => 7.5.1 
    @storybook/svelte: ^7.5.1 => 7.5.1 
    @storybook/sveltekit: ^7.5.1 => 7.5.1 
    @storybook/testing-library: ^0.2.2 => 0.2.2 
    storybook: ^7.5.1 => 7.5.1

Additional context

A simple workaround is to move any logic in the decorator that is not supposed to run twice, from the decorator into a Svelte component's onMount.

Before:

// preview.js

...
decorators: [
	(Story) => {
        // some code that cannot run twice
		return Story();
	},
]
...

After:

// preview.js
import MyDecorator from './MyDecorator.svelte';

...
decorators: [
	() => MyDecorator,
]
...
// MyDecorator.svelte
<script>
  import { onMount } from 'svelte';

  onMount(() => {
    // some code that cannot run twice
  })
</script>

<slot/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants