Unverified Commit 916d2aa4 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Validate i18n locale domain (#50220)

The `domain` value is meant to be a hostname and not include `http` or
port so this ensures we validate it doesn't contain `:` unexpectedly
causing invalid behavior.

Closes: https://github.com/vercel/next.js/issues/49656
x-ref:
https://github.com/vercel/next.js/issues/24991#issuecomment-870418366

---------
Co-authored-by: 's avatarJiachi Liu <inbox@huozhi.im>
parent 616ae108
...@@ -537,6 +537,13 @@ function assignDefaults( ...@@ -537,6 +537,13 @@ function assignDefaults(
if (!item.defaultLocale) return true if (!item.defaultLocale) return true
if (!item.domain || typeof item.domain !== 'string') return true if (!item.domain || typeof item.domain !== 'string') return true
if (item.domain.includes(':')) {
console.warn(
`i18n domain: "${item.domain}" is invalid it should be a valid domain without protocol (https://) or port (:3000) e.g. example.vercel.sh`
)
return true
}
const defaultLocaleDuplicate = i18n.domains?.find( const defaultLocaleDuplicate = i18n.domains?.find(
(altItem) => (altItem) =>
altItem.defaultLocale === item.defaultLocale && altItem.defaultLocale === item.defaultLocale &&
......
...@@ -550,4 +550,30 @@ describe('i18n Support', () => { ...@@ -550,4 +550,30 @@ describe('i18n Support', () => {
) )
expect(stderr).toContain(`eN, fr`) expect(stderr).toContain(`eN, fr`)
}) })
it('should show proper error for invalid locale domain', async () => {
nextConfig.write(`
module.exports = {
i18n: {
locales: ['en', 'fr', 'nl', 'eN', 'fr'],
domains: [
{
domain: 'hello:3000',
defaultLocale: 'en',
}
],
defaultLocale: 'en',
}
}
`)
const { code, stderr } = await nextBuild(appDir, undefined, {
stderr: true,
})
nextConfig.restore()
expect(code).toBe(1)
expect(stderr).toContain(
`i18n domain: "hello:3000" is invalid it should be a valid domain without protocol (https://) or port (:3000) e.g. example.vercel.sh`
)
})
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment