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

fix: infinite redirection loop when accessing user's preferred locale-prefixed path with middleware present #62435

Prev Previous commit
Next Next commit
test: add tests for preferred locale detection
masnormen committed Feb 23, 2024
commit 6d6cec73f894cfe688f074d9a27c51d9eb16612c
4 changes: 4 additions & 0 deletions test/e2e/i18n-preferred-locale-detection/app/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export async function middleware() {
const noop = () => {}
noop()
}
10 changes: 10 additions & 0 deletions test/e2e/i18n-preferred-locale-detection/app/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
i18n: {
locales: ['en', 'id'],
defaultLocale: 'en',
},
experimental: {
clientRouterFilter: true,
clientRouterFilterRedirects: true,
},
}
21 changes: 21 additions & 0 deletions test/e2e/i18n-preferred-locale-detection/app/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Link from 'next/link'

export const getServerSideProps = async ({ locale }) => {
return {
props: {
locale,
},
}
}

export default function Home({ locale }) {
return (
<div>
<div id="index">Index</div>
<div id="current-locale">{locale}</div>
<Link href="/new" id="to-new">
To new
</Link>
</div>
)
}
21 changes: 21 additions & 0 deletions test/e2e/i18n-preferred-locale-detection/app/pages/new.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Link from 'next/link'

export const getServerSideProps = async ({ locale }) => {
return {
props: {
locale,
},
}
}

export default function New({ locale }) {
return (
<div>
<div id="new">New</div>
<div id="current-locale">{locale}</div>
<Link href="/" id="to-index">
To index (No Locale Specified)
</Link>
</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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: any) => {
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')
})
})