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 i18n + API rewrite is handled correctly #20751

Merged
merged 2 commits into from
Jan 6, 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: 5 additions & 2 deletions packages/next/next-server/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions test/integration/i18n-support-base-path/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ module.exports = {
source: '/rewrite-5',
destination: 'http://localhost:__EXTERNAL_PORT__',
},
{
source: '/sitemap.xml',
destination: '/api/hello',
},
]
},
async headers() {
Expand Down
4 changes: 4 additions & 0 deletions test/integration/i18n-support/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ module.exports = {
source: '/rewrite-5',
destination: 'http://localhost:__EXTERNAL_PORT__',
},
{
source: '/sitemap.xml',
destination: '/api/hello',
},
]
},
async headers() {
Expand Down
65 changes: 65 additions & 0 deletions test/integration/i18n-support/test/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down