Unverified Commit 888c7002 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix external redirect with i18n domains (#27415)

This corrects a case when redirecting with i18n locale domains configured the locale domain would be prefixed on external redirects un-necessarily. 

Fixes: https://github.com/vercel/next.js/issues/27405

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [x] Errors have helpful link attached, see `contributing.md`
parent 14fb7df7
......@@ -514,21 +514,23 @@ function processRoutes<T>(
const destBasePath = srcBasePath && !isExternal ? srcBasePath : ''
if (config.i18n && r.locale !== false) {
defaultLocales.forEach((item) => {
let destination
if (r.destination) {
destination = item.base
? `${item.base}${destBasePath}${r.destination}`
: `${destBasePath}${r.destination}`
}
if (!isExternal) {
defaultLocales.forEach((item) => {
let destination
if (r.destination) {
destination = item.base
? `${item.base}${destBasePath}${r.destination}`
: `${destBasePath}${r.destination}`
}
newRoutes.push({
...r,
destination,
source: `${srcBasePath}/${item.locale}${r.source}`,
newRoutes.push({
...r,
destination,
source: `${srcBasePath}/${item.locale}${r.source}`,
})
})
})
}
r.source = `/:nextInternalLocale(${config.i18n.locales
.map((locale: string) => escapeStringRegexp(locale))
......
......@@ -55,6 +55,11 @@ module.exports = {
destination: '/',
permanent: false,
},
{
source: '/redirect-5',
destination: 'https://jobs.example.com',
permanent: false,
},
]
},
async rewrites() {
......
......@@ -56,6 +56,11 @@ module.exports = {
destination: '/',
permanent: false,
},
{
source: '/redirect-5',
destination: 'https://jobs.example.com',
permanent: false,
},
]
},
async rewrites() {
......
......@@ -37,6 +37,47 @@ async function addDefaultLocaleCookie(browser) {
}
export function runTests(ctx) {
it('should redirect external domain correctly', async () => {
const res = await fetchViaHTTP(
ctx.appPort,
`${ctx.basePath || ''}/do/redirect-5`,
undefined,
{
headers: {
'accept-language': 'do',
},
redirect: 'manual',
}
)
expect(res.status).toBe(307)
expect(res.headers.get('location')).toBe('https://jobs.example.com/')
const res2 = await fetchViaHTTP(
ctx.appPort,
`${ctx.basePath || ''}/redirect-5`,
undefined,
{
redirect: 'manual',
}
)
expect(res2.status).toBe(307)
expect(res2.headers.get('location')).toBe('https://jobs.example.com/')
const res3 = await fetchViaHTTP(
ctx.appPort,
`${ctx.basePath || ''}/fr/redirect-5`,
undefined,
{
redirect: 'manual',
}
)
expect(res3.status).toBe(307)
expect(res3.headers.get('location')).toBe('https://jobs.example.com/')
})
it('should have domainLocales available on useRouter', async () => {
const browser = await webdriver(ctx.appPort, `${ctx.basePath || '/'}`)
expect(
......
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