1
1
import { cp , mkdir , readFile , rm , writeFile } from 'node:fs/promises'
2
2
import { dirname , join } from 'node:path'
3
3
4
- import type { IntegrationsConfig } from '@netlify/edge-functions'
4
+ import type { IntegrationsConfig , Manifest , ManifestFunction } from '@netlify/edge-functions'
5
5
import { glob } from 'fast-glob'
6
6
import type { EdgeFunctionDefinition as NextDefinition } from 'next/dist/build/webpack/plugins/middleware-plugin.js'
7
7
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
- // }
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
15
16
16
const copyRuntime = async ( ctx : PluginContext , handlerDirectory : string ) : Promise < void > => {
17
17
const files = await glob ( 'edge-runtime/**/*' , {
@@ -84,6 +84,17 @@ const writeHandlerFile = async (ctx: PluginContext, { matchers, name, page }: Ne
84
84
JSON . stringify ( minimalNextConfig ) ,
85
85
)
86
86
87
+ const isc = ctx . useFrameworksAPI
88
+ ? `export const config = ${ JSON . stringify ( {
89
+ name : name . endsWith ( 'middleware' )
90
+ ? 'Next.js Middleware Handler'
91
+ : `Next.js Edge Handler: ${ page } ` ,
92
+ pattern : augmentedMatchers . map ( ( matcher ) => matcher . regexp ) ,
93
+ cache : name . endsWith ( 'middleware' ) ? undefined : 'manual' ,
94
+ generator : `${ ctx . pluginName } @${ ctx . pluginVersion } ` ,
95
+ } satisfies IntegrationsConfig ) } ;`
96
+ : ``
97
+
87
98
// Writing the function entry file. It wraps the middleware code with the
88
99
// compatibility layer mentioned above.
89
100
await writeFile (
@@ -92,16 +103,7 @@ const writeHandlerFile = async (ctx: PluginContext, { matchers, name, page }: Ne
92
103
import {handleMiddleware} from './edge-runtime/middleware.ts';
93
104
import handler from './server/${ name } .js';
94
105
export default (req, context) => handleMiddleware(req, context, handler);
95
-
96
- export const config = ${ JSON . stringify ( {
97
- name : name . endsWith ( 'middleware' )
98
- ? 'Next.js Middleware Handler'
99
- : `Next.js Edge Handler: ${ page } ` ,
100
- pattern : augmentedMatchers . map ( ( matcher ) => matcher . regexp ) ,
101
- cache : name . endsWith ( 'middleware' ) ? undefined : 'manual' ,
102
- generator : `${ ctx . pluginName } @${ ctx . pluginVersion } ` ,
103
- } satisfies IntegrationsConfig ) } ;
104
- ` ,
106
+ ${ isc } ` ,
105
107
)
106
108
}
107
109
@@ -150,25 +152,25 @@ const createEdgeHandler = async (ctx: PluginContext, definition: NextDefinition)
150
152
const getHandlerName = ( { name } : Pick < NextDefinition , 'name' > ) : string =>
151
153
`${ EDGE_HANDLER_NAME } -${ name . replace ( / \W / g, '-' ) } `
152
154
153
- // const buildHandlerDefinition = (
154
- // ctx: PluginContext,
155
- // { name, matchers, page }: NextDefinition,
156
- // ): Array<ManifestFunction> => {
157
- // const fun = getHandlerName({ name })
158
- // const funName = name.endsWith('middleware')
159
- // ? 'Next.js Middleware Handler'
160
- // : `Next.js Edge Handler: ${page}`
161
- // const cache = name.endsWith('middleware') ? undefined : ('manual' as const)
162
- // const generator = `${ctx.pluginName}@${ctx.pluginVersion}`
163
-
164
- // return augmentMatchers(matchers, ctx).map((matcher) => ({
165
- // function: fun,
166
- // name: funName,
167
- // pattern: matcher.regexp,
168
- // cache,
169
- // generator,
170
- // }))
171
- // }
155
+ const buildHandlerDefinition = (
156
+ ctx : PluginContext ,
157
+ { name, matchers, page } : NextDefinition ,
158
+ ) : Array < ManifestFunction > => {
159
+ const fun = getHandlerName ( { name } )
160
+ const funName = name . endsWith ( 'middleware' )
161
+ ? 'Next.js Middleware Handler'
162
+ : `Next.js Edge Handler: ${ page } `
163
+ const cache = name . endsWith ( 'middleware' ) ? undefined : ( 'manual' as const )
164
+ const generator = `${ ctx . pluginName } @${ ctx . pluginVersion } `
165
+
166
+ return augmentMatchers ( matchers , ctx ) . map ( ( matcher ) => ( {
167
+ function : fun ,
168
+ name : funName ,
169
+ pattern : matcher . regexp ,
170
+ cache,
171
+ generator,
172
+ } ) )
173
+ }
172
174
173
175
export const clearStaleEdgeHandlers = async ( ctx : PluginContext ) => {
174
176
await rm ( ctx . edgeFunctionsDir , { recursive : true , force : true } )
@@ -182,10 +184,12 @@ export const createEdgeHandlers = async (ctx: PluginContext) => {
182
184
]
183
185
await Promise . all ( nextDefinitions . map ( ( def ) => createEdgeHandler ( ctx , def ) ) )
184
186
185
- // const netlifyDefinitions = nextDefinitions.flatMap((def) => buildHandlerDefinition(ctx, def))
186
- // const netlifyManifest: Manifest = {
187
- // version: 1,
188
- // functions: netlifyDefinitions,
189
- // }
190
- // await writeEdgeManifest(ctx, netlifyManifest)
187
+ if ( ! ctx . useFrameworksAPI ) {
188
+ const netlifyDefinitions = nextDefinitions . flatMap ( ( def ) => buildHandlerDefinition ( ctx , def ) )
189
+ const netlifyManifest : Manifest = {
190
+ version : 1 ,
191
+ functions : netlifyDefinitions ,
192
+ }
193
+ await writeEdgeManifest ( ctx , netlifyManifest )
194
+ }
191
195
}
0 commit comments