Skip to content

Commit f3b7306

Browse files
committed
add a helper for how edge function config is defined
1 parent aec7ac2 commit f3b7306

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

src/build/functions/edge.ts

+22-19
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,19 @@ const augmentMatchers = (
5656
const getHandlerName = ({ name }: Pick<NextDefinition, 'name'>): string =>
5757
`${EDGE_HANDLER_NAME}-${name.replace(/\W/g, '-')}`
5858

59+
const getEdgeFunctionSharedConfig = (
60+
ctx: PluginContext,
61+
{ name, page }: Pick<NextDefinition, 'name' | 'page'>,
62+
) => {
63+
return {
64+
name: name.endsWith('middleware')
65+
? 'Next.js Middleware Handler'
66+
: `Next.js Edge Handler: ${page}`,
67+
cache: name.endsWith('middleware') ? undefined : ('manual' as const),
68+
generator: `${ctx.pluginName}@${ctx.pluginVersion}`,
69+
}
70+
}
71+
5972
const writeHandlerFile = async (ctx: PluginContext, { matchers, name, page }: NextDefinition) => {
6073
const nextConfig = ctx.buildConfig
6174
const handlerName = getHandlerName({ name })
@@ -87,16 +100,13 @@ const writeHandlerFile = async (ctx: PluginContext, { matchers, name, page }: Ne
87100
JSON.stringify(minimalNextConfig),
88101
)
89102

90-
const isc = ctx.useFrameworksAPI
91-
? `export const config = ${JSON.stringify({
92-
name: name.endsWith('middleware')
93-
? 'Next.js Middleware Handler'
94-
: `Next.js Edge Handler: ${page}`,
95-
pattern: augmentedMatchers.map((matcher) => matcher.regexp),
96-
cache: name.endsWith('middleware') ? undefined : 'manual',
97-
generator: `${ctx.pluginName}@${ctx.pluginVersion}`,
98-
} satisfies IntegrationsConfig)};`
99-
: ``
103+
const isc =
104+
ctx.edgeFunctionsConfigStrategy === 'inline'
105+
? `export const config = ${JSON.stringify({
106+
...getEdgeFunctionSharedConfig(ctx, { name, page }),
107+
pattern: augmentedMatchers.map((matcher) => matcher.regexp),
108+
} satisfies IntegrationsConfig)};`
109+
: ``
100110

101111
// Writing the function entry file. It wraps the middleware code with the
102112
// compatibility layer mentioned above.
@@ -157,18 +167,11 @@ const buildHandlerDefinition = (
157167
{ name, matchers, page }: NextDefinition,
158168
): Array<ManifestFunction> => {
159169
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}`
165170

166171
return augmentMatchers(matchers, ctx).map((matcher) => ({
172+
...getEdgeFunctionSharedConfig(ctx, { name, page }),
167173
function: fun,
168-
name: funName,
169174
pattern: matcher.regexp,
170-
cache,
171-
generator,
172175
}))
173176
}
174177

@@ -184,7 +187,7 @@ export const createEdgeHandlers = async (ctx: PluginContext) => {
184187
]
185188
await Promise.all(nextDefinitions.map((def) => createEdgeHandler(ctx, def)))
186189

187-
if (!ctx.useFrameworksAPI) {
190+
if (ctx.edgeFunctionsConfigStrategy === 'manifest') {
188191
const netlifyDefinitions = nextDefinitions.flatMap((def) => buildHandlerDefinition(ctx, def))
189192
const netlifyManifest: Manifest = {
190193
version: 1,

src/build/plugin-context.ts

+4
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@ export class PluginContext {
253253
return this.resolveFromPackagePath('.netlify/edge-functions')
254254
}
255255

256+
get edgeFunctionsConfigStrategy(): 'manifest' | 'inline' {
257+
return this.useFrameworksAPI ? 'inline' : 'manifest'
258+
}
259+
256260
/** Absolute path of the edge handler */
257261
get edgeHandlerDir(): string {
258262
return join(this.edgeFunctionsDir, EDGE_HANDLER_NAME)

0 commit comments

Comments
 (0)