From ec66cb67c2d5ac1bd03bce7ad9fdf3bf384d7fb6 Mon Sep 17 00:00:00 2001 From: Yann Braga Date: Tue, 14 Oct 2025 12:41:58 +0200 Subject: [PATCH] Nextjs: Fix Nextjs version detection with prereleases --- code/frameworks/nextjs/src/config/webpack.ts | 6 ++---- code/frameworks/nextjs/src/preset.ts | 5 ++--- code/frameworks/nextjs/src/utils.ts | 7 +++++++ 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/code/frameworks/nextjs/src/config/webpack.ts b/code/frameworks/nextjs/src/config/webpack.ts index 369f885557bc..340cf65cea86 100644 --- a/code/frameworks/nextjs/src/config/webpack.ts +++ b/code/frameworks/nextjs/src/config/webpack.ts @@ -1,13 +1,11 @@ import { fileURLToPath } from 'node:url'; import type { NextConfig } from 'next'; -import semver from 'semver'; import type { Configuration as WebpackConfig } from 'webpack'; -import { addScopedAlias, getNextjsVersion, resolveNextConfig } from '../utils'; +import { addScopedAlias, isNextVersionGte, resolveNextConfig } from '../utils'; -const nextjsVersion = getNextjsVersion(); -const isNext16orNewer = semver.gte(nextjsVersion, '16.0.0'); +const isNext16orNewer = isNextVersionGte('16.0.0'); const tryResolve = (path: string) => { try { diff --git a/code/frameworks/nextjs/src/preset.ts b/code/frameworks/nextjs/src/preset.ts index 27b5c5ed1a18..86da3fba457a 100644 --- a/code/frameworks/nextjs/src/preset.ts +++ b/code/frameworks/nextjs/src/preset.ts @@ -15,7 +15,7 @@ import nextBabelPreset from './babel/preset'; import { configureConfig } from './config/webpack'; import TransformFontImports from './font/babel'; import type { FrameworkOptions, StorybookConfig } from './types'; -import { getNextjsVersion } from './utils'; +import { isNextVersionGte } from './utils'; export const addons: PresetProperty<'addons'> = [ fileURLToPath(import.meta.resolve('@storybook/preset-react-webpack')), @@ -51,8 +51,7 @@ export const core: PresetProperty<'core'> = async (config, options) => { export const previewAnnotations: PresetProperty<'previewAnnotations'> = (entry = []) => { const annotations = [...entry, fileURLToPath(import.meta.resolve('@storybook/nextjs/preview'))]; - const nextjsVersion = getNextjsVersion(); - const isNext16orNewer = semver.gte(nextjsVersion, '16.0.0'); + const isNext16orNewer = isNextVersionGte('16.0.0'); // TODO: Remove this once we only support Next.js v16 and above if (!isNext16orNewer) { diff --git a/code/frameworks/nextjs/src/utils.ts b/code/frameworks/nextjs/src/utils.ts index 21f838e71ebf..9193e884f097 100644 --- a/code/frameworks/nextjs/src/utils.ts +++ b/code/frameworks/nextjs/src/utils.ts @@ -9,6 +9,7 @@ import { WebpackDefinePlugin } from '@storybook/builder-webpack5'; import type { NextConfig } from 'next'; import { PHASE_DEVELOPMENT_SERVER } from 'next/constants.js'; import nextJsLoadConfigModule from 'next/dist/server/config.js'; +import semver from 'semver'; import type { Configuration as WebpackConfig } from 'webpack'; import { resolvePackageDir } from '../../../core/src/shared/utils/module'; @@ -24,6 +25,12 @@ export const configureRuntimeNextjsVersionResolution = (baseConfig: WebpackConfi export const getNextjsVersion = (): string => JSON.parse(readFileSync(join(resolvePackageDir('next'), 'package.json'), 'utf8')).version; +export const isNextVersionGte = (version: string): boolean => { + const currentVersion = getNextjsVersion(); + const coercedVersion = semver.coerce(currentVersion); + return coercedVersion ? semver.gte(coercedVersion, version) : false; +}; + export const resolveNextConfig = async ({ nextConfigPath, }: {