Unverified Commit 118588b0 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Remove extra redirect for index with i18n (#20397)

This makes sure redirects to the index route properly end with a trailing slash or don't based on the `trailingSlash` config to ensure an additional redirect doesn't need to take place un-necessarily. 

Fixes: https://github.com/vercel/next.js/issues/19405
parent eeb28385
......@@ -45,7 +45,7 @@ interface ExportPageInput {
renderOpts: RenderOpts
buildExport?: boolean
serverRuntimeConfig: string
subFolders: string
subFolders?: boolean
serverless: boolean
optimizeFonts: boolean
optimizeImages: boolean
......
......@@ -410,7 +410,9 @@ function processRoutes<T>(
.join('|')})${r.source}`
if (r.destination && r.destination?.startsWith('/')) {
r.destination = `/:nextInternalLocale${r.destination}`
r.destination = `/:nextInternalLocale${
r.destination === '/' && !config.trailingSlash ? '' : r.destination
}`
}
}
r.source = `${srcBasePath}${r.source}`
......
......@@ -12,7 +12,7 @@ const targets = ['server', 'serverless', 'experimental-serverless-trace']
const reactModes = ['legacy', 'blocking', 'concurrent']
export type NextConfig = { [key: string]: any } & {
i18n: {
i18n?: {
domains?: Array<{
http?: true
domain: string
......@@ -27,6 +27,8 @@ export type NextConfig = { [key: string]: any } & {
headers?: () => Promise<Header[]>
rewrites?: () => Promise<Rewrite[]>
redirects?: () => Promise<Redirect[]>
trailingSlash?: boolean
}
const defaultConfig: NextConfig = {
......
......@@ -41,6 +41,11 @@ module.exports = {
destination: '/somewhere-else',
permanent: false,
},
{
source: '/redirect-4',
destination: '/',
permanent: false,
},
]
},
async rewrites() {
......
......@@ -42,6 +42,11 @@ module.exports = {
destination: '/somewhere-else',
permanent: false,
},
{
source: '/redirect-4',
destination: '/',
permanent: false,
},
]
},
async rewrites() {
......
......@@ -461,7 +461,7 @@ export function runTests(ctx) {
})
it('should apply redirects correctly', async () => {
for (const [path, shouldRedirect, locale] of [
for (const [path, shouldRedirect, locale, pathname] of [
['/en-US/redirect-1', true],
['/en/redirect-1', false],
['/nl/redirect-2', true],
......@@ -469,6 +469,8 @@ export function runTests(ctx) {
['/redirect-3', true],
['/en/redirect-3', true, '/en'],
['/nl-NL/redirect-3', true, '/nl-NL'],
['/redirect-4', true, null, '/'],
['/nl/redirect-4', true, null, '/nl'],
]) {
const res = await fetchViaHTTP(
ctx.appPort,
......@@ -484,7 +486,7 @@ export function runTests(ctx) {
if (shouldRedirect) {
const parsed = url.parse(res.headers.get('location'), true)
expect(parsed.pathname).toBe(
`${ctx.basePath}${locale || ''}/somewhere-else`
`${ctx.basePath}${locale || ''}${pathname || '/somewhere-else'}`
)
expect(parsed.query).toEqual({})
}
......
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