Unverified Commit a8d8f9ae authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure localeDetection: false doesn't detect from header (#18695)

This ensures we don't detect the locale from the header when `localeDetection` is set to `false`

Fixes: https://github.com/vercel/next.js/issues/18482
parent faf4a0ab
...@@ -243,10 +243,12 @@ const nextServerlessLoader: loader.Loader = function () { ...@@ -243,10 +243,12 @@ const nextServerlessLoader: loader.Loader = function () {
let locales = i18n.locales let locales = i18n.locales
let defaultLocale = i18n.defaultLocale let defaultLocale = i18n.defaultLocale
let detectedLocale = detectLocaleCookie(req, i18n.locales) let detectedLocale = detectLocaleCookie(req, i18n.locales)
let acceptPreferredLocale = accept.language( let acceptPreferredLocale = i18n.localeDetection !== false
req.headers['accept-language'], ? accept.language(
i18n.locales req.headers['accept-language'],
) i18n.locales
)
: detectedLocale
const { host } = req.headers || {} const { host } = req.headers || {}
// remove port from host and remove port if present // remove port from host and remove port if present
......
...@@ -411,7 +411,7 @@ function assignDefaults(userConfig: { [key: string]: any }) { ...@@ -411,7 +411,7 @@ function assignDefaults(userConfig: { [key: string]: any }) {
...i18n.locales.filter((locale: string) => locale !== i18n.defaultLocale), ...i18n.locales.filter((locale: string) => locale !== i18n.defaultLocale),
] ]
const localeDetectionType = typeof i18n.locales.localeDetection const localeDetectionType = typeof i18n.localeDetection
if ( if (
localeDetectionType !== 'boolean' && localeDetectionType !== 'boolean' &&
......
...@@ -312,10 +312,10 @@ export default class Server { ...@@ -312,10 +312,10 @@ export default class Server {
const { pathname, ...parsed } = parseUrl(req.url || '/') const { pathname, ...parsed } = parseUrl(req.url || '/')
let defaultLocale = i18n.defaultLocale let defaultLocale = i18n.defaultLocale
let detectedLocale = detectLocaleCookie(req, i18n.locales) let detectedLocale = detectLocaleCookie(req, i18n.locales)
let acceptPreferredLocale = accept.language( let acceptPreferredLocale =
req.headers['accept-language'], i18n.localeDetection !== false
i18n.locales ? accept.language(req.headers['accept-language'], i18n.locales)
) : detectedLocale
const { host } = req?.headers || {} const { host } = req?.headers || {}
// remove port from host and remove port if present // remove port from host and remove port if present
......
module.exports = { module.exports = {
// target: 'experimental-serverless-trace', // target: 'experimental-serverless-trace',
i18n: { i18n: {
// localeDetection: false,
locales: ['nl-NL', 'nl-BE', 'nl', 'fr-BE', 'fr', 'en-US', 'en'], locales: ['nl-NL', 'nl-BE', 'nl', 'fr-BE', 'fr', 'en-US', 'en'],
defaultLocale: 'en-US', defaultLocale: 'en-US',
domains: [ domains: [
......
...@@ -1823,4 +1823,91 @@ describe('i18n Support', () => { ...@@ -1823,4 +1823,91 @@ describe('i18n Support', () => {
runTests() runTests()
}) })
describe('with localeDetection disabled', () => {
beforeAll(async () => {
await fs.remove(join(appDir, '.next'))
nextConfig.replace('// localeDetection', 'localeDetection')
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
nextConfig.restore()
await killApp(app)
})
it('should have localeDetection in routes-manifest', async () => {
const routesManifest = await fs.readJSON(
join(appDir, '.next/routes-manifest.json')
)
expect(routesManifest.i18n).toEqual({
localeDetection: false,
locales: ['en-US', 'nl-NL', 'nl-BE', 'nl', 'fr-BE', 'fr', 'en'],
defaultLocale: 'en-US',
domains: [
{
http: true,
domain: 'example.be',
defaultLocale: 'nl-BE',
locales: ['nl', 'nl-NL', 'nl-BE'],
},
{
http: true,
domain: 'example.fr',
defaultLocale: 'fr',
locales: ['fr-BE'],
},
],
})
})
it('should not detect locale from accept-language', async () => {
const res = await fetchViaHTTP(
appPort,
'/',
{},
{
redirect: 'manual',
headers: {
'accept-language': 'fr',
},
}
)
expect(res.status).toBe(200)
const $ = cheerio.load(await res.text())
expect($('html').attr('lang')).toBe('en-US')
expect($('#router-locale').text()).toBe('en-US')
expect(JSON.parse($('#router-locales').text())).toEqual(locales)
expect($('#router-pathname').text()).toBe('/')
expect($('#router-as-path').text()).toBe('/')
})
it('should set locale from detected path', async () => {
for (const locale of locales) {
const res = await fetchViaHTTP(
appPort,
`/${locale}`,
{},
{
redirect: 'manual',
headers: {
'accept-language': 'en-US,en;q=0.9',
},
}
)
expect(res.status).toBe(200)
const $ = cheerio.load(await res.text())
expect($('html').attr('lang')).toBe(locale)
expect($('#router-locale').text()).toBe(locale)
expect(JSON.parse($('#router-locales').text())).toEqual(locales)
expect($('#router-pathname').text()).toBe('/')
expect($('#router-as-path').text()).toBe('/')
}
})
})
}) })
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