Skip to content

Commit 5a7e29a

Browse files
committed
Ensure default locale is redirected to without accept-language
1 parent e9f6317 commit 5a7e29a

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

packages/next/build/webpack/loaders/next-serverless-loader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ const nextServerlessLoader: loader.Loader = function () {
229229
detectedLocale = accept.language(
230230
req.headers['accept-language'],
231231
i18n.locales
232-
)
232+
) || i18n.defaultLocale
233233
}
234234
235235
if (

packages/next/next-server/server/next-server.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,10 +308,9 @@ export default class Server {
308308
let detectedLocale = detectLocaleCookie(req, i18n.locales)
309309

310310
if (!detectedLocale) {
311-
detectedLocale = accept.language(
312-
req.headers['accept-language'],
313-
i18n.locales
314-
)
311+
detectedLocale =
312+
accept.language(req.headers['accept-language'], i18n.locales) ||
313+
i18n.defaultLocale
315314
}
316315

317316
if (

test/integration/i18n-support/test/index.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,31 @@ function runTests() {
5858
expect(parsedUrl2.query).toEqual({ hello: 'world' })
5959
})
6060

61+
it('should redirect to default locale route for / without accept-language', async () => {
62+
const res = await fetchViaHTTP(appPort, '/', undefined, {
63+
redirect: 'manual',
64+
})
65+
expect(res.status).toBe(307)
66+
67+
const parsedUrl = url.parse(res.headers.get('location'), true)
68+
expect(parsedUrl.pathname).toBe('/en')
69+
expect(parsedUrl.query).toEqual({})
70+
71+
const res2 = await fetchViaHTTP(
72+
appPort,
73+
'/',
74+
{ hello: 'world' },
75+
{
76+
redirect: 'manual',
77+
}
78+
)
79+
expect(res2.status).toBe(307)
80+
81+
const parsedUrl2 = url.parse(res2.headers.get('location'), true)
82+
expect(parsedUrl2.pathname).toBe('/en')
83+
expect(parsedUrl2.query).toEqual({ hello: 'world' })
84+
})
85+
6186
it('should load getStaticProps page correctly SSR', async () => {
6287
const html = await renderViaHTTP(appPort, '/en-US/gsp')
6388
const $ = cheerio.load(html)

0 commit comments

Comments
 (0)