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]: Webpack fsCache does not work with @storybook/nextjs framework #29621

Open
sentience opened this issue Nov 15, 2024 · 0 comments · May be fixed by #29654
Open

[Bug]: Webpack fsCache does not work with @storybook/nextjs framework #29621

sentience opened this issue Nov 15, 2024 · 0 comments · May be fixed by #29654

Comments

@sentience
Copy link
Contributor

sentience commented Nov 15, 2024

Describe the bug

The Storybook Next.js framework uses webpack internally to build Storybooks for Next.js projects.

The Storybook webpack builder supports an fsCache option to enable disk-based caching, which greatly improves incremental build performance in webpack-based Storybook projects.

When the fsCache option is enabled in a Storybook project that uses the Next.js framework, however, the disk-based cache does not work, and the Storybook build outputs a large number of webpack.cache.packFileCacheStrategy warnings. As a result, Next.js projects cannot benefit from the significant speed boost afforded by the webpack disk cache.

Storybook config

import { StorybookConfig } from "@storybook/nextjs"

export default {
  stories: ["../example/**/*.stories.(ts|tsx)"],
  framework: {
    name: "@storybook/nextjs",
    options: {
      builder: {
        fsCache: true,
      },
    },
  },
  addons: [],
} satisfies StorybookConfig

Reproduction link

https://github.com/sentience/storybook-nextjs-webpack-cache-issue-demo

Reproduction steps

  1. git clone https://github.com/sentience/storybook-nextjs-webpack-cache-issue-demo.git
  2. cd storybook-nextjs-webpack-cache-issue-demo
  3. pnpm i
  4. pnpm storybook dev

System

Storybook Environment Info:

  System:
    OS: macOS 15.1
    CPU: (8) arm64 Apple M1
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.19.1 - ~/Developer/storybook-nextjs-webpack-cache-issue-demo/.devbox/nix/profile/default/bin/node
    Yarn: 1.22.22 - ~/Developer/storybook-nextjs-webpack-cache-issue-demo/.devbox/virtenv/nodejs/corepack-bin/yarn
    npm: 10.2.4 - ~/Developer/storybook-nextjs-webpack-cache-issue-demo/.devbox/nix/profile/default/bin/npm
    pnpm: 9.12.3 - ~/Developer/storybook-nextjs-webpack-cache-issue-demo/.devbox/virtenv/nodejs/corepack-bin/pnpm <----- active
  Browsers:
    Chrome: 130.0.6723.119
    Safari: 18.1
  npmPackages:
    @storybook/global: ^5.0.0 => 5.0.0 
    @storybook/nextjs: ^8.4.3 => 8.4.3 
    @storybook/types: ^8.4.3 => 8.4.3 
    storybook: ^8.4.3 => 8.4.3

Additional context

This problem seems to have existed since the introduction of @storybook/nextjs in Storybook 8.0.

sentience added a commit to sentience/storybook that referenced this issue Nov 19, 2024
Next.js overrides certain internal webpack packages (particularly `webpack-sources`), which are involved with filesystem caching, with its own versions from next/dist/compiled.

For filesystem caching to work, Next.js must be allowed to perform these overrides before webpack is first initialized by @storybook/builder-webpack5. If it is not, the objects to be serialized to disk in the caching process will be instantiated using the original (non-Next.js) modules, but the serializers will be created using the Next.js modules. This mismatch between the objects to be cached and the serializers that write the filesystem cache prevents the cache from being written; instead, webpack outputs a warning message to the console for every object that it tries and fails to find a matching serializer for.

This fix works by invoking Next.js to configure webpack in the `core` hook of @storybook/nextjs/preset, immediately before loading @storybook/builder-webpack5. We don't actually use this configuration that Next.js creates; the actual configuration that will be used in the build is still generated in `webpackFinal` as before.

`fsCache` has a large impact on Storybook build performance. Even in a minimal project with a single story, enabling it reduces build time by 66%! This is therefore a very valuable option to be able to enable.

Fixes storybookjs#29621
@sentience sentience linked a pull request Nov 19, 2024 that will close this issue
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants