Next.js: Add support for v16.2#34046
Conversation
|
View your CI Pipeline Execution ↗ for commit 02dfa45
☁️ Nx Cloud last updated this comment at |
12a66fd to
2951962
Compare
📝 WalkthroughWalkthroughThe SWC loader in Next.js framework has been refactored to dynamically resolve browser support configuration at runtime. The change replaces static import with an async helper that intelligently determines the correct Next.js module path based on version compatibility, supporting both v16.2+ and older releases. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes ✨ Finishing Touches
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@code/frameworks/nextjs/src/swc/loader.ts`:
- Around line 62-73: The try/catch around the dynamic import of
"next/dist/build/get-supported-browsers.js" in loader.ts is too broad and hides
real runtime errors; change the catch to only perform the fallback import when
the failure is a module-resolution error (e.g. error.code ===
'ERR_MODULE_NOT_FOUND' or the Node-specific "Cannot find module" condition), and
re-throw the caught error for any other error; specifically update the block
that calls getSupportedBrowsers (the dynamic import returning
getSupportedBrowsers(projectRoot, isDevelopment)) so it only falls back to
importing "next/dist/build/utils.js" when the error indicates the module is not
found, otherwise throw the error.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 4f463ad9-150b-4d4a-9407-ad0945b3688c
📒 Files selected for processing (1)
code/frameworks/nextjs/src/swc/loader.ts
| try { | ||
| // @ts-expect-error - Correct import since Next.js v16.2 | ||
| return (await import('next/dist/build/get-supported-browsers.js')).getSupportedBrowsers( | ||
| projectRoot, | ||
| isDevelopment | ||
| ); | ||
| } catch (e) { | ||
| // TODO: Remove as soon as we don't have to support Next.js < 16.2 anymore | ||
| return (await import('next/dist/build/utils.js')).getSupportedBrowsers( | ||
| projectRoot, | ||
| isDevelopment | ||
| ); |
There was a problem hiding this comment.
Limit fallback to module-resolution failures only.
At Line 68, the broad catch masks any runtime error from the v16.2 path and silently falls back to legacy code. Re-throw non-resolution errors so real failures are not hidden.
Proposed fix
async function getSupportedBrowsers(projectRoot: string, isDevelopment: boolean) {
try {
// `@ts-expect-error` - Correct import since Next.js v16.2
return (await import('next/dist/build/get-supported-browsers.js')).getSupportedBrowsers(
projectRoot,
isDevelopment
);
- } catch (e) {
+ } catch (error: unknown) {
+ const moduleResolutionError =
+ error instanceof Error &&
+ 'code' in error &&
+ ((error as NodeJS.ErrnoException).code === 'MODULE_NOT_FOUND' ||
+ (error as NodeJS.ErrnoException).code === 'ERR_MODULE_NOT_FOUND') &&
+ error.message.includes('next/dist/build/get-supported-browsers.js');
+
+ if (!moduleResolutionError) {
+ throw error;
+ }
+
// TODO: Remove as soon as we don't have to support Next.js < 16.2 anymore
return (await import('next/dist/build/utils.js')).getSupportedBrowsers(
projectRoot,
isDevelopment
);
}
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| try { | |
| // @ts-expect-error - Correct import since Next.js v16.2 | |
| return (await import('next/dist/build/get-supported-browsers.js')).getSupportedBrowsers( | |
| projectRoot, | |
| isDevelopment | |
| ); | |
| } catch (e) { | |
| // TODO: Remove as soon as we don't have to support Next.js < 16.2 anymore | |
| return (await import('next/dist/build/utils.js')).getSupportedBrowsers( | |
| projectRoot, | |
| isDevelopment | |
| ); | |
| async function getSupportedBrowsers(projectRoot: string, isDevelopment: boolean) { | |
| try { | |
| // `@ts-expect-error` - Correct import since Next.js v16.2 | |
| return (await import('next/dist/build/get-supported-browsers.js')).getSupportedBrowsers( | |
| projectRoot, | |
| isDevelopment | |
| ); | |
| } catch (error: unknown) { | |
| const moduleResolutionError = | |
| error instanceof Error && | |
| 'code' in error && | |
| ((error as NodeJS.ErrnoException).code === 'MODULE_NOT_FOUND' || | |
| (error as NodeJS.ErrnoException).code === 'ERR_MODULE_NOT_FOUND') && | |
| error.message.includes('next/dist/build/get-supported-browsers.js'); | |
| if (!moduleResolutionError) { | |
| throw error; | |
| } | |
| // TODO: Remove as soon as we don't have to support Next.js < 16.2 anymore | |
| return (await import('next/dist/build/utils.js')).getSupportedBrowsers( | |
| projectRoot, | |
| isDevelopment | |
| ); | |
| } | |
| } |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@code/frameworks/nextjs/src/swc/loader.ts` around lines 62 - 73, The try/catch
around the dynamic import of "next/dist/build/get-supported-browsers.js" in
loader.ts is too broad and hides real runtime errors; change the catch to only
perform the fallback import when the failure is a module-resolution error (e.g.
error.code === 'ERR_MODULE_NOT_FOUND' or the Node-specific "Cannot find module"
condition), and re-throw the caught error for any other error; specifically
update the block that calls getSupportedBrowsers (the dynamic import returning
getSupportedBrowsers(projectRoot, isDevelopment)) so it only falls back to
importing "next/dist/build/utils.js" when the error indicates the module is not
found, otherwise throw the error.
…ext-16.2 Next.js: Add support for v16.2 (cherry picked from commit 552c36a)
Closes #
What I did
Checklist for Contributors
Testing
The changes in this PR are covered in the following automated tests:
Manual testing
Caution
This section is mandatory for all contributions. If you believe no manual test is necessary, please state so explicitly. Thanks!
Documentation
MIGRATION.MD
Checklist for Maintainers
When this PR is ready for testing, make sure to add
ci:normal,ci:mergedorci:dailyGH 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.tsMake 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/coreteam here.core team members can create a canary release here or locally with
gh workflow run --repo storybookjs/storybook publish.yml --field pr=<PR_NUMBER>Summary by CodeRabbit