rsnext/test/e2e/i18n-default-locale-redirect/i18n-default-locale-redirect.test.ts
Shu Uesugi 723626cf48
Handle defaultLocale on client router filter (#47180)
x-ref: [slack
thread](https://vercel.slack.com/archives/C03S8ED1DKM/p1678838567947919)

Follow-up to https://github.com/vercel/next.js/pull/46317. The issue is
that, if:

- `experimental.clientRouterFilter` is enabled
- `i18n` is enabled with `defaultLocale` set
- Next.js router navigates to a path that (1) is the same as
`defaultLocale` and (2) will be redirected,

then:

- **Expected:** Should hard-navigate to this path without any locale
prefix (and then redirect occurs)
- **Actual:** Hard-navigates to this path with `defaultLocale` prefix,
even though it's not needed (and then redirect occurrs)

### Solution

This PR fixes the above issue by adding `defaultLocale` to `addLocale`
which is passed to `handleHardNavigation`. [`addLocale` skips adding the
locale if `locale` is equal to
`defaultLocale`](02125cf3b1/packages/next/src/shared/lib/router/utils/add-locale.ts (L17)).

### Fixing a bug

- [x] Related issues linked using `fixes #number`
- [x] Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs

Co-authored-by: JJ Kasper <jj@jjsweb.site>
2023-03-16 11:58:02 -07:00

44 lines
1.5 KiB
TypeScript

import { join } from 'path'
import { createNextDescribe, FileRef } from 'e2e-utils'
createNextDescribe(
'i18-default-locale-redirect',
{
files: {
pages: new FileRef(join(__dirname, './app/pages')),
'next.config.js': new FileRef(join(__dirname, './app/next.config.js')),
},
},
({ next }) => {
it('should not request a path prefixed with default locale', async () => {
const browser = await next.browser('/')
let requestedDefaultLocalePath = false
browser.on('request', (request: any) => {
if (new URL(request.url(), 'http://n').pathname === '/en/to-new') {
requestedDefaultLocalePath = true
}
})
await browser.elementByCss('#to-new').click().waitForElementByCss('#new')
expect(await browser.elementByCss('#new').text()).toBe('New')
expect(requestedDefaultLocalePath).toBe(false)
})
it('should request a path prefixed with non-default locale', async () => {
const browser = await next.browser('/')
let requestedNonDefaultLocalePath = false
browser.on('request', (request: any) => {
if (new URL(request.url(), 'http://n').pathname === '/nl/to-new') {
requestedNonDefaultLocalePath = true
}
})
await browser
.elementByCss('#to-new-nl')
.click()
.waitForElementByCss('#new')
expect(await browser.elementByCss('#new').text()).toBe('New')
expect(requestedNonDefaultLocalePath).toBe(true)
})
}
)