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