-
Notifications
You must be signed in to change notification settings - Fork 27.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate locale redirect handling to router-server (#62606)
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: #55648 Closes: #62435 Closes: NEXT-2627 Closes: NEXT-2628 --------- Co-authored-by: Nourman Hajar <nourmanhajar@gmail.com> Co-authored-by: samcx <sam@vercel.com>
- Loading branch information
1 parent
2f210d4
commit 960b738
Showing
7 changed files
with
174 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
test/e2e/i18n-preferred-locale-detection/app/next.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
test/e2e/i18n-preferred-locale-detection/app/pages/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) | ||
} |
63 changes: 63 additions & 0 deletions
63
test/e2e/i18n-preferred-locale-detection/i18n-preferred-locale-detection.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
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') | ||
}) | ||
}) |