diff --git a/packages/next/src/build/index.ts b/packages/next/src/build/index.ts index eb651d013b0ed..f550398420c37 100644 --- a/packages/next/src/build/index.ts +++ b/packages/next/src/build/index.ts @@ -188,6 +188,7 @@ import { handlePagesErrorRoute, formatIssue, isRelevantWarning, + isPersistentCachingEnabled, } from '../server/dev/turbopack-utils' import { TurbopackManifestLoader } from '../server/dev/turbopack/manifest-loader' import type { Entrypoints } from '../server/dev/turbopack/types' @@ -1409,7 +1410,7 @@ export default async function build( browserslistQuery: supportedBrowsers.join(', '), }, { - persistentCaching: config.experimental.turbo?.persistentCaching, + persistentCaching: isPersistentCachingEnabled(config), memoryLimit: config.experimental.turbo?.memoryLimit, } ) diff --git a/packages/next/src/server/config-schema.ts b/packages/next/src/server/config-schema.ts index f335d83233ef1..19c0d40c10c67 100644 --- a/packages/next/src/server/config-schema.ts +++ b/packages/next/src/server/config-schema.ts @@ -391,7 +391,9 @@ export const configSchema: zod.ZodType = z.lazy(() => resolveExtensions: z.array(z.string()).optional(), useSwcCss: z.boolean().optional(), treeShaking: z.boolean().optional(), - persistentCaching: z.boolean().optional(), + persistentCaching: z + .union([z.number(), z.literal(false)]) + .optional(), memoryLimit: z.number().optional(), moduleIdStrategy: z.enum(['named', 'deterministic']).optional(), }) diff --git a/packages/next/src/server/config-shared.ts b/packages/next/src/server/config-shared.ts index 1bb98ec08579e..cfd0e38c2971d 100644 --- a/packages/next/src/server/config-shared.ts +++ b/packages/next/src/server/config-shared.ts @@ -156,8 +156,10 @@ export interface ExperimentalTurboOptions { /** * Enable persistent caching for the turbopack dev server and build. + * Need to provide the expected level of stability, otherwise it will fail. + * Currently stability level: 1 */ - persistentCaching?: boolean + unstablePersistentCaching?: number | false /** * Enable tree shaking for the turbopack dev server and build. diff --git a/packages/next/src/server/dev/hot-reloader-turbopack.ts b/packages/next/src/server/dev/hot-reloader-turbopack.ts index 4df06edc81bd8..fdda62bcb0915 100644 --- a/packages/next/src/server/dev/hot-reloader-turbopack.ts +++ b/packages/next/src/server/dev/hot-reloader-turbopack.ts @@ -60,6 +60,7 @@ import { isWellKnownError, printNonFatalIssue, normalizedPageToTurbopackStructureRoute, + isPersistentCachingEnabled, } from './turbopack-utils' import { propagateServerField, @@ -176,7 +177,7 @@ export async function createHotReloaderTurbopack( browserslistQuery: supportedBrowsers.join(', '), }, { - persistentCaching: opts.nextConfig.experimental.turbo?.persistentCaching, + persistentCaching: isPersistentCachingEnabled(opts.nextConfig), memoryLimit: opts.nextConfig.experimental.turbo?.memoryLimit, } ) diff --git a/packages/next/src/server/dev/turbopack-utils.ts b/packages/next/src/server/dev/turbopack-utils.ts index 211f5204c3a44..be1c11155e207 100644 --- a/packages/next/src/server/dev/turbopack-utils.ts +++ b/packages/next/src/server/dev/turbopack-utils.ts @@ -1159,3 +1159,15 @@ export function normalizedPageToTurbopackStructureRoute( } return entrypointKey } + +export function isPersistentCachingEnabled( + config: NextConfigComplete +): boolean { + const unstableValue = config.experimental.turbo?.unstablePersistentCaching + if (typeof unstableValue === 'number' && unstableValue > 1) { + throw new Error( + 'Persistent caching in this version of Turbopack is not as stable as expected. Upgrade to a newer version of Turbopack to use this feature with the expected stability.' + ) + } + return !!unstableValue +}