@@ -5,12 +5,13 @@ import { resolve, join } from 'path'
5
5
import type { NetlifyConfig , NetlifyPluginConstants } from '@netlify/build'
6
6
import { greenBright } from 'chalk'
7
7
import destr from 'destr'
8
- import { copy , copyFile , emptyDir , ensureDir , readJSON , readJson , writeJSON , writeJson } from 'fs-extra'
8
+ import { copy , copyFile , emptyDir , ensureDir , readJson , writeJSON , writeJson } from 'fs-extra'
9
9
import type { MiddlewareManifest } from 'next/dist/build/webpack/plugins/middleware-plugin'
10
10
import type { RouteHas } from 'next/dist/lib/load-custom-routes'
11
11
import { outdent } from 'outdent'
12
12
13
- import { getRequiredServerFiles } from './config'
13
+ import { getRequiredServerFiles , NextConfig } from './config'
14
+ import { makeLocaleOptional , stripLookahead } from './matchers'
14
15
15
16
// This is the format as of next@12.2
16
17
interface EdgeFunctionDefinitionV1 {
@@ -132,10 +133,12 @@ const writeEdgeFunction = async ({
132
133
edgeFunctionDefinition,
133
134
edgeFunctionRoot,
134
135
netlifyConfig,
136
+ nextConfig,
135
137
} : {
136
138
edgeFunctionDefinition : EdgeFunctionDefinition
137
139
edgeFunctionRoot : string
138
140
netlifyConfig : NetlifyConfig
141
+ nextConfig : NextConfig
139
142
} ) : Promise <
140
143
Array < {
141
144
function : string
@@ -165,14 +168,21 @@ const writeEdgeFunction = async ({
165
168
// The v1 middleware manifest has a single regexp, but the v2 has an array of matchers
166
169
if ( 'regexp' in edgeFunctionDefinition ) {
167
170
matchers . push ( { regexp : edgeFunctionDefinition . regexp } )
171
+ } else if ( nextConfig . i18n ) {
172
+ matchers . push (
173
+ ...edgeFunctionDefinition . matchers . map ( ( matcher ) => ( {
174
+ regexp : makeLocaleOptional ( matcher . regexp ) ,
175
+ } ) ) ,
176
+ )
168
177
} else {
169
178
matchers . push ( ...edgeFunctionDefinition . matchers )
170
179
}
180
+
171
181
await writeJson ( join ( edgeFunctionDir , 'matchers.json' ) , matchers )
172
182
173
183
// We add a defintion for each matching path
174
184
return matchers . map ( ( matcher ) => {
175
- const pattern = matcher . regexp
185
+ const pattern = stripLookahead ( matcher . regexp )
176
186
return { function : name , pattern, name : edgeFunctionDefinition . name }
177
187
} )
178
188
}
@@ -258,6 +268,7 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
258
268
edgeFunctionDefinition,
259
269
edgeFunctionRoot,
260
270
netlifyConfig,
271
+ nextConfig,
261
272
} )
262
273
manifest . functions . push ( ...functionDefinitions )
263
274
}
@@ -270,6 +281,7 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
270
281
edgeFunctionDefinition,
271
282
edgeFunctionRoot,
272
283
netlifyConfig,
284
+ nextConfig,
273
285
} )
274
286
manifest . functions . push ( ...functionDefinitions )
275
287
}
@@ -284,9 +296,4 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
284
296
await writeJson ( join ( edgeFunctionRoot , 'manifest.json' ) , manifest )
285
297
}
286
298
287
- export const enableEdgeInNextConfig = async ( publish : string ) => {
288
- const configFile = join ( publish , 'required-server-files.json' )
289
- const config = await readJSON ( configFile )
290
- await writeJSON ( configFile , config )
291
- }
292
299
/* eslint-enable max-lines */
0 commit comments