Unverified Commit 723626cf authored by Shu Uesugi's avatar Shu Uesugi Committed by GitHub

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`](https://github.com/vercel/next.js/blob/02125cf3b1dfba52b240fd6e0f959d896e4b6195/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-nextjsCo-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 1255e199
...@@ -1096,7 +1096,9 @@ export default class Router implements BaseRouter { ...@@ -1096,7 +1096,9 @@ export default class Router implements BaseRouter {
// a hard navigation // a hard navigation
if (matchesBflStatic || matchesBflDynamic) { if (matchesBflStatic || matchesBflDynamic) {
handleHardNavigation({ handleHardNavigation({
url: addBasePath(addLocale(as, locale || this.locale)), url: addBasePath(
addLocale(as, locale || this.locale, this.defaultLocale)
),
router: this, router: this,
}) })
return new Promise(() => {}) return new Promise(() => {})
......
module.exports = {
i18n: {
locales: ['en', 'nl'],
defaultLocale: 'en',
},
experimental: {
clientRouterFilter: true,
clientRouterFilterRedirects: true,
},
redirects() {
return [
{
source: '/to-new',
destination: '/new',
permanent: false,
},
]
},
}
import Link from 'next/link'
export default function Home() {
return (
<div>
<div>
<Link href="/to-new" id="to-new">
To new (Default Locale)
</Link>
</div>
<div>
<Link href="/to-new" id="to-new-nl" locale="nl">
To new (NL)
</Link>
</div>
</div>
)
}
export default function New() {
return <div id="new">New</div>
}
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)
})
}
)
...@@ -37,7 +37,7 @@ export class Playwright extends BrowserInterface { ...@@ -37,7 +37,7 @@ export class Playwright extends BrowserInterface {
if (!this.eventCallbacks[event]) { if (!this.eventCallbacks[event]) {
throw new Error( throw new Error(
`Invalid event passed to browser.on, received ${event}. Valid events are ${Object.keys( `Invalid event passed to browser.on, received ${event}. Valid events are ${Object.keys(
event this.eventCallbacks
)}` )}`
) )
} }
......
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