-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Blocks: Fix Stories
block rendering duplicate stories when globals are changed
#26110
Blocks: Fix Stories
block rendering duplicate stories when globals are changed
#26110
Conversation
if (this.attachedCSFFiles.has(csfFile)) { | ||
// this CSF file is already attached, don't do anything | ||
return; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the actual fix for the issue, if we actually don't think supporting multiple attached CSF file is a valid use case then this PR can be reduced to these lines.
Did the index end up with the According to this test it should:
In which case I would have expected the line referenced by that comment to run, which would attach the second CSF file's stories to the docs context, and they'd show up in autodocs. I guess that's not happening, I would have to debug through why. Maybe we can do it together? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the changes look good apart from one nit.
I think maybe we should try and add some sandbox stories for this situation and maybe an E2E test that checks the behaviour?
code/lib/preview-api/src/modules/preview-web/docs-context/DocsContext.ts
Outdated
Show resolved
Hide resolved
@tmeasday I don't know what happened, but when adding the E2E tests it works as expected, stories from multiple CSF files are merged in autodocs. I must have gotten my test-setup wrong. It doesn't work in MDX but that's to be expected. |
Closes #24751
What I did
This PR refactors
DocsContext
so it's more resilient in scenarios where multiple CSF files are being attached.The cause of the linked issue was that rendering
<Meta />
would callattachCSFFile
, and changing a global causes the whole docs view to re-render. This meant thatattachCSFFile
would be called again with the same CSF file, which would just append the stories to the existing array inDocsContext
again and again.Upon investigating this I concluded that
DocsContext
wasn't completely implemented in a way that handled multiple CSF files being attached at the same time. This could theoretically result in theprimaryStory
being the primary of the first attached CSF file, however a resolvedmeta
orcomponent
would return the one from the last attached CSF file. This has now been fixed in this PR - the first attached CSF file will always be considered the "primary" one to resolve from.However I haven't figured out how to test this in practice, I can't set up a scenario where multiple different CSF files are being attached to the same context. Based on the comment here, it suggest that this is a valid scenario though in non-manual-MDX files:
storybook/code/lib/preview-api/src/modules/preview-web/render/CsfDocsRender.ts
Lines 106 to 108 in 332c593
I tried having two different CSF files with the same title, and while that did collect all stories under the same component in the sidebar, it didn't make a difference in autodocs. @tmeasday any ideas on how I can test this out in a real Storybook?
Checklist for Contributors
Testing
The changes in this PR are covered in the following automated tests:
Manual testing
<Stories />
: https://630511d655df72125520f051-tprruuakio.chromatic.com/?path=/docs/addons-docs-docs2-button--metaofnext
, where it duplicates the stories when globals are changed: https://next--630511d655df72125520f051.chromatic.com/?path=/docs/addons-docs-docs2-button--metaofDocumentation
MIGRATION.MD
Checklist for Maintainers
When this PR is ready for testing, make sure to add
ci:normal
,ci:merged
orci:daily
GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found incode/lib/cli/src/sandbox-templates.ts
Make sure this PR contains one of the labels below:
Available labels
bug
: Internal changes that fixes incorrect behavior.maintenance
: User-facing maintenance tasks.dependencies
: Upgrading (sometimes downgrading) dependencies.build
: Internal-facing build tooling & test updates. Will not show up in release changelog.cleanup
: Minor cleanup style change. Will not show up in release changelog.documentation
: Documentation only changes. Will not show up in release changelog.feature request
: Introducing a new feature.BREAKING CHANGE
: Changes that break compatibility in some way with current major version.other
: Changes that don't fit in the above categories.🦋 Canary release
This PR does not have a canary release associated. You can request a canary release of this pull request by mentioning the
@storybookjs/core
team here.core team members can create a canary release here or locally with
gh workflow run --repo storybookjs/storybook canary-release-pr.yml --field pr=<PR_NUMBER>