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

Fix rewriting to API routes not including query #10223

Merged
merged 1 commit into from Jan 23, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 20 additions & 12 deletions packages/next/next-server/server/next-server.ts
Expand Up @@ -561,7 +561,8 @@ export default class Server {
const handled = await this.handleApiRequest(
req as NextApiRequest,
res as NextApiResponse,
pathname!
pathname!,
query
)
if (handled) {
return { finished: true }
Expand Down Expand Up @@ -633,7 +634,8 @@ export default class Server {
private async handleApiRequest(
req: IncomingMessage,
res: ServerResponse,
pathname: string
pathname: string,
query: ParsedUrlQuery
) {
let page = pathname
let params: Params | boolean = false
Expand All @@ -659,15 +661,17 @@ export default class Server {

const builtPagePath = await this.getPagePath(page)
const pageModule = require(builtPagePath)
query = { ...query, ...params }

if (!this.renderOpts.dev && this._isLikeServerless) {
if (typeof pageModule.default === 'function') {
this.prepareServerlessUrl(req, query)
await pageModule.default(req, res)
return true
}
}

await apiResolver(req, res, params, pageModule, this.onErrorMiddleware)
await apiResolver(req, res, query, pageModule, this.onErrorMiddleware)
return true
}

Expand Down Expand Up @@ -831,6 +835,18 @@ export default class Server {
res.end(payload)
}

private prepareServerlessUrl(req: IncomingMessage, query: ParsedUrlQuery) {
const curUrl = parseUrl(req.url!, true)
req.url = formatUrl({
...curUrl,
search: undefined,
query: {
...curUrl.query,
...query,
},
})
}

private async renderToHTMLWithComponents(
req: IncomingMessage,
res: ServerResponse,
Expand All @@ -854,15 +870,7 @@ export default class Server {
if (!isSSG) {
// handle serverless
if (isLikeServerless) {
const curUrl = parseUrl(req.url!, true)
req.url = formatUrl({
...curUrl,
search: undefined,
query: {
...curUrl.query,
...query,
},
})
this.prepareServerlessUrl(req, query)
return result.Component.renderReqToHTML(req, res)
}

Expand Down
12 changes: 12 additions & 0 deletions test/integration/custom-routes/next.config.js
Expand Up @@ -55,6 +55,18 @@ module.exports = {
source: '/hidden/_next/:path*',
destination: '/_next/:path*',
},
{
source: '/api-hello',
destination: '/api/hello',
},
{
source: '/api-hello-regex/(.*)',
destination: '/api/hello?name=:1',
},
{
source: '/api-hello-param/:name',
destination: '/api/hello?name=:name',
},
]
},
async redirects() {
Expand Down
1 change: 1 addition & 0 deletions test/integration/custom-routes/pages/api/hello.js
@@ -0,0 +1 @@
export default async (req, res) => res.json({ query: req.query })
32 changes: 32 additions & 0 deletions test/integration/custom-routes/test/index.test.js
Expand Up @@ -260,6 +260,23 @@ const runTests = (isDev = false) => {
expect(res.headers.get('refresh')).toBe(`0;url=/`)
})

it('should handle basic api rewrite successfully', async () => {
const data = await renderViaHTTP(appPort, '/api-hello')
expect(JSON.parse(data)).toEqual({ query: {} })
})

it('should handle api rewrite with un-named param successfully', async () => {
const data = await renderViaHTTP(appPort, '/api-hello-regex/hello/world')
expect(JSON.parse(data)).toEqual({
query: { '1': 'hello/world', name: 'hello/world' },
})
})

it('should handle api rewrite with param successfully', async () => {
const data = await renderViaHTTP(appPort, '/api-hello-param/hello')
expect(JSON.parse(data)).toEqual({ query: { name: 'hello' } })
})

if (!isDev) {
it('should output routes-manifest successfully', async () => {
const manifest = await fs.readJSON(
Expand Down Expand Up @@ -476,6 +493,21 @@ const runTests = (isDev = false) => {
),
source: '/hidden/_next/:path*',
},
{
destination: '/api/hello',
regex: normalizeRegEx('^\\/api-hello$'),
source: '/api-hello',
},
{
destination: '/api/hello?name=:1',
regex: normalizeRegEx('^\\/api-hello-regex(?:\\/(.*))$'),
source: '/api-hello-regex/(.*)',
},
{
destination: '/api/hello?name=:name',
regex: normalizeRegEx('^\\/api-hello-param(?:\\/([^\\/]+?))$'),
source: '/api-hello-param/:name',
},
],
dynamicRoutes: [
{
Expand Down