From b8475b94e2a90c3372388fb17272b28d4a407d87 Mon Sep 17 00:00:00 2001 From: Kyle Gach Date: Thu, 17 Nov 2022 14:20:17 -0700 Subject: [PATCH] Fix next@13 `next/link` --- code/frameworks/nextjs/src/preset.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/code/frameworks/nextjs/src/preset.ts b/code/frameworks/nextjs/src/preset.ts index 2833d9c5752f..a40204593c60 100644 --- a/code/frameworks/nextjs/src/preset.ts +++ b/code/frameworks/nextjs/src/preset.ts @@ -1,5 +1,6 @@ // https://storybook.js.org/docs/react/addons/writing-presets import { dirname, join } from 'path'; +import semver from 'semver'; import type { Options, PresetProperty } from '@storybook/types'; import type { TransformOptions } from '@babel/core'; import { configureConfig } from './config/webpack'; @@ -9,7 +10,7 @@ import { configureRouting } from './routing/webpack'; import { configureStyledJsx } from './styledJsx/webpack'; import { configureStyledJsxTransforms } from './styledJsx/babel'; import { configureImages } from './images/webpack'; -import { configureRuntimeNextjsVersionResolution } from './utils'; +import { configureRuntimeNextjsVersionResolution, getNextjsVersion } from './utils'; import type { FrameworkOptions, StorybookConfig } from './types'; import { configureTypescript } from './config/babel'; @@ -18,6 +19,8 @@ export const addons: PresetProperty<'addons', StorybookConfig> = [ dirname(require.resolve(join('@storybook/builder-webpack5', 'package.json'))), ]; +const version = getNextjsVersion(); + const defaultFrameworkOptions: FrameworkOptions = {}; export const frameworkOptions = async ( @@ -68,6 +71,17 @@ export const config: StorybookConfig['previewAnnotations'] = (entry = []) => [ require.resolve('@storybook/nextjs/preview.js'), ]; +export const env = (envConfig: PresetProperty<'env', StorybookConfig>) => { + return { + ...envConfig, + // Can be removed if https://github.com/vercel/next.js/issues/42621 is resolved + ...(semver.gte(version, '13.0.0') && { + // TODO: This should also respect `newNextLinkBehavior`: https://github.com/vercel/next.js/blob/07d3da102dfef65be9c13fd4b754a12eda7eded1/packages/next/server/config-shared.ts#L88 + __NEXT_NEW_LINK_BEHAVIOR: 'true', + }), + }; +}; + // Not even sb init - automigrate - running dev // You're using a version of Nextjs prior to v10, which is unsupported by this framework.