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

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 16 additions & 9 deletions packages/next/shared/lib/router/router.ts
Expand Up @@ -125,6 +125,11 @@ function addPathPrefix(path: string, prefix?: string) {
)
}

// NOTE: Does NOT work when path contains ? or #
nkzawa marked this conversation as resolved.
Show resolved Hide resolved
function hasPathPrefix(path: string, prefix: string) {
return path === prefix || path.startsWith(prefix + '/')
}

export function getDomainLocale(
path: string,
locale?: string | false,
Expand Down Expand Up @@ -153,16 +158,18 @@ export function addLocale(
defaultLocale?: string
) {
if (process.env.__NEXT_I18N_SUPPORT) {
const pathname = pathNoQueryHash(path)
const pathLower = pathname.toLowerCase()
const localeLower = locale && locale.toLowerCase()
if (locale && locale !== defaultLocale) {
const pathname = pathNoQueryHash(path)
const pathLower = pathname.toLowerCase()
const localeLower = locale.toLowerCase()

return locale &&
locale !== defaultLocale &&
!pathLower.startsWith('/' + localeLower + '/') &&
pathLower !== '/' + localeLower
? addPathPrefix(path, '/' + locale)
: path
if (
!hasPathPrefix(pathLower, '/' + localeLower) &&
!hasPathPrefix(pathLower, '/api')
) {
return addPathPrefix(path, '/' + locale)
}
}
}
return path
}
Expand Down
4 changes: 4 additions & 0 deletions test/integration/i18n-support/pages/index.js
Expand Up @@ -47,6 +47,10 @@ export default function Page(props) {
<a id="to-gssp-slug">to /gssp/first</a>
</Link>
<br />
<Link href="/api/post/asdf">
<a id="to-api-post">to /api/post/[slug]</a>
</Link>
<br />
</>
)
}
Expand Down
10 changes: 10 additions & 0 deletions test/integration/i18n-support/test/index.test.js
Expand Up @@ -495,6 +495,16 @@ describe('i18n Support', () => {
expect(await browser.eval('window.location.pathname')).toBe(
`${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)
}
})
}
Expand Down
3 changes: 3 additions & 0 deletions test/integration/middleware/core/pages/api/ok.js
@@ -0,0 +1,3 @@
export default function handler(req, res) {
res.send('ok')
}
Expand Up @@ -58,4 +58,10 @@ export async function middleware(request) {
url.pathname = '/redirects/infinite-loop'
return Response.redirect(url)
}

if (url.pathname === '/redirects/to') {
url.pathname = url.searchParams.get('pathname')
url.searchParams.delete('pathname')
return Response.redirect(url)
}
}
4 changes: 4 additions & 0 deletions test/integration/middleware/core/pages/redirects/index.js
Expand Up @@ -28,6 +28,10 @@ export default function Home() {
<a>Redirect me alot (infinite loop)</a>
</Link>
<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>
)
}
8 changes: 8 additions & 0 deletions test/integration/middleware/core/test/index.test.js
Expand Up @@ -579,6 +579,14 @@ function redirectTests(locale = '') {
fetchViaHTTP(context.appPort, `${locale}/redirects/infinite-loop`)
).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 = '') {
Expand Down
7 changes: 1 addition & 6 deletions test/lib/browsers/playwright.ts
Expand Up @@ -315,12 +315,7 @@ class Playwright extends BrowserInterface {

waitForCondition(condition, timeout) {
return this.chain(() => {
return page.waitForFunction(
`function() {
return ${condition}
}`,
{ timeout }
)
return page.waitForFunction(condition, { timeout })
})
}

Expand Down