Skip to content

Commit ed666d2

Browse files
committed
fix: escape locale strings when matching from routes-manifest
1 parent b83c2c3 commit ed666d2

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

packages/runtime/src/helpers/config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import slash from 'slash'
99
import { HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME } from '../constants'
1010

1111
import type { RoutesManifest } from './types'
12+
import { escapeStringRegexp } from './utils'
1213

1314
const ROUTES_MANIFEST_FILE = 'routes-manifest.json'
1415

@@ -214,7 +215,9 @@ export const generateCustomHeaders = (nextConfig: NextConfig, netlifyHeaders: Ne
214215

215216
if (useLocale) {
216217
const { locales } = i18n
217-
const joinedLocales = locales.join('|')
218+
219+
// escape the locale strings to match the way Next writes the routes-manifest.json file
220+
const joinedLocales = locales.map((locale: string) => escapeStringRegexp(locale)).join('|')
218221

219222
/**
220223
* converts e.g.

packages/runtime/src/helpers/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,3 +313,8 @@ export const getRemotePatterns = (experimental: ExperimentalConfigWithLegacy, im
313313
}
314314
return []
315315
}
316+
317+
// Taken from next/src/shared/lib/escape-regexp.ts
318+
const reHasRegExp = /[|\\{}()[\]^$+*?.-]/
319+
const reReplaceRegExp = /[|\\{}()[\]^$+*?.-]/g
320+
export const escapeStringRegexp = (str: string) => (reHasRegExp.test(str) ? str.replace(reReplaceRegExp, '\\$&') : str)

0 commit comments

Comments
 (0)