Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions code/frameworks/nextjs/src/swc/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { getVirtualModules } from '@storybook/builder-webpack5';

import type { NextConfig } from 'next';
import nextJSLoadConfigModule from 'next/dist/build/load-jsconfig.js';
import { getSupportedBrowsers } from 'next/dist/build/utils.js';
import type { Configuration as WebpackConfig } from 'webpack';

import { getNodeModulesExcludeRegex } from '../utils';
Expand Down Expand Up @@ -51,10 +50,26 @@ export const configureSWCLoader = async (
hasReactRefresh: isDevelopment,
jsConfig,
nextConfig,
supportedBrowsers: getSupportedBrowsers(projectRoot, isDevelopment),
supportedBrowsers: await getSupportedBrowsers(projectRoot, isDevelopment),
swcCacheDir: join(projectRoot, nextConfig?.distDir ?? '.next', 'cache', 'swc'),
bundleTarget: 'default',
},
},
});
};

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) {
// 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
);
Comment on lines +62 to +73

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

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.

Suggested change
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.

}
}
Loading