Unverified Commit 2443c715 authored by Shu Uesugi's avatar Shu Uesugi Committed by GitHub

Fix redirect for the root path with i18n and defaultLocale (#45847)

If `config.i18n` exists with `defaultLocale` and there's a redirect that
looks like this:

```js
{
  source: '/',
  destination: '/destination',
  permanent: false,
}
```

Then, if you access `/`:
  - **Expected:** It redirects to `/destination`
  - **Actual:** redirects to `/<defaultLocale>/destination`

This PR fixes it by adding a missing special-case logic for `/` in
`packages/next/src/lib/load-custom-routes.ts`.

x-ref: [slack
thread](https://vercel.slack.com/archives/C03S8ED1DKM/p1676271611253739)

## Bug

- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

---------
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 826a237b
...@@ -525,7 +525,9 @@ function processRoutes<T>( ...@@ -525,7 +525,9 @@ function processRoutes<T>(
newRoutes.push({ newRoutes.push({
...r, ...r,
destination, destination,
source: `${srcBasePath}/${item.locale}${r.source}`, source: `${srcBasePath}/${item.locale}${
r.source === '/' && !config.trailingSlash ? '' : r.source
}`,
}) })
}) })
} }
......
module.exports = {
i18n: {
locales: ['fr', 'en'],
defaultLocale: 'en',
},
async redirects() {
return [
{
source: '/',
destination: '/destination',
permanent: false,
},
]
},
}
export default function Page() {
return null
}
/* eslint-env jest */
import url from 'url'
import http from 'http'
import { join } from 'path'
import {
launchApp,
killApp,
findPort,
nextBuild,
nextStart,
fetchViaHTTP,
} from 'next-test-utils'
const appDir = join(__dirname, '..')
let server
let externalPort
let appPort
let app
const runTests = () => {
it('should respond to default locale redirects correctly for index redirect', async () => {
for (const [path, dest] of [
['/', '/destination'],
['/en', '/destination'],
['/fr', '/fr/destination'],
]) {
const res = await fetchViaHTTP(appPort, path, undefined, {
redirect: 'manual',
})
expect(res.status).toBe(dest ? 307 : 404)
if (dest) {
const text = await res.text()
expect(text).toEqual(dest)
if (dest.startsWith('/')) {
const parsed = url.parse(res.headers.get('location'))
expect(parsed.pathname).toBe(dest)
expect(parsed.query).toBe(null)
} else {
expect(res.headers.get('location')).toBe(dest)
}
}
}
})
}
describe('Custom routes i18n with index redirect', () => {
beforeAll(async () => {
externalPort = await findPort()
server = http.createServer((_req, res) => {
res.statusCode = 200
res.end()
})
await new Promise((res, rej) => {
server.listen(externalPort, (err) => (err ? rej(err) : res()))
})
})
afterAll(async () => {
server.close()
})
describe('dev mode', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))
runTests(true)
})
describe('production mode', () => {
beforeAll(async () => {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))
runTests()
})
})
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