未验证 提交 75ba3db2 编写于 作者: T Tim Neutkens 提交者: GitHub

Make sure API routes are built in production (#7306)

* Make sure API routes is built correctly

* Don’t create client-side bundle for API routes

* Add tests for production
上级 c73a76b3
......@@ -50,17 +50,23 @@ export function createEntrypoints(pages: PagesMapping, target: 'server'|'serverl
Object.keys(pages).forEach((page) => {
const absolutePagePath = pages[page]
const bundleFile = page === '/' ? '/index.js' : `${page}.js`
const isApiRoute = bundleFile.startsWith('/api')
const bundlePath = join('static', buildId, 'pages', bundleFile)
if(target === 'serverless' && page !== '/_app' && page !== '/_document') {
if(isApiRoute || target === 'server') {
server[bundlePath] = [absolutePagePath]
} else if(target === 'serverless' && page !== '/_app' && page !== '/_document') {
const serverlessLoaderOptions: ServerlessLoaderQuery = {page, absolutePagePath, ...defaultServerlessOptions}
server[join('pages', bundleFile)] = `next-serverless-loader?${stringify(serverlessLoaderOptions)}!`
} else if(target === 'server') {
server[bundlePath] = [absolutePagePath]
}
if (page === '/_document') {
return
}
client[bundlePath] = `next-client-pages-loader?${stringify({page, absolutePagePath})}!`
if(!isApiRoute) {
client[bundlePath] = `next-client-pages-loader?${stringify({page, absolutePagePath})}!`
}
})
return {
......
export default (req, res) => {
res.end('API hello works')
}
export default (req, res) => {
res.end('API index works')
}
......@@ -154,6 +154,22 @@ describe('Production Usage', () => {
})
})
describe('API routes', () => {
it('should work with pages/api/index.js', async () => {
const url = `http://localhost:${appPort}/api`
const res = await fetch(url)
const body = await res.text()
expect(body).toEqual('API index works')
})
it('should work with pages/api/hello.js', async () => {
const url = `http://localhost:${appPort}/api/hello`
const res = await fetch(url)
const body = await res.text()
expect(body).toEqual('API hello works')
})
})
describe('With navigation', () => {
it('should navigate via client side', async () => {
const browser = await webdriver(appPort, '/')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册