-
-
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
Next.js: Fix webpack fsCache not working #29654
base: next
Are you sure you want to change the base?
Conversation
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
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.
1 file(s) reviewed, 1 comment(s)
Edit PR Review Bot Settings | Greptile
☁️ Nx Cloud ReportCI is running/has finished running commands for commit 2a02e13. As they complete they will appear below. Click to see the status, the terminal output, and the build insights. 📂 See all runs for this CI Pipeline Execution ✅ Successfully ran 1 targetSent with 💌 from NxCloud. |
@valentinpalkovic May I request a canary release of this? Sorry if you're not the right person to mention; the PR template says to mention @storybookjs/core, but that doesn't seem to be recognised by GitHub. |
Here we go: https://github.com/storybookjs/storybook/actions/runs/12026402956/job/33525318348 When the job is done, you can see the canary version :) |
Closes #29621
What I did
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 cache 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 inwebpackFinal
as before.fsCache
(docs) has a large impact on Storybook build performance. Even in a minimal project with a single story, enabling it reduces hot-cache build time by 66%! This is therefore a very valuable option to be able to enable.Checklist for Contributors
Testing
@storybook/nextjs does not appear to have a test suite, so I don't see an easy way to test this feature. I've verified that it resolves the issue in my repro project for #29621.
The changes in this PR are covered in the following automated tests:
Manual testing
pnpm i
,pnpm storybook build
) and note the console warnings at the end of the build that indicate the filesystem cache is not working.Documentation
Add or update documentation reflecting your changesIf you are deprecating/removing a feature, make sure to updateMIGRATION.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-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 pull request has been released as version
0.0.0-pr-29654-sha-2a02e134
. Try it out in a new sandbox by runningnpx [email protected] sandbox
or in an existing project withnpx [email protected] upgrade
.More information
0.0.0-pr-29654-sha-2a02e134
fix-nextjs-webpack-fscache
2a02e134
1732608762
)To request a new release of this pull request, mention the
@storybookjs/core
team.core team members can create a new canary release here or locally with
gh workflow run --repo storybookjs/storybook canary-release-pr.yml --field pr=29654
Greptile Summary
This PR fixes webpack's filesystem caching in @storybook/nextjs by ensuring Next.js can properly override webpack internals before Storybook's webpack builder initialization, significantly improving build performance.
code/frameworks/nextjs/src/preset.ts
to load Next.js configuration in thecore
hook before webpack initializationwebpack-sources
webpackFinal
hook for actual builds