Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

JReinhold
Copy link
Contributor

@JReinhold JReinhold commented Feb 5, 2025

Closes #29576

What I did

Added two workaround for polka+sirv:

  1. sirv doesn't support serving single files, only directories (which is fair). added a workaround to detect when files need to be served, and if so, serve the parent dir instead, but filtering only for that file.

  2. sirv+polka has a bug (🤷) where serving multiple dirs on the same endpoint results in only the first sirv instance to work. added a workarund so any number of middlewares will continue to work.

I noticed when testing, that if you serve multiple dirs at the same path with conflicting files, dev and build are inconsistent. (in the testing scenario below, that would be /my-static/file-b.txt) dev will serve the file that matches the first staticDir entry with a match, while build will produce the last file that matches.

This is because serving stops at the first sirv that matches, while building copies the files over one by one, and so the last one wins naturally. A solution would be to traverse the staticDirs in reverse order in either serve or build, so they match, but I'm not sure if that will break other use cases, so I've opted for just not fixing that here. 🤷

Checklist for Contributors

Testing

@valentinpalkovic I haven't added any tests for this. I don't think this is really unit-testable. Adding a bunch of static dirs to all sandboxes seems a bit too much. I could add some static dirs just for the UI Storybook and have a story show their content. Then it would be tested by Chromatic, but only the built Storybook would be tested, not the dev one which we're fixing here. 🤷

The changes in this PR are covered in the following automated tests:

  • stories
  • unit tests
  • integration tests
  • end-to-end tests

Manual testing

  1. Add the following to code/.storybook/main.ts:
  staticDirs: [
    {
      from: './some-static/a-dir',
      to: '/my-static',
    },
    {
      from: './some-static/b-dir',
      to: '/my-static',
    },
    {
      from: './some-static/just-a-file.txt',
      to: '/the-file.txt',
    },
    {
      from: './some-static/just-a-file.txt',
      to: '/my-static/the-file.txt',
    },
  ],
  1. Add the corresponding files to code/.storybook with some content:
some-static/
├── just-a-file.txt
├── a-dir/
│   ├── file-a.txt
│   └── file-b.txt
└── b-dir/
    ├── file-b.txt
    ├── file-c.txt
    └── sub/
        └── file-d.txt
  1. Start Storybook dev and visit the following URLs and see that they work:

    1. http://localhost:6006/my-static/file-a.txt
    2. http://localhost:6006/my-static/file-b.txt
    3. http://localhost:6006/my-static/file-c.txt
    4. http://localhost:6006/my-static/sub/file-d.txt
    5. http://localhost:6006/my-static/the-file.txt
    6. http://localhost:6006/the-file.txt
  2. Build the storybook and visit the same URLs and see that they work.

This section is mandatory for all contributions. If you believe no manual test is necessary, please state so explicitly. Thanks!

Documentation

  • Add or update documentation reflecting your changes
  • If you are deprecating/removing a feature, make sure to update
    MIGRATION.MD

Checklist for Maintainers

  • When this PR is ready for testing, make sure to add ci:normal, ci:merged or ci:daily GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found in code/lib/cli-storybook/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>

