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 { Manifest , ManifestFunction } from '@netlify/edge-functions'
4
+ import type { IntegrationsConfig } 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/**/*' , {
@@ -53,7 +53,7 @@ const augmentMatchers = (
53
53
} )
54
54
}
55
55
56
- const writeHandlerFile = async ( ctx : PluginContext , { matchers, name } : NextDefinition ) => {
56
+ const writeHandlerFile = async ( ctx : PluginContext , { matchers, name, page } : NextDefinition ) => {
57
57
const nextConfig = ctx . buildConfig
58
58
const handlerName = getHandlerName ( { name } )
59
59
const handlerDirectory = join ( ctx . edgeFunctionsDir , handlerName )
@@ -63,12 +63,11 @@ const writeHandlerFile = async (ctx: PluginContext, { matchers, name }: NextDefi
63
63
// Netlify Edge Functions and the Next.js edge runtime.
64
64
await copyRuntime ( ctx , handlerDirectory )
65
65
66
+ const augmentedMatchers = augmentMatchers ( matchers , ctx )
67
+
66
68
// Writing a file with the matchers that should trigger this function. We'll
67
69
// read this file from the function at runtime.
68
- await writeFile (
69
- join ( handlerRuntimeDirectory , 'matchers.json' ) ,
70
- JSON . stringify ( augmentMatchers ( matchers , ctx ) ) ,
71
- )
70
+ await writeFile ( join ( handlerRuntimeDirectory , 'matchers.json' ) , JSON . stringify ( augmentedMatchers ) )
72
71
73
72
// The config is needed by the edge function to match and normalize URLs. To
74
73
// avoid shipping and parsing a large file at runtime, let's strip it down to
@@ -93,6 +92,15 @@ const writeHandlerFile = async (ctx: PluginContext, { matchers, name }: NextDefi
93
92
import {handleMiddleware} from './edge-runtime/middleware.ts';
94
93
import handler from './server/${ name } .js';
95
94
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 ) } ;
96
104
` ,
97
105
)
98
106
}
@@ -142,25 +150,25 @@ const createEdgeHandler = async (ctx: PluginContext, definition: NextDefinition)
142
150
const getHandlerName = ( { name } : Pick < NextDefinition , 'name' > ) : string =>
143
151
`${ EDGE_HANDLER_NAME } -${ name . replace ( / \W / g, '-' ) } `
144
152
145
- const buildHandlerDefinition = (
146
- ctx : PluginContext ,
147
- { name, matchers, page } : NextDefinition ,
148
- ) : Array < ManifestFunction > => {
149
- const fun = getHandlerName ( { name } )
150
- const funName = name . endsWith ( 'middleware' )
151
- ? 'Next.js Middleware Handler'
152
- : `Next.js Edge Handler: ${ page } `
153
- const cache = name . endsWith ( 'middleware' ) ? undefined : ( 'manual' as const )
154
- const generator = `${ ctx . pluginName } @${ ctx . pluginVersion } `
155
-
156
- return augmentMatchers ( matchers , ctx ) . map ( ( matcher ) => ( {
157
- function : fun ,
158
- name : funName ,
159
- pattern : matcher . regexp ,
160
- cache,
161
- generator,
162
- } ) )
163
- }
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
+ // }
164
172
165
173
export const clearStaleEdgeHandlers = async ( ctx : PluginContext ) => {
166
174
await rm ( ctx . edgeFunctionsDir , { recursive : true , force : true } )
@@ -174,10 +182,10 @@ export const createEdgeHandlers = async (ctx: PluginContext) => {
174
182
]
175
183
await Promise . all ( nextDefinitions . map ( ( def ) => createEdgeHandler ( ctx , def ) ) )
176
184
177
- const netlifyDefinitions = nextDefinitions . flatMap ( ( def ) => buildHandlerDefinition ( ctx , def ) )
178
- const netlifyManifest : Manifest = {
179
- version : 1 ,
180
- functions : netlifyDefinitions ,
181
- }
182
- await writeEdgeManifest ( ctx , netlifyManifest )
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)
183
191
}
0 commit comments