@@ -8,11 +8,6 @@ import { pathToRegexp } from 'path-to-regexp'
8
8
9
9
import { EDGE_HANDLER_NAME , PluginContext } from '../plugin-context.js'
10
10
11
- const writeEdgeManifest = async ( ctx : PluginContext , manifest : Manifest ) => {
12
- await mkdir ( ctx . edgeFunctionsDir , { recursive : true } )
13
- await writeFile ( join ( ctx . edgeFunctionsDir , 'manifest.json' ) , JSON . stringify ( manifest , null , 2 ) )
14
- }
15
-
16
11
const copyRuntime = async ( ctx : PluginContext , handlerDirectory : string ) : Promise < void > => {
17
12
const files = await glob ( 'edge-runtime/**/*' , {
18
13
cwd : ctx . pluginDir ,
@@ -63,7 +58,12 @@ const augmentMatchers = (
63
58
} )
64
59
}
65
60
66
- const writeHandlerFile = async ( ctx : PluginContext , { matchers, name } : NextDefinition ) => {
61
+ const writeHandlerFile = async (
62
+ ctx : PluginContext ,
63
+ definition : NextDefinition ,
64
+ { isFrameworksAPI } : { isFrameworksAPI : boolean } ,
65
+ ) => {
66
+ const { name, page } = definition
67
67
const nextConfig = ctx . buildConfig
68
68
const handlerName = getHandlerName ( { name } )
69
69
const handlerDirectory = join ( ctx . edgeFunctionsDir , handlerName )
@@ -75,7 +75,12 @@ const writeHandlerFile = async (ctx: PluginContext, { matchers, name }: NextDefi
75
75
76
76
// Writing a file with the matchers that should trigger this function. We'll
77
77
// read this file from the function at runtime.
78
- await writeFile ( join ( handlerRuntimeDirectory , 'matchers.json' ) , JSON . stringify ( matchers ) )
78
+ if ( ! isFrameworksAPI ) {
79
+ await writeFile (
80
+ join ( handlerRuntimeDirectory , 'matchers.json' ) ,
81
+ JSON . stringify ( definition . matchers ) ,
82
+ )
83
+ }
79
84
80
85
// The config is needed by the edge function to match and normalize URLs. To
81
86
// avoid shipping and parsing a large file at runtime, let's strip it down to
@@ -99,22 +104,37 @@ const writeHandlerFile = async (ctx: PluginContext, { matchers, name }: NextDefi
99
104
) ,
100
105
)
101
106
107
+ let handlerFileContents = `
108
+ import { init as htmlRewriterInit } from './edge-runtime/vendor/deno.land/x/[email protected] /src/index.ts'
109
+ import { handleMiddleware } from './edge-runtime/middleware.ts';
110
+ import handler from './server/${ name } .js';
111
+
112
+ await htmlRewriterInit({ module_or_path: Uint8Array.from(${ JSON . stringify ( [
113
+ ...htmlRewriterWasm ,
114
+ ] ) } ) });
115
+
116
+ export default (req, context) => handleMiddleware(req, context, handler);
117
+ `
118
+
119
+ if ( isFrameworksAPI ) {
120
+ const augmentedMatchers = augmentMatchers ( definition . matchers , ctx )
121
+
122
+ const config = {
123
+ path : augmentedMatchers . map ( ( matcher ) => matcher . regexp ) ,
124
+ // TODO: this is not correct, we need to handle excluded paths
125
+ excludedPath : [ ] ,
126
+ name : name . endsWith ( 'middleware' )
127
+ ? 'Next.js Middleware Handler'
128
+ : `Next.js Edge Handler: ${ page } ` ,
129
+ generator : `${ ctx . pluginName } @${ ctx . pluginVersion } ` ,
130
+ cache : name . endsWith ( 'middleware' ) ? undefined : 'manual' ,
131
+ }
132
+ handlerFileContents += `\nexport const config = ${ JSON . stringify ( config , null , 2 ) } `
133
+ }
134
+
102
135
// Writing the function entry file. It wraps the middleware code with the
103
136
// compatibility layer mentioned above.
104
- await writeFile (
105
- join ( handlerDirectory , `${ handlerName } .js` ) ,
106
- `
107
- import { init as htmlRewriterInit } from './edge-runtime/vendor/deno.land/x/[email protected] /src/index.ts'
108
- import { handleMiddleware } from './edge-runtime/middleware.ts';
109
- import handler from './server/${ name } .js';
110
-
111
- await htmlRewriterInit({ module_or_path: Uint8Array.from(${ JSON . stringify ( [
112
- ...htmlRewriterWasm ,
113
- ] ) } ) });
114
-
115
- export default (req, context) => handleMiddleware(req, context, handler);
116
- ` ,
117
- )
137
+ await writeFile ( join ( handlerDirectory , `${ handlerName } .js` ) , handlerFileContents )
118
138
}
119
139
120
140
const copyHandlerDependencies = async (
@@ -161,47 +181,55 @@ const copyHandlerDependencies = async (
161
181
await writeFile ( outputFile , parts . join ( '\n' ) )
162
182
}
163
183
164
- const createEdgeHandler = async ( ctx : PluginContext , definition : NextDefinition ) : Promise < void > => {
184
+ const createEdgeHandler = async (
185
+ ctx : PluginContext ,
186
+ definition : NextDefinition ,
187
+ { isFrameworksAPI } : { isFrameworksAPI : boolean } ,
188
+ ) : Promise < void > => {
165
189
await copyHandlerDependencies ( ctx , definition )
166
- await writeHandlerFile ( ctx , definition )
190
+ await writeHandlerFile ( ctx , definition , { isFrameworksAPI } )
167
191
}
168
192
169
193
const getHandlerName = ( { name } : Pick < NextDefinition , 'name' > ) : string =>
170
194
`${ EDGE_HANDLER_NAME } -${ name . replace ( / \W / g, '-' ) } `
171
195
172
- const buildHandlerDefinition = (
173
- ctx : PluginContext ,
174
- { name, matchers, page } : NextDefinition ,
175
- ) : Array < ManifestFunction > => {
176
- const functionHandlerName = getHandlerName ( { name } )
177
- const functionName = name . endsWith ( 'middleware' )
178
- ? 'Next.js Middleware Handler'
179
- : `Next.js Edge Handler: ${ page } `
180
- const cache = name . endsWith ( 'middleware' ) ? undefined : ( 'manual' as const )
181
- const generator = `${ ctx . pluginName } @${ ctx . pluginVersion } `
182
-
183
- return augmentMatchers ( matchers , ctx ) . map ( ( matcher ) => ( {
184
- function : functionHandlerName ,
185
- name : functionName ,
186
- pattern : matcher . regexp ,
187
- cache,
188
- generator,
189
- } ) )
190
- }
191
-
192
196
export const clearStaleEdgeHandlers = async ( ctx : PluginContext ) => {
193
197
await rm ( ctx . edgeFunctionsDir , { recursive : true , force : true } )
194
198
}
195
199
196
200
export const createEdgeHandlers = async ( ctx : PluginContext ) => {
197
201
const nextManifest = await ctx . getMiddlewareManifest ( )
198
202
const nextDefinitions = [ ...Object . values ( nextManifest . middleware ) ]
199
- await Promise . all ( nextDefinitions . map ( ( def ) => createEdgeHandler ( ctx , def ) ) )
200
-
201
- const netlifyDefinitions = nextDefinitions . flatMap ( ( def ) => buildHandlerDefinition ( ctx , def ) )
202
- const netlifyManifest : Manifest = {
203
- version : 1 ,
204
- functions : netlifyDefinitions ,
203
+ const isFrameworksAPI = ctx . shouldUseFrameworksAPI
204
+
205
+ await Promise . all ( nextDefinitions . map ( ( def ) => createEdgeHandler ( ctx , def , { isFrameworksAPI } ) ) )
206
+
207
+ if ( ! isFrameworksAPI ) {
208
+ const netlifyDefinitions = nextDefinitions . flatMap ( ( def ) => {
209
+ const { name, matchers, page } = def
210
+ const functionHandlerName = getHandlerName ( { name } )
211
+ const functionName = name . endsWith ( 'middleware' )
212
+ ? 'Next.js Middleware Handler'
213
+ : `Next.js Edge Handler: ${ page } `
214
+ const cache = name . endsWith ( 'middleware' ) ? undefined : ( 'manual' as const )
215
+ const generator = `${ ctx . pluginName } @${ ctx . pluginVersion } `
216
+
217
+ return augmentMatchers ( matchers , ctx ) . map ( ( matcher ) => ( {
218
+ function : functionHandlerName ,
219
+ name : functionName ,
220
+ pattern : matcher . regexp ,
221
+ cache,
222
+ generator,
223
+ } ) )
224
+ } )
225
+ const netlifyManifest : Manifest = {
226
+ version : 1 ,
227
+ functions : netlifyDefinitions ,
228
+ }
229
+ await mkdir ( ctx . edgeFunctionsDir , { recursive : true } )
230
+ await writeFile (
231
+ join ( ctx . edgeFunctionsDir , 'manifest.json' ) ,
232
+ JSON . stringify ( netlifyManifest , null , 2 ) ,
233
+ )
205
234
}
206
- await writeEdgeManifest ( ctx , netlifyManifest )
207
235
}
0 commit comments