未验证 提交 0d059045 编写于 作者: J Jan Potoms 提交者: GitHub

Fix catch-all route + index.js in dev when accessed with trailing slash (#10502)

Failing test case for https://github.com/zeit/next.js/issues/10488#issuecomment-584500081

This used to give a 500 in dev environment
上级 670b2c15
......@@ -604,7 +604,7 @@ export default class Server {
const handled = await this.handleApiRequest(
req as NextApiRequest,
res as NextApiResponse,
pathname!,
pathname,
query
)
if (handled) {
......@@ -701,7 +701,16 @@ export default class Server {
// or else it won't be in the manifest yet
await this.ensureApiPage(page)
const builtPagePath = await this.getPagePath(page)
let builtPagePath
try {
builtPagePath = await this.getPagePath(page)
} catch (err) {
if (err.code === 'ENOENT') {
return false
}
throw err
}
const pageModule = require(builtPagePath)
query = { ...query, ...params }
......
export default function(req, res) {
res.json(req.query)
}
/* eslint-env jest */
import fs from 'fs-extra'
import { join } from 'path'
import {
killApp,
findPort,
launchApp,
fetchViaHTTP,
nextBuild,
nextStart,
} from 'next-test-utils'
jest.setTimeout(1000 * 60 * 2)
const appDir = join(__dirname, '../')
const nextConfig = join(appDir, 'next.config.js')
let appPort
let app
function runTests() {
it('should return data when catch-all', async () => {
const data = await fetchViaHTTP(appPort, '/api/users/1', null, {}).then(
res => res.ok && res.json()
)
expect(data).toEqual({ slug: ['1'] })
})
it('should 404 when catch-all with index and trailing slash', async () => {
const data = await fetchViaHTTP(appPort, '/api/users/', null, {}).then(
res => res.status
)
expect(data).toEqual(404)
})
it('should return data when catch-all with index and no trailing slash', async () => {
const data = await fetchViaHTTP(appPort, '/api/users', null, {}).then(
res => res.ok && res.json()
)
expect(data).toEqual({})
})
}
describe('API routes', () => {
describe('dev support', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))
runTests()
})
describe('Server support', () => {
beforeAll(async () => {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))
runTests()
})
describe('Serverless support', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfig,
`module.exports = { target: 'serverless' }`
)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
await fs.remove(nextConfig)
})
runTests()
})
})
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册