Unverified Commit b9ba264b authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure i18n + API rewrite is handled correctly (#20751)

This ensures rewrites to API routes with i18n enabled handles as an API route correctly. This also adds tests for API routes in the i18n test suite

Fixes: https://github.com/vercel/next.js/issues/20725
parent 1a3adaa5
......@@ -908,7 +908,7 @@ export default class Server {
match: route('/:path*'),
type: 'route',
name: 'Catchall render',
fn: async (req, res, params, parsedUrl) => {
fn: async (req, res, _params, parsedUrl) => {
let { pathname, query } = parsedUrl
if (!pathname) {
throw new Error('pathname is undefined')
......@@ -929,7 +929,7 @@ export default class Server {
}
}
if (params?.path?.[0] === 'api') {
if (pathname === '/api' || pathname.startsWith('/api/')) {
const handled = await this.handleApiRequest(
req as NextApiRequest,
res as NextApiResponse,
......@@ -1045,6 +1045,9 @@ export default class Server {
const pageModule = await require(builtPagePath)
query = { ...query, ...params }
delete query.__nextLocale
delete query.__nextDefaultLocale
if (!this.renderOpts.dev && this._isLikeServerless) {
if (typeof pageModule.default === 'function') {
prepareServerlessUrl(req, query)
......
......@@ -82,6 +82,10 @@ module.exports = {
source: '/rewrite-5',
destination: 'http://localhost:__EXTERNAL_PORT__',
},
{
source: '/sitemap.xml',
destination: '/api/hello',
},
]
},
async headers() {
......
......@@ -83,6 +83,10 @@ module.exports = {
source: '/rewrite-5',
destination: 'http://localhost:__EXTERNAL_PORT__',
},
{
source: '/sitemap.xml',
destination: '/api/hello',
},
]
},
async headers() {
......
......@@ -670,6 +670,71 @@ export function runTests(ctx) {
}
})
it('should visit API route directly correctly', async () => {
for (const locale of locales) {
const res = await fetchViaHTTP(
ctx.appPort,
`${ctx.basePath || ''}${
locale === 'en-US' ? '' : `/${locale}`
}/api/hello`,
undefined,
{
redirect: 'manual',
}
)
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`,
undefined,
{
redirect: 'manual',
}
)
const data = await res.json()
expect(data).toEqual({
post: true,
query: {
slug: 'first',
},
})
}
})
it('should rewrite to API route correctly', async () => {
for (const locale of locales) {
const res = await fetchViaHTTP(
ctx.appPort,
`${ctx.basePath || ''}${
locale === 'en-US' ? '' : `/${locale}`
}/sitemap.xml`,
undefined,
{
redirect: 'manual',
}
)
const data = await res.json()
expect(data).toEqual({
hello: true,
query: {},
})
}
})
it('should apply rewrites correctly', async () => {
let res = await fetchViaHTTP(
ctx.appPort,
......
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