Skip to content

Commit

Permalink
[Turbopack] add unstable prefix to persistent caching option and forc…
Browse files Browse the repository at this point in the history
…e to provide an expected level of stability (#71090)

### What?

Rename option to `unstablePersistentCaching` and require to pass a
persistent caching version (currently 1).

We increase the number when making improvements to persistent caching.
Smaller numbers are accepted too, but providing a larger number will
fail the build.

e. g. if we document `experimental.turbo.unstablePersistentCaching: 5`,
this will only work for next version that ship at least persistent
caching with version 5. Older version would be rejected.
  • Loading branch information
sokra authored Oct 11, 2024
1 parent 1caa151 commit cfcfaba
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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,
}
)
Expand Down
4 changes: 3 additions & 1 deletion packages/next/src/server/config-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,9 @@ export const configSchema: zod.ZodType<NextConfig> = 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(),
})
Expand Down
4 changes: 3 additions & 1 deletion packages/next/src/server/config-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion packages/next/src/server/dev/hot-reloader-turbopack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import {
isWellKnownError,
printNonFatalIssue,
normalizedPageToTurbopackStructureRoute,
isPersistentCachingEnabled,
} from './turbopack-utils'
import {
propagateServerField,
Expand Down Expand Up @@ -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,
}
)
Expand Down
12 changes: 12 additions & 0 deletions packages/next/src/server/dev/turbopack-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit cfcfaba

Please sign in to comment.