Skip to content

Commit 9fe2fe6

Browse files
committed
fix: correctly handle matchers with lookaheads and i18n
1 parent 05c24d2 commit 9fe2fe6

File tree

5 files changed

+77
-51
lines changed

5 files changed

+77
-51
lines changed

demos/middleware/middleware.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export const config = {
9494
'/api/:all*',
9595
'/headers',
9696
{ source: '/static' },
97-
{ source: '/shows/:all*' },
97+
{ source: '/shows/((?!99|88).*)' },
9898
{
9999
source: '/conditional',
100100
has: [

package-lock.json

Lines changed: 29 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/runtime/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"p-limit": "^3.1.0",
2929
"pathe": "^0.2.0",
3030
"pretty-bytes": "^5.6.0",
31+
"regexp-tree": "^0.1.24",
3132
"semver": "^7.3.5",
3233
"slash": "^3.0.0",
3334
"tiny-glob": "^0.2.9"

packages/runtime/src/helpers/edge.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import { resolve, join } from 'path'
55
import type { NetlifyConfig, NetlifyPluginConstants } from '@netlify/build'
66
import { greenBright } from 'chalk'
77
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'
99
import type { MiddlewareManifest } from 'next/dist/build/webpack/plugins/middleware-plugin'
1010
import type { RouteHas } from 'next/dist/lib/load-custom-routes'
1111
import { 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
1617
interface 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,21 @@ 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+
regexp: makeLocaleOptional(matcher.regexp),
175+
})),
176+
)
168177
} else {
169178
matchers.push(...edgeFunctionDefinition.matchers)
170179
}
180+
171181
await writeJson(join(edgeFunctionDir, 'matchers.json'), matchers)
172182

173183
// We add a defintion for each matching path
174184
return matchers.map((matcher) => {
175-
const pattern = matcher.regexp
185+
const pattern = stripLookahead(matcher.regexp)
176186
return { function: name, pattern, name: edgeFunctionDefinition.name }
177187
})
178188
}
@@ -258,6 +268,7 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
258268
edgeFunctionDefinition,
259269
edgeFunctionRoot,
260270
netlifyConfig,
271+
nextConfig,
261272
})
262273
manifest.functions.push(...functionDefinitions)
263274
}
@@ -270,6 +281,7 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
270281
edgeFunctionDefinition,
271282
edgeFunctionRoot,
272283
netlifyConfig,
284+
nextConfig,
273285
})
274286
manifest.functions.push(...functionDefinitions)
275287
}
@@ -284,9 +296,4 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
284296
await writeJson(join(edgeFunctionRoot, 'manifest.json'), manifest)
285297
}
286298

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-
}
292299
/* eslint-enable max-lines */

0 commit comments

Comments
 (0)