@@ -8,11 +8,6 @@ import { pathToRegexp } from 'path-to-regexp'
88
99import { EDGE_HANDLER_NAME , PluginContext } from '../plugin-context.js'
1010
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-
1611const copyRuntime = async ( ctx : PluginContext , handlerDirectory : string ) : Promise < void > => {
1712 const files = await glob ( 'edge-runtime/**/*' , {
1813 cwd : ctx . pluginDir ,
@@ -63,7 +58,12 @@ const augmentMatchers = (
6358 } )
6459}
6560
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
6767 const nextConfig = ctx . buildConfig
6868 const handlerName = getHandlerName ( { name } )
6969 const handlerDirectory = join ( ctx . edgeFunctionsDir , handlerName )
@@ -75,7 +75,12 @@ const writeHandlerFile = async (ctx: PluginContext, { matchers, name }: NextDefi
7575
7676 // Writing a file with the matchers that should trigger this function. We'll
7777 // 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+ }
7984
8085 // The config is needed by the edge function to match and normalize URLs. To
8186 // 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
99104 ) ,
100105 )
101106
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+
102135 // Writing the function entry file. It wraps the middleware code with the
103136 // 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 )
118138}
119139
120140const copyHandlerDependencies = async (
@@ -161,47 +181,55 @@ const copyHandlerDependencies = async (
161181 await writeFile ( outputFile , parts . join ( '\n' ) )
162182}
163183
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 > => {
165189 await copyHandlerDependencies ( ctx , definition )
166- await writeHandlerFile ( ctx , definition )
190+ await writeHandlerFile ( ctx , definition , { isFrameworksAPI } )
167191}
168192
169193const getHandlerName = ( { name } : Pick < NextDefinition , 'name' > ) : string =>
170194 `${ EDGE_HANDLER_NAME } -${ name . replace ( / \W / g, '-' ) } `
171195
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-
192196export const clearStaleEdgeHandlers = async ( ctx : PluginContext ) => {
193197 await rm ( ctx . edgeFunctionsDir , { recursive : true , force : true } )
194198}
195199
196200export const createEdgeHandlers = async ( ctx : PluginContext ) => {
197201 const nextManifest = await ctx . getMiddlewareManifest ( )
198202 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+ )
205234 }
206- await writeEdgeManifest ( ctx , netlifyManifest )
207235}
0 commit comments