@@ -16,10 +16,12 @@ 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 type { NextConfigComplete } from 'next/dist/server/config-shared.js'
20+ import { prerelease , satisfies , lt as semverLowerThan , lte as semverLowerThanOrEqual } from 'semver'
2021
21- import { RUN_CONFIG } from '../../run/constants.js'
22- import { PluginContext } from '../plugin-context.js'
22+ import type { RunConfig } from '../../run/config.js'
23+ import { RUN_CONFIG_FILE } from '../../run/constants.js'
24+ import type { PluginContext , RequiredServerFilesManifest } from '../plugin-context.js'
2325
2426const tracer = wrapTracer ( trace . getTracer ( 'Next runtime' ) )
2527
@@ -32,8 +34,8 @@ function isError(error: unknown): error is NodeJS.ErrnoException {
3234/**
3335 * Copy App/Pages Router Javascript needed by the server handler
3436 */
35- export const copyNextServerCode = async ( ctx : PluginContext ) : Promise < void > => {
36- await tracer . withActiveSpan ( 'copyNextServerCode' , async ( ) => {
37+ export const copyNextServerCode = async ( ctx : PluginContext ) : Promise < NextConfigComplete > => {
38+ return await tracer . withActiveSpan ( 'copyNextServerCode' , async ( ) => {
3739 // update the dist directory inside the required-server-files.json to work with
3840 // nx monorepos and other setups where the dist directory is modified
3941 const reqServerFilesPath = join (
@@ -54,7 +56,9 @@ export const copyNextServerCode = async (ctx: PluginContext): Promise<void> => {
5456 throw error
5557 }
5658 }
57- const reqServerFiles = JSON . parse ( await readFile ( reqServerFilesPath , 'utf-8' ) )
59+ const reqServerFiles = JSON . parse (
60+ await readFile ( reqServerFilesPath , 'utf-8' ) ,
61+ ) as RequiredServerFilesManifest
5862
5963 // if the resolved dist folder does not match the distDir of the required-server-files.json
6064 // this means the path got altered by a plugin like nx and contained ../../ parts so we have to reset it
@@ -73,8 +77,17 @@ export const copyNextServerCode = async (ctx: PluginContext): Promise<void> => {
7377 // write our run-config.json to the root dir so that we can easily get the runtime config of the required-server-files.json
7478 // without the need to know about the monorepo or distDir configuration upfront.
7579 await writeFile (
76- join ( ctx . serverHandlerDir , RUN_CONFIG ) ,
77- JSON . stringify ( reqServerFiles . config ) ,
80+ join ( ctx . serverHandlerDir , RUN_CONFIG_FILE ) ,
81+ JSON . stringify ( {
82+ nextConfig : reqServerFiles . config ,
83+ // only enable setting up 'use cache' handler when Next.js supports CacheHandlerV2 as we don't have V1 compatible implementation
84+ // see https://github.com/vercel/next.js/pull/76687 first released in v15.3.0-canary.13
85+ enableUseCacheHandler : ctx . nextVersion
86+ ? satisfies ( ctx . nextVersion , '>=15.3.0-canary.13' , {
87+ includePrerelease : true ,
88+ } )
89+ : false ,
90+ } satisfies RunConfig ) ,
7891 'utf-8' ,
7992 )
8093
@@ -336,9 +349,11 @@ const replaceMiddlewareManifest = async (sourcePath: string, destPath: string) =
336349}
337350
338351export const verifyHandlerDirStructure = async ( ctx : PluginContext ) => {
339- const runConfig = JSON . parse ( await readFile ( join ( ctx . serverHandlerDir , RUN_CONFIG ) , 'utf-8' ) )
352+ const { nextConfig } = JSON . parse (
353+ await readFile ( join ( ctx . serverHandlerDir , RUN_CONFIG_FILE ) , 'utf-8' ) ,
354+ ) as RunConfig
340355
341- const expectedBuildIDPath = join ( ctx . serverHandlerDir , runConfig . distDir , 'BUILD_ID' )
356+ const expectedBuildIDPath = join ( ctx . serverHandlerDir , nextConfig . distDir , 'BUILD_ID' )
342357 if ( ! existsSync ( expectedBuildIDPath ) ) {
343358 ctx . failBuild (
344359 `Failed creating server handler. BUILD_ID file not found at expected location "${ expectedBuildIDPath } ".` ,
0 commit comments