Skip to content

Commit

Permalink
Ensure default locale is redirected to without accept-language
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Oct 5, 2020
1 parent e9f6317 commit 5a7e29a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ const nextServerlessLoader: loader.Loader = function () {
detectedLocale = accept.language(
req.headers['accept-language'],
i18n.locales
)
) || i18n.defaultLocale
}
if (
Expand Down
7 changes: 3 additions & 4 deletions packages/next/next-server/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,9 @@ export default class Server {
let detectedLocale = detectLocaleCookie(req, i18n.locales)

if (!detectedLocale) {
detectedLocale = accept.language(
req.headers['accept-language'],
i18n.locales
)
detectedLocale =
accept.language(req.headers['accept-language'], i18n.locales) ||
i18n.defaultLocale
}

if (
Expand Down
25 changes: 25 additions & 0 deletions test/integration/i18n-support/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,31 @@ function runTests() {
expect(parsedUrl2.query).toEqual({ hello: 'world' })
})

it('should redirect to default locale route for / without accept-language', async () => {
const res = await fetchViaHTTP(appPort, '/', undefined, {
redirect: 'manual',
})
expect(res.status).toBe(307)

const parsedUrl = url.parse(res.headers.get('location'), true)
expect(parsedUrl.pathname).toBe('/en')
expect(parsedUrl.query).toEqual({})

const res2 = await fetchViaHTTP(
appPort,
'/',
{ hello: 'world' },
{
redirect: 'manual',
}
)
expect(res2.status).toBe(307)

const parsedUrl2 = url.parse(res2.headers.get('location'), true)
expect(parsedUrl2.pathname).toBe('/en')
expect(parsedUrl2.query).toEqual({ hello: 'world' })
})

it('should load getStaticProps page correctly SSR', async () => {
const html = await renderViaHTTP(appPort, '/en-US/gsp')
const $ = cheerio.load(html)
Expand Down

0 comments on commit 5a7e29a

Please sign in to comment.