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