Commit 4ad934c1 authored by Lukáš Huvar's avatar Lukáš Huvar Committed by Joe Haddad

Serverless build for API routes (#7649)

* Fixes serverless target in development

* Fix serverless build

* Fix serverless build

* Add manifest test

* Better output

* Fix manifest test

* Update snapshot

* Remove snapshots
parent af94c27a
......@@ -71,7 +71,10 @@ export function createEntrypoints(
const isApiRoute = bundleFile.startsWith('/api')
const bundlePath = join('static', buildId, 'pages', bundleFile)
if (isApiRoute || target === 'server') {
if (isApiRoute && target === 'serverless') {
server[join('pages', bundleFile)] = [absolutePagePath]
} else if (isApiRoute || target === 'server') {
server[bundlePath] = [absolutePagePath]
} else if (
target === 'serverless' &&
......
......@@ -455,7 +455,6 @@ export default async function build(dir: string, conf = null): Promise<void> {
// remove temporary export folder
await recursiveDelete(exportOptions.outdir)
await fsRmdir(exportOptions.outdir)
await fsWriteFile(manifestPath, JSON.stringify(pagesManifest), 'utf8')
}
staticPages.forEach(pg => allStaticPages.add(pg))
......
......@@ -84,13 +84,9 @@ export function printTreeView(
? chalk.cyan('AMP')
: pageInfo.size >= 0
? getPrettySize(pageInfo.size)
: 'N/A',
pageInfo.chunks
? pageInfo.chunks.internal.size.toString()
: 'N/A',
pageInfo.chunks
? pageInfo.chunks.external.size.toString()
: 'N/A',
: '',
pageInfo.chunks ? pageInfo.chunks.internal.size.toString() : '',
pageInfo.chunks ? pageInfo.chunks.external.size.toString() : '',
]
: ['', '', '']),
])
......
/* eslint-env jest */
/* global jasmine */
import { join } from 'path'
import { existsSync } from 'fs'
import { existsSync, readFileSync } from 'fs'
import {
killApp,
findPort,
launchApp,
fetchViaHTTP,
renderViaHTTP
renderViaHTTP,
nextBuild,
File
} from 'next-test-utils'
import json from '../big.json'
......@@ -195,6 +197,23 @@ function runTests (serverless = false) {
expect(res.status).toBe(404)
})
it('should build api routes', async () => {
await nextBuild(appDir, [], { stdout: true })
if (serverless) {
const pagesManifest = JSON.parse(
readFileSync(
join(appDir, '.next/serverless/pages-manifest.json'),
'utf8'
)
)
expect(Object.keys(pagesManifest).includes('/api/[post]')).toBeTruthy()
} else {
expect(
existsSync(join(appDir, '.next/server/pages-manifest.json'), 'utf8')
).toBeTruthy()
}
})
it('should return data on dynamic optional nested route', async () => {
const data = await fetchViaHTTP(
appPort,
......@@ -242,13 +261,14 @@ describe('API routes', () => {
runTests()
})
// TODO: after fixing serverless build
describe('Serverless support', () => {
// const configPath = join(appDir, 'next.config.js')
// const originalConfig = readFileSync(configPath, 'utf8')
// const editedConfig = originalConfig.replace('server', 'serverless')
// writeFileSync(configPath, editedConfig, 'utf8')
// runTests(true)
// writeFileSync(configPath, originalConfig, 'utf8')
const nextConfig = new File(join(appDir, 'next.config.js'))
beforeEach(() => {
nextConfig.replace('server', 'serverless')
})
afterEach(() => {
nextConfig.restore()
})
runTests(true)
})
})
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