@@ -20,18 +20,22 @@ export const copyNextServerCode = async (ctx: PluginContext): Promise<void> => {
20
20
21
21
await Promise . all (
22
22
paths . map ( async ( path : string ) => {
23
+ const srcPath = join ( srcDir , path )
23
24
const destPath = join ( destDir , path )
24
25
25
26
// If this is the middleware manifest file, replace it with an empty
26
27
// manifest to avoid running middleware again in the server handler.
27
28
if ( path === 'server/middleware-manifest.json' ) {
28
- await mkdir ( dirname ( destPath ) , { recursive : true } )
29
- await writeFile ( destPath , getEmptyMiddlewareManifest ( ) )
29
+ try {
30
+ await replaceMiddlewareManifest ( srcPath , destPath )
31
+ } catch ( error ) {
32
+ throw new Error ( 'Could not patch middleware manifest file' , { cause : error } )
33
+ }
30
34
31
35
return
32
36
}
33
37
34
- await cp ( join ( srcDir , path ) , destPath , { recursive : true } )
38
+ await cp ( srcPath , destPath , { recursive : true } )
35
39
} ) ,
36
40
)
37
41
}
@@ -125,17 +129,25 @@ export const writeTagsManifest = async (ctx: PluginContext): Promise<void> => {
125
129
}
126
130
127
131
/**
128
- * Generates an empty middleware manifest. We don't want to run middleware in
129
- * the server handler, because we'll run it upstream in an edge function. So
130
- * we patch the manifest to make it seem like there's no middleware configured .
132
+ * Generates a copy of the middleware manifest without any middleware in it. We
133
+ * do this because we'll run middleware in an edge function, and we don't want
134
+ * to run it again in the server handler .
131
135
*/
132
- const getEmptyMiddlewareManifest = ( ) => {
133
- const manifest = {
134
- sortedMiddleware : [ ] ,
136
+ const replaceMiddlewareManifest = async ( sourcePath : string , destPath : string ) => {
137
+ await mkdir ( dirname ( destPath ) , { recursive : true } )
138
+
139
+ const data = await readFile ( sourcePath , 'utf8' )
140
+ const manifest = JSON . parse ( data )
141
+
142
+ // TODO: Check for `manifest.version` and write an error to the system log
143
+ // when we find a value that is not equal to 2. This will alert us in case
144
+ // Next.js starts using a new format for the manifest and we're writing
145
+ // one with the old version.
146
+ const newManifest = {
147
+ ...manifest ,
135
148
middleware : { } ,
136
- functions : { } ,
137
- version : 2 ,
138
149
}
150
+ const newData = JSON . stringify ( newManifest )
139
151
140
- return JSON . stringify ( manifest )
152
+ await writeFile ( destPath , newData )
141
153
}
0 commit comments