Unverified Commit 1eeac4f9 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Make sure locale detecting is case-insensitive (#17757)

Follow-up to https://github.com/vercel/next.js/pull/17370 this makes sure the locale detection is case-insensitive. 
parent 22f91bb3
...@@ -241,10 +241,10 @@ const nextServerlessLoader: loader.Loader = function () { ...@@ -241,10 +241,10 @@ const nextServerlessLoader: loader.Loader = function () {
} }
const denormalizedPagePath = denormalizePagePath(parsedUrl.pathname || '/') const denormalizedPagePath = denormalizePagePath(parsedUrl.pathname || '/')
const detectedDefaultLocale = !detectedLocale || detectedLocale === defaultLocale const detectedDefaultLocale = !detectedLocale || detectedLocale.toLowerCase() === defaultLocale.toLowerCase()
const shouldStripDefaultLocale = const shouldStripDefaultLocale =
detectedDefaultLocale && detectedDefaultLocale &&
denormalizedPagePath === \`/\${defaultLocale}\` denormalizedPagePath.toLowerCase() === \`/\${defaultLocale.toLowerCase()}\`
const shouldAddLocalePrefix = const shouldAddLocalePrefix =
!detectedDefaultLocale && denormalizedPagePath === '/' !detectedDefaultLocale && denormalizedPagePath === '/'
......
import { IncomingMessage } from 'http' import { IncomingMessage } from 'http'
export function detectLocaleCookie(req: IncomingMessage, locales: string[]) { export function detectLocaleCookie(req: IncomingMessage, locales: string[]) {
let detectedLocale: string | undefined
if (req.headers.cookie && req.headers.cookie.includes('NEXT_LOCALE')) { if (req.headers.cookie && req.headers.cookie.includes('NEXT_LOCALE')) {
const { NEXT_LOCALE } = (req as any).cookies const { NEXT_LOCALE } = (req as any).cookies
return locales.find(
if (locales.some((locale: string) => NEXT_LOCALE === locale)) { (locale: string) => NEXT_LOCALE.toLowerCase() === locale.toLowerCase()
detectedLocale = NEXT_LOCALE )
}
} }
return detectedLocale
} }
...@@ -10,7 +10,7 @@ export function normalizeLocalePath( ...@@ -10,7 +10,7 @@ export function normalizeLocalePath(
const pathnameParts = pathname.split('/') const pathnameParts = pathname.split('/')
;(locales || []).some((locale) => { ;(locales || []).some((locale) => {
if (pathnameParts[1] === locale) { if (pathnameParts[1].toLowerCase() === locale.toLowerCase()) {
detectedLocale = locale detectedLocale = locale
pathnameParts.splice(1, 1) pathnameParts.splice(1, 1)
pathname = pathnameParts.join('/') || '/' pathname = pathnameParts.join('/') || '/'
......
...@@ -324,9 +324,11 @@ export default class Server { ...@@ -324,9 +324,11 @@ export default class Server {
const denormalizedPagePath = denormalizePagePath(pathname || '/') const denormalizedPagePath = denormalizePagePath(pathname || '/')
const detectedDefaultLocale = const detectedDefaultLocale =
!detectedLocale || detectedLocale === defaultLocale !detectedLocale ||
detectedLocale.toLowerCase() === defaultLocale.toLowerCase()
const shouldStripDefaultLocale = const shouldStripDefaultLocale =
detectedDefaultLocale && denormalizedPagePath === `/${defaultLocale}` detectedDefaultLocale &&
denormalizedPagePath.toLowerCase() === `/${defaultLocale.toLowerCase()}`
const shouldAddLocalePrefix = const shouldAddLocalePrefix =
!detectedDefaultLocale && denormalizedPagePath === '/' !detectedDefaultLocale && denormalizedPagePath === '/'
......
...@@ -173,6 +173,15 @@ function runTests() { ...@@ -173,6 +173,15 @@ function runTests() {
expect($('#router-as-path').text()).toBe('/gsp') expect($('#router-as-path').text()).toBe('/gsp')
expect($('#router-pathname').text()).toBe('/gsp') expect($('#router-pathname').text()).toBe('/gsp')
expect(JSON.parse($('#router-locales').text())).toEqual(locales) expect(JSON.parse($('#router-locales').text())).toEqual(locales)
// make sure locale is case-insensitive
const html2 = await renderViaHTTP(appPort, `/${locale.toUpperCase()}/gsp`)
const $2 = cheerio.load(html2)
expect($2('html').attr('lang')).toBe(locale)
expect($2('#router-locale').text()).toBe(locale)
expect($2('#router-as-path').text()).toBe('/gsp')
expect($2('#router-pathname').text()).toBe('/gsp')
expect(JSON.parse($2('#router-locales').text())).toEqual(locales)
} }
}) })
...@@ -193,6 +202,21 @@ function runTests() { ...@@ -193,6 +202,21 @@ function runTests() {
expect(parsedUrl.pathname).toBe('/') expect(parsedUrl.pathname).toBe('/')
expect(parsedUrl.query).toEqual({}) expect(parsedUrl.query).toEqual({})
// make sure locale is case-insensitive
const res2 = await fetchViaHTTP(appPort, '/eN-Us', undefined, {
redirect: 'manual',
headers: {
'Accept-Language': 'en-US;q=0.9',
},
})
expect(res2.status).toBe(307)
const parsedUrl2 = url.parse(res.headers.get('location'), true)
expect(parsedUrl2.pathname).toBe('/')
expect(parsedUrl2.query).toEqual({})
}) })
it('should load getStaticProps page correctly SSR (default locale no prefix)', async () => { it('should load getStaticProps page correctly SSR (default locale no prefix)', async () => {
...@@ -391,8 +415,8 @@ function runTests() { ...@@ -391,8 +415,8 @@ function runTests() {
it('should navigate client side for default locale with no prefix', async () => { it('should navigate client side for default locale with no prefix', async () => {
const browser = await webdriver(appPort, '/') const browser = await webdriver(appPort, '/')
// make sure default locale is used in case browser isn't set to // make sure default locale is used in case browser isn't set to
// favor en-US by default // favor en-US by default, (we use all caps to ensure it's case-insensitive)
await browser.manage().addCookie({ name: 'NEXT_LOCALE', value: 'en-US' }) await browser.manage().addCookie({ name: 'NEXT_LOCALE', value: 'EN-US' })
await browser.get(browser.initUrl) await browser.get(browser.initUrl)
const checkIndexValues = async () => { const checkIndexValues = async () => {
......
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