26de5ca269
This moves the locale redirect handling out of `base-server` as it shouldn't be handled here and should be at the routing level. This avoids the duplicate handling with middleware that causes the incorrect detection/infinite looping. Test case from separate PR was carried over to prevent regression. Fixes: https://github.com/vercel/next.js/issues/55648 Closes: https://github.com/vercel/next.js/pull/62435 Closes: NEXT-2627 Closes: NEXT-2628 --------- Co-authored-by: Nourman Hajar <nourmanhajar@gmail.com> Co-authored-by: samcx <sam@vercel.com>
63 lines
2 KiB
TypeScript
63 lines
2 KiB
TypeScript
import type { Request } from 'playwright'
|
|
import { join } from 'path'
|
|
import { FileRef, nextTestSetup } from 'e2e-utils'
|
|
|
|
describe('i18-preferred-locale-redirect', () => {
|
|
const { next } = nextTestSetup({
|
|
files: new FileRef(join(__dirname, './app/')),
|
|
})
|
|
|
|
it('should request a path prefixed with my preferred detected locale when accessing index', async () => {
|
|
const browser = await next.browser('/new', {
|
|
locale: 'id',
|
|
})
|
|
|
|
let requestedPreferredLocalePathCount = 0
|
|
browser.on('request', (request: Request) => {
|
|
if (new URL(request.url(), 'http://n').pathname === '/id') {
|
|
requestedPreferredLocalePathCount++
|
|
}
|
|
})
|
|
|
|
const goToIndex = async () => {
|
|
await browser.get(next.url)
|
|
}
|
|
|
|
await expect(goToIndex()).resolves.not.toThrow(/ERR_TOO_MANY_REDIRECTS/)
|
|
|
|
await browser.waitForElementByCss('#index')
|
|
|
|
expect(await browser.elementByCss('#index').text()).toBe('Index')
|
|
expect(await browser.elementByCss('#current-locale').text()).toBe('id')
|
|
|
|
expect(requestedPreferredLocalePathCount).toBe(1)
|
|
})
|
|
|
|
it('should not request a path prefixed with my preferred detected locale when clicking link to index from a non-locale-prefixed path', async () => {
|
|
const browser = await next.browser('/new', {
|
|
locale: 'id',
|
|
})
|
|
|
|
await browser
|
|
.waitForElementByCss('#to-index')
|
|
.click()
|
|
.waitForElementByCss('#index')
|
|
|
|
expect(await browser.elementByCss('#index').text()).toBe('Index')
|
|
expect(await browser.elementByCss('#current-locale').text()).toBe('en')
|
|
})
|
|
|
|
it('should request a path prefixed with my preferred detected locale when clicking link to index from a locale-prefixed path', async () => {
|
|
const browser = await next.browser('/id/new', {
|
|
locale: 'id',
|
|
})
|
|
|
|
await browser
|
|
.waitForElementByCss('#to-index')
|
|
.click()
|
|
.waitForElementByCss('#index')
|
|
|
|
expect(await browser.elementByCss('#index').text()).toBe('Index')
|
|
expect(await browser.elementByCss('#current-locale').text()).toBe('id')
|
|
})
|
|
})
|