Unverified Commit 93678b56 authored by Naoyuki Kanezawa's avatar Naoyuki Kanezawa Committed by GitHub

Do not add locale to link for api route and middleware preflight (#35994)

Fixes https://github.com/vercel/next.js/issues/33578

This PR fixes `Link` component as well to not add locale for api routes since both preflight redirect and link should work the same.

Additionally, it fixes the webdriver method `browser.waitForCondition()` for playwright to not wrap with function string.

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`
parent aaa823c3
...@@ -125,6 +125,11 @@ function addPathPrefix(path: string, prefix?: string) { ...@@ -125,6 +125,11 @@ function addPathPrefix(path: string, prefix?: string) {
) )
} }
function hasPathPrefix(path: string, prefix: string) {
path = pathNoQueryHash(path)
return path === prefix || path.startsWith(prefix + '/')
}
export function getDomainLocale( export function getDomainLocale(
path: string, path: string,
locale?: string | false, locale?: string | false,
...@@ -153,16 +158,18 @@ export function addLocale( ...@@ -153,16 +158,18 @@ export function addLocale(
defaultLocale?: string defaultLocale?: string
) { ) {
if (process.env.__NEXT_I18N_SUPPORT) { if (process.env.__NEXT_I18N_SUPPORT) {
const pathname = pathNoQueryHash(path) if (locale && locale !== defaultLocale) {
const pathLower = pathname.toLowerCase() const pathname = pathNoQueryHash(path)
const localeLower = locale && locale.toLowerCase() const pathLower = pathname.toLowerCase()
const localeLower = locale.toLowerCase()
return locale && if (
locale !== defaultLocale && !hasPathPrefix(pathLower, '/' + localeLower) &&
!pathLower.startsWith('/' + localeLower + '/') && !hasPathPrefix(pathLower, '/api')
pathLower !== '/' + localeLower ) {
? addPathPrefix(path, '/' + locale) return addPathPrefix(path, '/' + locale)
: path }
}
} }
return path return path
} }
...@@ -194,8 +201,7 @@ function pathNoQueryHash(path: string) { ...@@ -194,8 +201,7 @@ function pathNoQueryHash(path: string) {
} }
export function hasBasePath(path: string): boolean { export function hasBasePath(path: string): boolean {
path = pathNoQueryHash(path) return hasPathPrefix(path, basePath)
return path === basePath || path.startsWith(basePath + '/')
} }
export function addBasePath(path: string): string { export function addBasePath(path: string): string {
......
...@@ -47,6 +47,10 @@ export default function Page(props) { ...@@ -47,6 +47,10 @@ export default function Page(props) {
<a id="to-gssp-slug">to /gssp/first</a> <a id="to-gssp-slug">to /gssp/first</a>
</Link> </Link>
<br /> <br />
<Link href="/api/post/asdf">
<a id="to-api-post">to /api/post/[slug]</a>
</Link>
<br />
</> </>
) )
} }
......
...@@ -495,6 +495,16 @@ describe('i18n Support', () => { ...@@ -495,6 +495,16 @@ describe('i18n Support', () => {
expect(await browser.eval('window.location.pathname')).toBe( expect(await browser.eval('window.location.pathname')).toBe(
`${localePath}gssp/first/` `${localePath}gssp/first/`
) )
await browser.back().waitForElementByCss('#index')
await browser.elementByCss('#to-api-post').click()
await browser.waitForCondition(
'window.location.pathname === "/api/post/asdf/"'
)
const body = await browser.elementByCss('body').text()
const json = JSON.parse(body)
expect(json.post).toBe(true)
} }
}) })
} }
......
export default function handler(req, res) {
res.send('ok')
}
...@@ -58,4 +58,10 @@ export async function middleware(request) { ...@@ -58,4 +58,10 @@ export async function middleware(request) {
url.pathname = '/redirects/infinite-loop' url.pathname = '/redirects/infinite-loop'
return Response.redirect(url) return Response.redirect(url)
} }
if (url.pathname === '/redirects/to') {
url.pathname = url.searchParams.get('pathname')
url.searchParams.delete('pathname')
return Response.redirect(url)
}
} }
...@@ -28,6 +28,10 @@ export default function Home() { ...@@ -28,6 +28,10 @@ export default function Home() {
<a>Redirect me alot (infinite loop)</a> <a>Redirect me alot (infinite loop)</a>
</Link> </Link>
<div /> <div />
<Link href="/redirects/to?pathname=/api/ok" locale="nl">
<a id="link-to-api-with-locale">>Redirect me to api with locale</a>
</Link>
<div />
</div> </div>
) )
} }
...@@ -579,6 +579,14 @@ function redirectTests(locale = '') { ...@@ -579,6 +579,14 @@ function redirectTests(locale = '') {
fetchViaHTTP(context.appPort, `${locale}/redirects/infinite-loop`) fetchViaHTTP(context.appPort, `${locale}/redirects/infinite-loop`)
).rejects.toThrow() ).rejects.toThrow()
}) })
it(`${locale} should redirect to api route with locale`, async () => {
const browser = await webdriver(context.appPort, `${locale}/redirects`)
await browser.elementByCss('#link-to-api-with-locale').click()
await browser.waitForCondition('window.location.pathname === "/api/ok"')
const body = await browser.elementByCss('body').text()
expect(body).toBe('ok')
})
} }
function responseTests(locale = '') { function responseTests(locale = '') {
......
...@@ -315,12 +315,7 @@ class Playwright extends BrowserInterface { ...@@ -315,12 +315,7 @@ class Playwright extends BrowserInterface {
waitForCondition(condition, timeout) { waitForCondition(condition, timeout) {
return this.chain(() => { return this.chain(() => {
return page.waitForFunction( return page.waitForFunction(condition, { timeout })
`function() {
return ${condition}
}`,
{ timeout }
)
}) })
} }
......
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