@@ -10,18 +10,38 @@ import { copyTracedFiles } from "@opennextjs/aws/build/copyTracedFiles.js";
1010import { generateEdgeBundle } from "@opennextjs/aws/build/edge/createEdgeBundle.js" ;
1111import * as buildHelper from "@opennextjs/aws/build/helper.js" ;
1212import { installDependencies } from "@opennextjs/aws/build/installDeps.js" ;
13+ import type { CodePatcher } from "@opennextjs/aws/build/patch/codePatcher.js" ;
14+ import { applyCodePatches } from "@opennextjs/aws/build/patch/codePatcher.js" ;
15+ import {
16+ patchFetchCacheForISR ,
17+ patchUnstableCacheForISR ,
18+ } from "@opennextjs/aws/build/patch/patchFetchCacheISR.js" ;
19+ import { patchFetchCacheSetMissingWaitUntil } from "@opennextjs/aws/build/patch/patchFetchCacheWaitUntil.js" ;
1320import logger from "@opennextjs/aws/logger.js" ;
1421import { minifyAll } from "@opennextjs/aws/minimize-js.js" ;
22+ import type { ContentUpdater } from "@opennextjs/aws/plugins/content-updater.js" ;
1523import { openNextEdgePlugins } from "@opennextjs/aws/plugins/edge.js" ;
1624import { openNextExternalMiddlewarePlugin } from "@opennextjs/aws/plugins/externalMiddleware.js" ;
1725import { openNextReplacementPlugin } from "@opennextjs/aws/plugins/replacement.js" ;
1826import { openNextResolvePlugin } from "@opennextjs/aws/plugins/resolve.js" ;
1927import type { FunctionOptions , SplittedFunctionOptions } from "@opennextjs/aws/types/open-next.js" ;
2028import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js" ;
29+ import type { Plugin } from "esbuild" ;
2130
2231import { normalizePath } from "../utils/index.js" ;
2332
24- export async function createServerBundle ( options : buildHelper . BuildOptions ) {
33+ interface CodeCustomization {
34+ // These patches are meant to apply on user and next generated code
35+ additionalCodePatches ?: CodePatcher [ ] ;
36+ // These plugins are meant to apply during the esbuild bundling process.
37+ // This will only apply to OpenNext code.
38+ additionalPlugins ?: ( contentUpdater : ContentUpdater ) => Plugin [ ] ;
39+ }
40+
41+ export async function createServerBundle (
42+ options : buildHelper . BuildOptions ,
43+ codeCustomization ?: CodeCustomization
44+ ) {
2545 const { config } = options ;
2646 const foundRoutes = new Set < string > ( ) ;
2747 // Get all functions to build
@@ -39,7 +59,7 @@ export async function createServerBundle(options: buildHelper.BuildOptions) {
3959 if ( fnOptions . runtime === "edge" ) {
4060 await generateEdgeBundle ( name , options , fnOptions ) ;
4161 } else {
42- await generateBundle ( name , options , fnOptions ) ;
62+ await generateBundle ( name , options , fnOptions , codeCustomization ) ;
4363 }
4464 } ) ;
4565
@@ -102,7 +122,8 @@ export async function createServerBundle(options: buildHelper.BuildOptions) {
102122async function generateBundle (
103123 name : string ,
104124 options : buildHelper . BuildOptions ,
105- fnOptions : SplittedFunctionOptions
125+ fnOptions : SplittedFunctionOptions ,
126+ codeCustomization ?: CodeCustomization
106127) {
107128 const { appPath, appBuildOutputPath, config, outputDir, monorepoRoot } = options ;
108129 logger . info ( `Building server function: ${ name } ...` ) ;
@@ -151,27 +172,37 @@ async function generateBundle(
151172 buildHelper . copyEnvFile ( appBuildOutputPath , packagePath , outputPath ) ;
152173
153174 // Copy all necessary traced files
154- await copyTracedFiles ( {
175+ const { tracedFiles , manifests } = await copyTracedFiles ( {
155176 buildOutputPath : appBuildOutputPath ,
156177 packagePath,
157178 outputDir : outputPath ,
158179 routes : fnOptions . routes ?? [ "app/page.tsx" ] ,
159180 bundledNextServer : isBundled ,
160181 } ) ;
161182
183+ const additionalCodePatches = codeCustomization ?. additionalCodePatches ?? [ ] ;
184+
185+ await applyCodePatches ( options , tracedFiles , manifests , [
186+ patchFetchCacheSetMissingWaitUntil ,
187+ patchFetchCacheForISR ,
188+ patchUnstableCacheForISR ,
189+ ...additionalCodePatches ,
190+ ] ) ;
191+
162192 // Build Lambda code
163193 // note: bundle in OpenNext package b/c the adapter relies on the
164194 // "serverless-http" package which is not a dependency in user's
165195 // Next.js app.
166196
167197 const disableNextPrebundledReact =
168- buildHelper . compareSemver ( options . nextVersion , "13.5.1" ) >= 0 ||
169- buildHelper . compareSemver ( options . nextVersion , "13.4.1" ) <= 0 ;
198+ buildHelper . compareSemver ( options . nextVersion , ">=" , " 13.5.1") ||
199+ buildHelper . compareSemver ( options . nextVersion , "<=" , " 13.4.1") ;
170200
171201 const overrides = fnOptions . override ?? { } ;
172202
173- const isBefore13413 = buildHelper . compareSemver ( options . nextVersion , "13.4.13" ) <= 0 ;
174- const isAfter141 = buildHelper . compareSemver ( options . nextVersion , "14.1" ) >= 0 ;
203+ const isBefore13413 = buildHelper . compareSemver ( options . nextVersion , "<=" , "13.4.13" ) ;
204+ const isAfter141 = buildHelper . compareSemver ( options . nextVersion , ">=" , "14.1" ) ;
205+ const isAfter142 = buildHelper . compareSemver ( options . nextVersion , ">=" , "14.2" ) ;
175206
176207 const disableRouting = isBefore13413 || config . middleware ?. external ;
177208
@@ -182,6 +213,7 @@ async function generateBundle(
182213 deletes : [
183214 ...( disableNextPrebundledReact ? [ "applyNextjsPrebundledReact" ] : [ ] ) ,
184215 ...( disableRouting ? [ "withRouting" ] : [ ] ) ,
216+ ...( isAfter142 ? [ "patchAsyncStorage" ] : [ ] ) ,
185217 ] ,
186218 } ) ,
187219 openNextReplacementPlugin ( {
0 commit comments