@@ -16,10 +16,11 @@ import { join as posixJoin, sep as posixSep } from 'node:path/posix'
16
16
import { trace } from '@opentelemetry/api'
17
17
import { wrapTracer } from '@opentelemetry/api/experimental'
18
18
import 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'
20
20
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'
23
24
24
25
const tracer = wrapTracer ( trace . getTracer ( 'Next runtime' ) )
25
26
@@ -54,7 +55,9 @@ export const copyNextServerCode = async (ctx: PluginContext): Promise<void> => {
54
55
throw error
55
56
}
56
57
}
57
- const reqServerFiles = JSON . parse ( await readFile ( reqServerFilesPath , 'utf-8' ) )
58
+ const reqServerFiles = JSON . parse (
59
+ await readFile ( reqServerFilesPath , 'utf-8' ) ,
60
+ ) as RequiredServerFilesManifest
58
61
59
62
// if the resolved dist folder does not match the distDir of the required-server-files.json
60
63
// 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> => {
73
76
// write our run-config.json to the root dir so that we can easily get the runtime config of the required-server-files.json
74
77
// without the need to know about the monorepo or distDir configuration upfront.
75
78
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 ) ,
78
90
'utf-8' ,
79
91
)
80
92
@@ -336,9 +348,11 @@ const replaceMiddlewareManifest = async (sourcePath: string, destPath: string) =
336
348
}
337
349
338
350
export 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
340
354
341
- const expectedBuildIDPath = join ( ctx . serverHandlerDir , runConfig . distDir , 'BUILD_ID' )
355
+ const expectedBuildIDPath = join ( ctx . serverHandlerDir , nextConfig . distDir , 'BUILD_ID' )
342
356
if ( ! existsSync ( expectedBuildIDPath ) ) {
343
357
ctx . failBuild (
344
358
`Failed creating server handler. BUILD_ID file not found at expected location "${ expectedBuildIDPath } ".` ,
0 commit comments