Skip to content

fix: custom headers for root route when using i18n #1893

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/runtime/src/helpers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions packages/runtime/src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
6 changes: 3 additions & 3 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -1635,7 +1635,7 @@ describe('function helpers', () => {
},
},
{
for: '/en/with-locale/*',
for: '/en-US/with-locale/*',
values: {
'X-Unit-Test': 'true',
},
Expand Down