@@ -10,7 +10,8 @@ import type { MiddlewareManifest } from 'next/dist/build/webpack/plugins/middlew
1010import type { RouteHas } from 'next/dist/lib/load-custom-routes'
1111import { outdent } from 'outdent'
1212
13- import { getRequiredServerFiles } from './config'
13+ import { getRequiredServerFiles , NextConfig } from './config'
14+ import { makeLocaleOptional , stripLookahead } from './matchers'
1415
1516// This is the format as of next@12.2
1617interface EdgeFunctionDefinitionV1 {
@@ -132,10 +133,12 @@ const writeEdgeFunction = async ({
132133 edgeFunctionDefinition,
133134 edgeFunctionRoot,
134135 netlifyConfig,
136+ nextConfig,
135137} : {
136138 edgeFunctionDefinition : EdgeFunctionDefinition
137139 edgeFunctionRoot : string
138140 netlifyConfig : NetlifyConfig
141+ nextConfig : NextConfig
139142} ) : Promise <
140143 Array < {
141144 function : string
@@ -165,14 +168,22 @@ const writeEdgeFunction = async ({
165168 // The v1 middleware manifest has a single regexp, but the v2 has an array of matchers
166169 if ( 'regexp' in edgeFunctionDefinition ) {
167170 matchers . push ( { regexp : edgeFunctionDefinition . regexp } )
171+ } else if ( nextConfig . i18n ) {
172+ matchers . push (
173+ ...edgeFunctionDefinition . matchers . map ( ( matcher ) => ( {
174+ ...matcher ,
175+ regexp : makeLocaleOptional ( matcher . regexp ) ,
176+ } ) ) ,
177+ )
168178 } else {
169179 matchers . push ( ...edgeFunctionDefinition . matchers )
170180 }
181+
171182 await writeJson ( join ( edgeFunctionDir , 'matchers.json' ) , matchers )
172183
173184 // We add a defintion for each matching path
174185 return matchers . map ( ( matcher ) => {
175- const pattern = matcher . regexp
186+ const pattern = stripLookahead ( matcher . regexp )
176187 return { function : name , pattern, name : edgeFunctionDefinition . name }
177188 } )
178189}
@@ -258,6 +269,7 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
258269 edgeFunctionDefinition,
259270 edgeFunctionRoot,
260271 netlifyConfig,
272+ nextConfig,
261273 } )
262274 manifest . functions . push ( ...functionDefinitions )
263275 }
@@ -270,6 +282,7 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
270282 edgeFunctionDefinition,
271283 edgeFunctionRoot,
272284 netlifyConfig,
285+ nextConfig,
273286 } )
274287 manifest . functions . push ( ...functionDefinitions )
275288 }
0 commit comments