storybook: Windows staticDirs issue - Storybook not loading assets despite path seemingly correct

Describe the bug On a Windows machine the staticDirs build with the expected structure:

dist/
    storybook/
        assets/
            imgs/
                imgs_sub/
                    image.png
            svgs/
                icon.svg

Then when we run npm run start, Storybook starts and appears to be working fine, but none of the static assets can be found. When looking at the network it seems to be requesting the correct paths:

http://localhost:6006/assets/imgs/imgs_sub/image.png http://localhost:6006/assets/svgs/icon.svg

But it’s returning a 404. If I go direct to that path it also doesn’t load.

main.js looks like this:

module.exports = {
  stories: ['../src/**/*.stories.ts'],
  addons: ['@storybook/addon-actions', '@storybook/addon-links', '@storybook/addon-a11y', '@storybook/addon-knobs'],
  staticDirs: [
    '../src/assets',
    { from: '../projects/icons/svgs', to: './assets/svgs' },
    { from: '../projects/images', to: './assets/imgs' }
  ],
  core: {
    builder: 'webpack5'
  },
  angularOptions: {
    enableIvy: false
  }
};

Debugging server-statics.js returns the following:

_arg$split = [".\projects\icons\svgs",".\assets\svgs"]
target=".\assets\svgs"
staticDir="./.\projects\icons\svgs"
staticPath="C:\Users\user\IdeaProjects\repo\projects\icons\svgs"
targetDir="./.\assets\svgs"
targetEndpoint="/.\assets\svgs"

and for imgs

_arg$split = [".\projects\images",".\assets\imgs"]
target=".\assets\imgs"
staticDir="./.\projects\images"
staticPath="C:\Users\user\IdeaProjects\repo\projects\images"
targetDir="./.\assets\imgs"
targetEndpoint="/.\assets\imgs"

If I remove the dot from main.js, like so, then the build errors:

staticDirs: [
    '../src/assets',
    { from: '../projects/icons/svgs', to: '/assets/svgs' },
    { from: '../projects/images', to: '/assets/imgs' }
],

Error returned:

info => Copying static files: C:\Users\user\IdeaProjects\repo\src\assets at C:\Users\user\IdeaProjects\repo\dist\storybook\
C:\Users\user\IdeaProjects\repo\node_modules\@storybook\core-server\dist\cjs\utils\server-statics.js:118
    throw new Error((0, _tsDedent.default)((0, _chalk.default)`
          ^

Error: Failed to load static files, no such directory: C:\Users\user\IdeaProjects\repo\projects\icons\svgs:\assets\svgs

System

Environment Info:

  System:
    OS: Windows 10 10.0.19044
    CPU: (8) x64 Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
  Binaries:
    Node: 16.13.2 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.17 - ~\IdeaProjects\repo\node_modules\.bin\yarn.CMD
    npm: 6.14.15 - ~\IdeaProjects\repo\node_modules\.bin\npm.CMD
  Browsers:
    Chrome: 97.0.4692.71
    Edge: Spartan (44.19041.1266.0), Chromium (97.0.1072.62)
  npmPackages:
    @storybook/addon-a11y: ^6.4.13 => 6.4.13
    @storybook/addon-actions: ^6.4.13 => 6.4.13
    @storybook/addon-knobs: 6.2.9 => 6.2.9
    @storybook/addon-links: ^6.4.13 => 6.4.13
    @storybook/addons: ^6.4.13 => 6.4.13
    @storybook/angular: ^6.4.13 => 6.4.13
    @storybook/builder-webpack5: ^6.4.13 => 6.4.13
    @storybook/manager-webpack5: ^6.4.13 => 6.4.13

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 13
  • Comments: 27 (4 by maintainers)

Commits related to this issue

Most upvoted comments

Still doesn’t work

Update: Problem solved for me after creating storybook-static folder at the root of my project and placing all the assets files there.

Would be nice to backport this to 6.4.x if it’s not too much trouble 😄

Sorry to jump in a closed issue, but is this problem also linked to the fact that I am not able to use the following ?

    staticDirs: [
      { from: '../../public', to: 'assets/' }, // working properly
      { from: '../../public', to: '/assets' }, // not working
      { from: '../../public', to: '/assets/' }, // not working
    ],

Based on a docs configuration object example found here, it should work with a leading foward slash.

I just ran into this recently, staticDir:[..] instead of staticDirs:[...]

Whoopee!! I just released https://github.com/storybookjs/storybook/releases/tag/v6.5.0-alpha.47 containing PR #17641 that references this issue. Upgrade today to the @next NPM tag to try it out!

npx sb upgrade --prerelease

👆🏼 So I fixed this and submitted a PR, details are (copied from PR):


Noticed that the test “supports absolute file paths with custom endpoint” didn’t cover the reality for Windows.

The relativeDir provided by getDirectoryFromWorkingDir() resolves all paths to OS separators (e.g. \ on Windows). The result of which would look like C:\\foo\\bar:\\custom-endpoint and not C:\\foo\\bar:/custom-endpoint which the test was covering; so I added a test to cover the actual case.


Digging in, the regex split in parseStaticDir didn’t account for the actual path, as it was ignoring all cases of :\ to cover the X:\ scenario, which is what resulted in bug #17271.

To work around this, I changed the code to look for the last colon :, and use that to split. However, in case the last colon is actually part of a Windows absolute path (e.g. C:\), needed to check a few conditions:

  • Is the path Windows absolute? (We don’t care if Unix absolute because it won’t have a colon)
  • Is the path just a single path (i.e. doesn’t contain the : delimiter for static and target dirs.

Once this was determined, the final piece of the puzzle was ensuring the target was converted from \ to /, for which I just split on path.sep and joined on path.posix.sep to ensure a forward-slash /.

This is not working with 6.5

main.js config => staticDirs: [‘…/public’],

This is the logo (in public folder). Check the storybook log at the bottom. It is able to find the directory. img1

This is how it is displayed. img2

This is how it comes img3

I got stuck because of this bug and I can’t upgrade to another version… But I found a workaround that allows me to continue:

You can also configure a directory (or a list of directories) for searching static content when you are starting Storybook. You can do that with the -s flag. https://stackoverflow.com/questions/58267903/serving-static-files-in-storybook-js

Thanks for your great work and all the best for the Storybook team!

It might sound dumb, but for me, I just realized I am using version 6.2, and 6.2 doesn’t have staticDirs feature, I have to use start-storybook -s ./static instead.