-
Notifications
You must be signed in to change notification settings - Fork 26.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add locale prefix to middleware preflight url (#35911)
Fixes part of #34274 Navigating to `/` causes to redirect preflight request to a url of browser language like `/en`. This PR fixes to add the locale prefix always so that the redirect does not happen anymore and middleware can get a correct locale. ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `yarn lint`
- Loading branch information
Showing
9 changed files
with
146 additions
and
13 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
6 changes: 6 additions & 0 deletions
6
test/integration/middleware/with-i18n-preflight/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,6 @@ | ||
module.exports = { | ||
i18n: { | ||
locales: ['ja', 'en', 'fr'], | ||
defaultLocale: 'ja', | ||
}, | ||
} |
7 changes: 7 additions & 0 deletions
7
test/integration/middleware/with-i18n-preflight/pages/_middleware.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,7 @@ | ||
import { NextResponse } from 'next/server' | ||
|
||
export function middleware(req) { | ||
const url = req.nextUrl.clone() | ||
url.searchParams.set('locale', url.locale) | ||
return NextResponse.rewrite(url) | ||
} |
40 changes: 40 additions & 0 deletions
40
test/integration/middleware/with-i18n-preflight/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,40 @@ | ||
import Link from 'next/link' | ||
|
||
export default function Home({ locale }) { | ||
return ( | ||
<div> | ||
<div className={locale}>{locale}</div> | ||
<ul> | ||
<li> | ||
<Link href="/" locale="en"> | ||
<a id="link-en">Go to en</a> | ||
</Link> | ||
</li> | ||
<li> | ||
<Link href="/en" locale={false}> | ||
<a id="link-en2">Go to en2</a> | ||
</Link> | ||
</li> | ||
<li> | ||
<Link href="/" locale="ja"> | ||
<a id="link-ja">Go to ja</a> | ||
</Link> | ||
</li> | ||
<li> | ||
<Link href="/ja" locale={false}> | ||
<a id="link-ja2">Go to ja2</a> | ||
</Link> | ||
</li> | ||
<li> | ||
<Link href="/" locale="fr"> | ||
<a id="link-fr">Go to fr</a> | ||
</Link> | ||
</li> | ||
</ul> | ||
</div> | ||
) | ||
} | ||
|
||
export const getServerSideProps = ({ query }) => ({ | ||
props: { locale: query.locale }, | ||
}) |
69 changes: 69 additions & 0 deletions
69
test/integration/middleware/with-i18n-preflight/test/index.test.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,69 @@ | ||
/* eslint-env jest */ | ||
|
||
import { join } from 'path' | ||
import webdriver, { USE_SELENIUM } from 'next-webdriver' | ||
import { | ||
findPort, | ||
killApp, | ||
launchApp, | ||
nextBuild, | ||
nextStart, | ||
} from 'next-test-utils' | ||
|
||
jest.setTimeout(1000 * 60 * 2) | ||
|
||
const context = {} | ||
context.appDir = join(__dirname, '../') | ||
|
||
const itif = (condition) => (condition ? it : it.skip) | ||
|
||
describe('Middleware base tests', () => { | ||
describe('dev mode', () => { | ||
beforeAll(async () => { | ||
context.appPort = await findPort() | ||
context.app = await launchApp(context.appDir, context.appPort) | ||
}) | ||
|
||
afterAll(() => killApp(context.app)) | ||
|
||
runTests() | ||
}) | ||
|
||
describe('production mode', () => { | ||
beforeAll(async () => { | ||
await nextBuild(context.appDir, undefined, { | ||
stderr: true, | ||
stdout: true, | ||
}) | ||
|
||
context.appPort = await findPort() | ||
context.app = await nextStart(context.appDir, context.appPort) | ||
}) | ||
|
||
afterAll(() => killApp(context.app)) | ||
|
||
runTests() | ||
}) | ||
}) | ||
|
||
function runTests() { | ||
itif(!USE_SELENIUM)( | ||
`should send preflight for specified locale`, | ||
async () => { | ||
const browser = await webdriver(context.appPort, '/', { | ||
locale: 'en-US,en', | ||
}) | ||
await browser.waitForElementByCss('.en') | ||
await browser.elementByCss('#link-ja').click() | ||
await browser.waitForElementByCss('.ja') | ||
await browser.elementByCss('#link-en').click() | ||
await browser.waitForElementByCss('.en') | ||
await browser.elementByCss('#link-fr').click() | ||
await browser.waitForElementByCss('.fr') | ||
await browser.elementByCss('#link-ja2').click() | ||
await browser.waitForElementByCss('.ja') | ||
await browser.elementByCss('#link-en2').click() | ||
await browser.waitForElementByCss('.en') | ||
} | ||
) | ||
} |
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
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