name before after diff z %
createSize 0 B 0 B 0 B - -
generateSize 78.3 MB 78.3 MB 0 B 1.51 0%
initSize 132 MB 132 MB 955 B 2.04 0%
diffSize 53.2 MB 53.2 MB 955 B 2.89 0%
buildSize 7.22 MB 7.22 MB 0 B 3 0%
buildSbAddonsSize 1.87 MB 1.87 MB 0 B 3 0%
buildSbCommonSize 195 kB 195 kB 0 B - 0%
buildSbManagerSize 1.88 MB 1.88 MB 0 B 3 0%
buildSbPreviewSize 0 B 0 B 0 B - -
buildStaticSize 0 B 0 B 0 B - -
buildPrebuildSize 3.95 MB 3.95 MB 0 B 3 0%
buildPreviewSize 3.27 MB 3.27 MB 0 B 2.99 0%
testBuildSize 0 B 0 B 0 B - -
testBuildSbAddonsSize 0 B 0 B 0 B - -
testBuildSbCommonSize 0 B 0 B 0 B - -
testBuildSbManagerSize 0 B 0 B 0 B - -
testBuildSbPreviewSize 0 B 0 B 0 B - -
testBuildStaticSize 0 B 0 B 0 B - -
testBuildPrebuildSize 0 B 0 B 0 B - -
testBuildPreviewSize 0 B 0 B 0 B - -
name before after diff z %
createTime 8.9s 9.8s 921ms -1.26 🔺9.3%
generateTime 17.9s 19.8s 1.9s 0.08 9.6%
initTime 11.4s 12.6s 1.1s -0.27 9.1%
buildTime 8.6s 9.1s 482ms 0.17 5.3%
testBuildTime 0ms 0ms 0ms - -
devPreviewResponsive 4.6s 7.3s 2.6s 3.76 🔺36.4%
devManagerResponsive 3.6s 4.9s 1.3s 2.75 🔺27.5%
devManagerHeaderVisible 658ms 1.2s 543ms 3.64 🔺45.2%
devManagerIndexVisible 718ms 1.3s 617ms 4.6 🔺46.2%
devStoryVisibleUncached 3.6s 4.4s 796ms 1.18 17.9%
devStoryVisible 720ms 1.2s 545ms 3.65 🔺43.1%
devAutodocsVisible 653ms 1.2s 589ms 5.31 🔺47.4%
devMDXVisible 643ms 1.1s 550ms 5.73 🔺46.1%
buildManagerHeaderVisible 744ms 1.1s 373ms 3.05 🔺33.4%
buildManagerIndexVisible 843ms 1.2s 409ms 3.7 🔺32.7%
buildStoryVisible 672ms 1s 346ms 3.35 🔺34%
buildAutodocsVisible 570ms 779ms 209ms 1.25 🔺26.8%
buildMDXVisible 544ms 798ms 254ms 2.35 🔺31.8%

Greptile Summary

Based on the provided information, I'll create a concise summary of the PR changes focusing on the static file serving improvements in Storybook:

This PR adds workarounds for serving static files in Storybook, addressing issues with the sirv and polka middleware stack to improve static file handling.

  • Added fix in code/core/src/core-server/utils/server-statics.ts to serve single files by using parent directory with filtered access
  • Implemented workaround for serving multiple directories at the same endpoint by preserving request URL state
  • Fixed regression from Express to Polka migration that prevented serving individual files in staticDirs config
  • Maintains backward compatibility with existing staticDirs configurations including single file paths
  • Note: Dev and build environments may handle conflicting files from multiple directories differently

Copy link

nx-cloud bot commented Feb 5, 2025

View your CI Pipeline Execution ↗ for commit e82077b.

Command Status Duration Result
nx run-many -t build --parallel=3 ✅ Succeeded 1m 47s View ↗

☁️ Nx Cloud last updated this comment at 2025-02-05 10:38:34 UTC

@JReinhold JReinhold self-assigned this Feb 5, 2025
@JReinhold JReinhold marked this pull request as ready for review February 5, 2025 10:05
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 file(s) reviewed, 1 comment(s)
Edit PR Review Bot Settings | Greptile

@valentinpalkovic valentinpalkovic changed the title add workaround for static serving of single files and for serving multiple dirs on the same endpoint Core: Add workaround for static serving of single files and for serving multiple dirs on the same endpoint Feb 5, 2025
@JReinhold JReinhold changed the title Core: Add workaround for static serving of single files and for serving multiple dirs on the same endpoint Core: Fix statically serving single files and multiple dirs on the same endpoint Feb 5, 2025
@JReinhold JReinhold merged commit 30a5263 into next Feb 5, 2025
55 of 59 checks passed
@JReinhold JReinhold deleted the jeppe/staticdirs-workaround branch February 5, 2025 12:45
@github-actions github-actions bot mentioned this pull request Feb 5, 2025
11 tasks
@JReinhold JReinhold added needs qa Indicates that this needs manual QA during the upcoming minor/major release and removed needs qa Indicates that this needs manual QA during the upcoming minor/major release labels Feb 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Storybook 8.4 cannot serve static assets declared by file
2 participants