@@ -16,10 +16,11 @@ import { join as posixJoin, sep as posixSep } from 'node:path/posix'
1616import { trace } from '@opentelemetry/api'
1717import { wrapTracer } from '@opentelemetry/api/experimental'
1818import glob from 'fast-glob'
19- import { prerelease , lt as semverLowerThan , lte as semverLowerThanOrEqual } from 'semver'
19+ import { prerelease , satisfies , lt as semverLowerThan , lte as semverLowerThanOrEqual } from 'semver'
2020
21- import { RUN_CONFIG } from '../../run/constants.js'
22- import { PluginContext } from '../plugin-context.js'
21+ import type { RunConfig } from '../../run/config.js'
22+ import { RUN_CONFIG_FILE } from '../../run/constants.js'
23+ import type { PluginContext , RequiredServerFilesManifest } from '../plugin-context.js'
2324
2425const tracer = wrapTracer ( trace . getTracer ( 'Next runtime' ) )
2526
@@ -54,7 +55,9 @@ export const copyNextServerCode = async (ctx: PluginContext): Promise<void> => {
5455 throw error
5556 }
5657 }
57- const reqServerFiles = JSON . parse ( await readFile ( reqServerFilesPath , 'utf-8' ) )
58+ const reqServerFiles = JSON . parse (
59+ await readFile ( reqServerFilesPath , 'utf-8' ) ,
60+ ) as RequiredServerFilesManifest
5861
5962 // if the resolved dist folder does not match the distDir of the required-server-files.json
6063 // this means the path got altered by a plugin like nx and contained ../../ parts so we have to reset it
@@ -73,8 +76,17 @@ export const copyNextServerCode = async (ctx: PluginContext): Promise<void> => {
7376 // write our run-config.json to the root dir so that we can easily get the runtime config of the required-server-files.json
7477 // without the need to know about the monorepo or distDir configuration upfront.
7578 await writeFile (
76- join ( ctx . serverHandlerDir , RUN_CONFIG ) ,
77- JSON . stringify ( reqServerFiles . config ) ,
79+ join ( ctx . serverHandlerDir , RUN_CONFIG_FILE ) ,
80+ JSON . stringify ( {
81+ nextConfig : reqServerFiles . config ,
82+ // only enable setting up 'use cache' handler when Next.js supports CacheHandlerV2 as we don't have V1 compatible implementation
83+ // see https://github.com/vercel/next.js/pull/76687 first released in v15.3.0-canary.13
84+ enableUseCacheHandler : ctx . nextVersion
85+ ? satisfies ( ctx . nextVersion , '>=15.3.0-canary.13' , {
86+ includePrerelease : true ,
87+ } )
88+ : false ,
89+ } satisfies RunConfig ) ,
7890 'utf-8' ,
7991 )
8092
@@ -336,9 +348,11 @@ const replaceMiddlewareManifest = async (sourcePath: string, destPath: string) =
336348}
337349
338350export const verifyHandlerDirStructure = async ( ctx : PluginContext ) => {
339- const runConfig = JSON . parse ( await readFile ( join ( ctx . serverHandlerDir , RUN_CONFIG ) , 'utf-8' ) )
351+ const { nextConfig } = JSON . parse (
352+ await readFile ( join ( ctx . serverHandlerDir , RUN_CONFIG_FILE ) , 'utf-8' ) ,
353+ ) as RunConfig
340354
341- const expectedBuildIDPath = join ( ctx . serverHandlerDir , runConfig . distDir , 'BUILD_ID' )
355+ const expectedBuildIDPath = join ( ctx . serverHandlerDir , nextConfig . distDir , 'BUILD_ID' )
342356 if ( ! existsSync ( expectedBuildIDPath ) ) {
343357 ctx . failBuild (
344358 `Failed creating server handler. BUILD_ID file not found at expected location "${ expectedBuildIDPath } ".` ,
0 commit comments