-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Next.js: Fix webpack fsCache not working #29654
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 902dc18. 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
Sent 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 :) |
@valentinpalkovic Thanks to that canary build I found some more places where webpack was being referenced before Next.js replaced it with its own internal instance, causing disk cache serializer mismatch errors. I believe I've now fixed all of these. Could you roll me another canary build, please? 🙏🏻 |
I've now tested this in several projects, and I think this is in good enough shape to release. Setting I am, however, aware of one caveat to this fix: projects that use If you set
…and set
These messages complain that import makeSerializable from "webpack/lib/util/makeSerializable.js";
import NullDependency from "webpack/lib/dependencies/NullDependency.js"; Next.js overrides a bunch of webpack import paths to redirect consumers to its internal copy of webpack, but these paths are not among them. As a result, @storybook/react-docgen-typescript-plugin registers its cache serializer for This should be possible to fix by updating @storybook/react-docgen-typescript-plugin to use the officially exposed API for registering cache serializers, rather than its current approach that depends on internals that are not exposed by Next.js's webpack instance. Looking at mini-css-extract-plugin, for example, it uses the In the meantime, however, this PR is no worse than the current behaviour (where all objects print a "not serializable cache item" warning), and even in projects where react-docgen-typescript is used, the partial fix of this PR provides a large boost to incremental build performance. I therefore propose that this be merged as-is, and I'll open a separate issue to track the warnings in react-docgen-typescript projects. |
Package BenchmarksCommit: No significant changes detected, all good. 👏 |
Not sure why |
Anything else I can do to help move this along? |
Hi @sentience Thank you for your contribution. I was on vacation but I am back again. I'll take a close look this week. |
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-83a1f24f
. Try it out in a new sandbox by runningnpx storybook@0.0.0-pr-29654-sha-83a1f24f sandbox
or in an existing project withnpx storybook@0.0.0-pr-29654-sha-83a1f24f upgrade
.More information
0.0.0-pr-29654-sha-83a1f24f
fix-nextjs-webpack-fscache
83a1f24f
1733206781
)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