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,6 +514,7 @@ function processRoutes<T>( ...@@ -514,6 +514,7 @@ function processRoutes<T>(
const destBasePath = srcBasePath && !isExternal ? srcBasePath : '' const destBasePath = srcBasePath && !isExternal ? srcBasePath : ''
if (config.i18n && r.locale !== false) { if (config.i18n && r.locale !== false) {
if (!isExternal) {
defaultLocales.forEach((item) => { defaultLocales.forEach((item) => {
let destination let destination
...@@ -529,6 +530,7 @@ function processRoutes<T>( ...@@ -529,6 +530,7 @@ function processRoutes<T>(
source: `${srcBasePath}/${item.locale}${r.source}`, source: `${srcBasePath}/${item.locale}${r.source}`,
}) })
}) })
}
r.source = `/:nextInternalLocale(${config.i18n.locales r.source = `/:nextInternalLocale(${config.i18n.locales
.map((locale: string) => escapeStringRegexp(locale)) .map((locale: string) => escapeStringRegexp(locale))
......
...@@ -55,6 +55,11 @@ module.exports = { ...@@ -55,6 +55,11 @@ module.exports = {
destination: '/', destination: '/',
permanent: false, permanent: false,
}, },
{
source: '/redirect-5',
destination: 'https://jobs.example.com',
permanent: false,
},
] ]
}, },
async rewrites() { async rewrites() {
......
...@@ -56,6 +56,11 @@ module.exports = { ...@@ -56,6 +56,11 @@ module.exports = {
destination: '/', destination: '/',
permanent: false, permanent: false,
}, },
{
source: '/redirect-5',
destination: 'https://jobs.example.com',
permanent: false,
},
] ]
}, },
async rewrites() { async rewrites() {
......
...@@ -37,6 +37,47 @@ async function addDefaultLocaleCookie(browser) { ...@@ -37,6 +37,47 @@ async function addDefaultLocaleCookie(browser) {
} }
export function runTests(ctx) { 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 () => { it('should have domainLocales available on useRouter', async () => {
const browser = await webdriver(ctx.appPort, `${ctx.basePath || '/'}`) const browser = await webdriver(ctx.appPort, `${ctx.basePath || '/'}`)
expect( 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