From ed666d22b39aaed428443171e943a95b457b112e Mon Sep 17 00:00:00 2001 From: Rob Stanford Date: Thu, 19 Jan 2023 10:34:45 +0000 Subject: [PATCH 1/2] fix: escape locale strings when matching from routes-manifest --- packages/runtime/src/helpers/config.ts | 5 ++++- packages/runtime/src/helpers/utils.ts | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/runtime/src/helpers/config.ts b/packages/runtime/src/helpers/config.ts index 9cd17265a4..83be5ada51 100644 --- a/packages/runtime/src/helpers/config.ts +++ b/packages/runtime/src/helpers/config.ts @@ -9,6 +9,7 @@ import slash from 'slash' import { HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME } from '../constants' import type { RoutesManifest } from './types' +import { escapeStringRegexp } from './utils' const ROUTES_MANIFEST_FILE = 'routes-manifest.json' @@ -214,7 +215,9 @@ export const generateCustomHeaders = (nextConfig: NextConfig, netlifyHeaders: Ne if (useLocale) { const { locales } = i18n - const joinedLocales = locales.join('|') + + // escape the locale strings to match the way Next writes the routes-manifest.json file + const joinedLocales = locales.map((locale: string) => escapeStringRegexp(locale)).join('|') /** * converts e.g. diff --git a/packages/runtime/src/helpers/utils.ts b/packages/runtime/src/helpers/utils.ts index ab73a19d6c..c47ec1be71 100644 --- a/packages/runtime/src/helpers/utils.ts +++ b/packages/runtime/src/helpers/utils.ts @@ -313,3 +313,8 @@ export const getRemotePatterns = (experimental: ExperimentalConfigWithLegacy, im } return [] } + +// Taken from next/src/shared/lib/escape-regexp.ts +const reHasRegExp = /[|\\{}()[\]^$+*?.-]/ +const reReplaceRegExp = /[|\\{}()[\]^$+*?.-]/g +export const escapeStringRegexp = (str: string) => (reHasRegExp.test(str) ? str.replace(reReplaceRegExp, '\\$&') : str) From 646805b4be2b91a561c4e8b649b035a0031245fb Mon Sep 17 00:00:00 2001 From: Rob Stanford Date: Thu, 19 Jan 2023 10:35:11 +0000 Subject: [PATCH 2/2] chore: amend i18n headers test to cover regex special characters --- test/index.spec.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/index.spec.js b/test/index.spec.js index b3977d8606..1979e81407 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -1606,13 +1606,13 @@ describe('function helpers', () => { // Next.js has modified the routesManifest to have the locales in the source. const nextConfig = { i18n: { - locales: ['en', 'fr'], + locales: ['en-US', 'fr'], defaultLocale: 'en', }, routesManifest: { headers: [ { - source: '/:nextInternalLocale(en|fr)/with-locale/:path*', + source: '/:nextInternalLocale(en\\-US|fr)/with-locale/:path*', headers: [ { key: 'X-Unit-Test', @@ -1635,7 +1635,7 @@ describe('function helpers', () => { }, }, { - for: '/en/with-locale/*', + for: '/en-US/with-locale/*', values: { 'X-Unit-Test': 'true', },