Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove extra redirect for index with i18n #20397

Merged
merged 5 commits into from
Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/next/export/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ interface ExportPageInput {
renderOpts: RenderOpts
buildExport?: boolean
serverRuntimeConfig: string
subFolders: string
subFolders?: boolean
serverless: boolean
optimizeFonts: boolean
optimizeImages: boolean
Expand Down
4 changes: 3 additions & 1 deletion packages/next/lib/load-custom-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand Down
4 changes: 3 additions & 1 deletion packages/next/next-server/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,6 +27,8 @@ export type NextConfig = { [key: string]: any } & {
headers?: () => Promise<Header[]>
rewrites?: () => Promise<Rewrite[]>
redirects?: () => Promise<Redirect[]>

trailingSlash?: boolean
}

const defaultConfig: NextConfig = {
Expand Down
5 changes: 5 additions & 0 deletions test/integration/i18n-support-base-path/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ module.exports = {
destination: '/somewhere-else',
permanent: false,
},
{
source: '/redirect-4',
destination: '/',
permanent: false,
},
]
},
async rewrites() {
Expand Down
5 changes: 5 additions & 0 deletions test/integration/i18n-support/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ module.exports = {
destination: '/somewhere-else',
permanent: false,
},
{
source: '/redirect-4',
destination: '/',
permanent: false,
},
]
},
async rewrites() {
Expand Down
6 changes: 4 additions & 2 deletions test/integration/i18n-support/test/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,14 +461,16 @@ 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],
['/fr/redirect-2', false],
['/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,
Expand All @@ -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({})
}
Expand Down