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

Ensure API routes are not available under the locale #26629

Merged
merged 2 commits into from
Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/next/next-server/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,13 @@ export default class Server {
pathname: localePathResult.pathname,
})
;(req as any).__nextStrippedLocale = true

if (
localePathResult.pathname === '/api' ||
localePathResult.pathname.startsWith('/api/')
) {
return this.render404(req, res, parsedUrl)
}
}

// If a detected locale is a domain specific locale and we aren't already
Expand Down
48 changes: 29 additions & 19 deletions test/integration/i18n-support/test/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async function addDefaultLocaleCookie(browser) {
}

export function runTests(ctx) {
it.only('should have domainLocales available on useRouter', async () => {
it('should have domainLocales available on useRouter', async () => {
const browser = await webdriver(ctx.appPort, `${ctx.basePath || '/'}`)
expect(
JSON.parse(await browser.elementByCss('#router-domain-locales').text())
Expand Down Expand Up @@ -1146,44 +1146,54 @@ export function runTests(ctx) {
for (const locale of locales) {
const res = await fetchViaHTTP(
ctx.appPort,
`${ctx.basePath || ''}${
locale === 'en-US' ? '' : `/${locale}`
}/api/hello`,
`${ctx.basePath || ''}/${locale}/api/hello`,
undefined,
{
redirect: 'manual',
}
)

const data = await res.json()
expect(data).toEqual({
hello: true,
query: {},
})
expect(res.status).toBe(404)
}

const res = await fetchViaHTTP(
ctx.appPort,
`${ctx.basePath || ''}/api/hello`
)

const data = await res.json()
expect(data).toEqual({
hello: true,
query: {},
})
})

it('should visit dynamic API route directly correctly', async () => {
for (const locale of locales) {
const res = await fetchViaHTTP(
ctx.appPort,
`${ctx.basePath || ''}${
locale === 'en-US' ? '' : `/${locale}`
}/api/post/first`,
`${ctx.basePath || ''}/${locale}/api/post/first`,
undefined,
{
redirect: 'manual',
}
)

const data = await res.json()
expect(data).toEqual({
post: true,
query: {
slug: 'first',
},
})
expect(res.status).toBe(404)
}

const res = await fetchViaHTTP(
ctx.appPort,
`${ctx.basePath || ''}/api/post/first`
)

const data = await res.json()
expect(data).toEqual({
post: true,
query: {
slug: 'first',
},
})
})

it('should rewrite to API route correctly', async () => {
Expand Down