Skip to content

Commit d4ea6e6

Browse files
committed
test: add test case
1 parent ee3a48e commit d4ea6e6

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

cypress/e2e/default/i18n.cy.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,28 @@ describe('Localization', () => {
3535

3636
cy.findByText('The current locale is en')
3737
})
38+
39+
it(`function handler doesn't produce locale when first language in 'Accept-Language' header is not matching any of locales`, () => {
40+
// make sure we don't use cached results
41+
cy.setCookie('__prerender_bypass', '1')
42+
cy.request({
43+
url: `/`,
44+
followRedirect: false,
45+
headers: {
46+
// jp is not in i18n config, fr is in config
47+
// Netlify Redirects only match on first language
48+
// https://docs.netlify.com/routing/redirects/redirect-options/#redirect-by-country-or-language
49+
// > Language-based redirects always match against the first language reported by the browser in the Accept-Language header regardless of quality value weighting.
50+
// while Next.js matches on every language: https://github.com/vercel/next.js/blob/5d9597879c46b383d595d6f7b37fd373325b7544/test/unit/accept-headers.test.ts
51+
'Accept-Language': 'jp, fr;q=0.9',
52+
Accept: '*/*',
53+
cookie: '__prerender_bypass=1',
54+
},
55+
}).then((response) => {
56+
// we don't expect for function handler to return a redirect
57+
// locale redirects should be handled by Netlify redirects
58+
// otherwise we could wrongly cache locale redirect generated by ODB
59+
expect(response.status).to.eq(200)
60+
})
61+
})
3862
})

packages/runtime/src/templates/getHandler.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ const makeHandler = ({ conf, app, pageRoot, NextServer, staticManifest = [], mod
135135
// https://docs.netlify.com/routing/redirects/redirect-options/#redirect-by-country-or-language
136136
// > Language-based redirects always match against the first language reported by the browser in the Accept-Language header regardless of quality value weighting.
137137
// If we wouldn't keep just first language, it's possible for `next-server` to generate locale redirect that could be cached by ODB
138+
// because it matches on every language listed: https://github.com/vercel/next.js/blob/5d9597879c46b383d595d6f7b37fd373325b7544/test/unit/accept-headers.test.ts
138139
event.headers['accept-language'] = event.headers['accept-language'].replace(/\s*,.*$/, '')
139140
}
140141

0 commit comments

Comments
 (0)