Skip to content

Commit dda77b7

Browse files
committed
migrate edge functions to frameworks api
1 parent 5a5fcb0 commit dda77b7

File tree

2 files changed

+44
-36
lines changed

2 files changed

+44
-36
lines changed

src/build/functions/edge.ts

+43-35
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import { cp, mkdir, readFile, rm, writeFile } from 'node:fs/promises'
22
import { dirname, join } from 'node:path'
33

4-
import type { Manifest, ManifestFunction } from '@netlify/edge-functions'
4+
import type { IntegrationsConfig } from '@netlify/edge-functions'
55
import { glob } from 'fast-glob'
66
import type { EdgeFunctionDefinition as NextDefinition } from 'next/dist/build/webpack/plugins/middleware-plugin.js'
77
import { pathToRegexp } from 'path-to-regexp'
88

99
import { 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-
}
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+
// }
1515

1616
const copyRuntime = async (ctx: PluginContext, handlerDirectory: string): Promise<void> => {
1717
const files = await glob('edge-runtime/**/*', {
@@ -53,7 +53,7 @@ const augmentMatchers = (
5353
})
5454
}
5555

56-
const writeHandlerFile = async (ctx: PluginContext, { matchers, name }: NextDefinition) => {
56+
const writeHandlerFile = async (ctx: PluginContext, { matchers, name, page }: NextDefinition) => {
5757
const nextConfig = ctx.buildConfig
5858
const handlerName = getHandlerName({ name })
5959
const handlerDirectory = join(ctx.edgeFunctionsDir, handlerName)
@@ -63,12 +63,11 @@ const writeHandlerFile = async (ctx: PluginContext, { matchers, name }: NextDefi
6363
// Netlify Edge Functions and the Next.js edge runtime.
6464
await copyRuntime(ctx, handlerDirectory)
6565

66+
const augmentedMatchers = augmentMatchers(matchers, ctx)
67+
6668
// Writing a file with the matchers that should trigger this function. We'll
6769
// 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))
7271

7372
// The config is needed by the edge function to match and normalize URLs. To
7473
// 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
9392
import {handleMiddleware} from './edge-runtime/middleware.ts';
9493
import handler from './server/${name}.js';
9594
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)};
96104
`,
97105
)
98106
}
@@ -142,25 +150,25 @@ const createEdgeHandler = async (ctx: PluginContext, definition: NextDefinition)
142150
const getHandlerName = ({ name }: Pick<NextDefinition, 'name'>): string =>
143151
`${EDGE_HANDLER_NAME}-${name.replace(/\W/g, '-')}`
144152

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+
// }
164172

165173
export const clearStaleEdgeHandlers = async (ctx: PluginContext) => {
166174
await rm(ctx.edgeFunctionsDir, { recursive: true, force: true })
@@ -174,10 +182,10 @@ export const createEdgeHandlers = async (ctx: PluginContext) => {
174182
]
175183
await Promise.all(nextDefinitions.map((def) => createEdgeHandler(ctx, def)))
176184

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)
183191
}

src/build/plugin-context.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ export class PluginContext {
195195
* `.netlify/edge-functions`
196196
*/
197197
get edgeFunctionsDir(): string {
198-
return this.resolveFromPackagePath('.netlify/edge-functions')
198+
return this.resolveFromPackagePath('.netlify/v1/edge-functions')
199199
}
200200

201201
/** Absolute path of the edge handler */

0 commit comments

Comments
 (0)