Skip to content

Commit

Permalink
Fix serverless chunking (vercel#8569)
Browse files Browse the repository at this point in the history
* Fix Serverless Chunking

* Remove old test & environment variable

* Update serverless trace test case
  • Loading branch information
Timer committed Aug 30, 2019
1 parent 3405943 commit 67e7753
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 65 deletions.
2 changes: 1 addition & 1 deletion packages/next-server/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const defaultConfig: { [key: string]: any } = {
generateBuildId: () => null,
generateEtags: true,
pageExtensions: ['tsx', 'ts', 'jsx', 'js'],
target: process.env.__NEXT_BUILDER_EXPERIMENTAL_TARGET || 'server',
target: 'server',
poweredByHeader: true,
compress: true,
onDemandEntries: {
Expand Down
2 changes: 1 addition & 1 deletion packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ export default async function getBaseWebpackConfig(
)
},
}),
isServerless && new ServerlessPlugin(),
isServerless && isServer && new ServerlessPlugin(),
isServer && new PagesManifestPlugin(isLikeServerless),
target === 'server' &&
isServer &&
Expand Down
6 changes: 0 additions & 6 deletions test/integration/serverless-now/next.config.js

This file was deleted.

7 changes: 0 additions & 7 deletions test/integration/serverless-now/pages/index.js

This file was deleted.

43 changes: 0 additions & 43 deletions test/integration/serverless-now/test/index.test.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default ({ query }, res) => {
res.json(query)
}
4 changes: 2 additions & 2 deletions test/integration/serverless-trace/pages/api/posts/[id].js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default (req, res) => {
res.json({ post: req.query.id })
export default ({ query }, res) => {
res.json(query)
}
48 changes: 44 additions & 4 deletions test/integration/serverless-trace/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* global jasmine */
import webdriver from 'next-webdriver'
import { join } from 'path'
import { existsSync } from 'fs'
import { existsSync, readdirSync, readFileSync } from 'fs'
import {
killApp,
findPort,
Expand All @@ -15,11 +15,13 @@ import fetch from 'node-fetch'

const appDir = join(__dirname, '../')
const serverlessDir = join(appDir, '.next/serverless/pages')
const chunksDir = join(appDir, '.next/static/chunks')
const buildIdFile = join(appDir, '.next/BUILD_ID')
let appPort
let app
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5

describe('Serverless', () => {
describe('Serverless Trace', () => {
beforeAll(async () => {
await nextBuild(appDir)
appPort = await findPort()
Expand Down Expand Up @@ -52,6 +54,11 @@ describe('Serverless', () => {
expect(html).toMatch(/This page could not be found/)
})

it('should render 404 for /_next/static', async () => {
const html = await renderViaHTTP(appPort, '/_next/static')
expect(html).toMatch(/This page could not be found/)
})

it('should render an AMP page', async () => {
const html = await renderViaHTTP(appPort, '/some-amp?amp=1')
expect(html).toMatch(/Hi Im an AMP page/)
Expand Down Expand Up @@ -93,6 +100,19 @@ describe('Serverless', () => {
}
})

it('should not have combined client-side chunks', () => {
expect(readdirSync(chunksDir).length).toBeGreaterThanOrEqual(2)
const buildId = readFileSync(buildIdFile, 'utf8').trim()

const pageContent = join(
appDir,
'.next/static',
buildId,
'pages/dynamic.js'
)
expect(readFileSync(pageContent, 'utf8')).not.toContain('Hello!')
})

it('should not output _app.js and _document.js to serverless build', () => {
expect(existsSync(join(serverlessDir, '_app.js'))).toBeFalsy()
expect(existsSync(join(serverlessDir, '_document.js'))).toBeFalsy()
Expand Down Expand Up @@ -121,8 +141,28 @@ describe('Serverless', () => {

it('should reply on dynamic API request successfully', async () => {
const result = await renderViaHTTP(appPort, '/api/posts/post-1')
const { post } = JSON.parse(result)
expect(post).toBe('post-1')
const { id } = JSON.parse(result)
expect(id).toBe('post-1')
})

it('should reply on dynamic API request successfully with query parameters', async () => {
const result = await renderViaHTTP(appPort, '/api/posts/post-1?param=val')
const { id, param } = JSON.parse(result)
expect(id).toBe('post-1')
expect(param).toBe('val')
})

it('should reply on dynamic API index request successfully', async () => {
const result = await renderViaHTTP(appPort, '/api/dynamic/post-1')
const { path } = JSON.parse(result)
expect(path).toBe('post-1')
})

it('should reply on dynamic API index request successfully with query parameters', async () => {
const result = await renderViaHTTP(appPort, '/api/dynamic/post-1?param=val')
const { path, param } = JSON.parse(result)
expect(path).toBe('post-1')
expect(param).toBe('val')
})

it('should 404 on API request with trailing slash', async () => {
Expand Down
17 changes: 16 additions & 1 deletion test/integration/serverless/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* global jasmine */
import webdriver from 'next-webdriver'
import { join } from 'path'
import { existsSync } from 'fs'
import { existsSync, readdirSync, readFileSync } from 'fs'
import {
killApp,
findPort,
Expand All @@ -15,6 +15,8 @@ import fetch from 'node-fetch'

const appDir = join(__dirname, '../')
const serverlessDir = join(appDir, '.next/serverless/pages')
const chunksDir = join(appDir, '.next/static/chunks')
const buildIdFile = join(appDir, '.next/BUILD_ID')
let appPort
let app
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5
Expand Down Expand Up @@ -98,6 +100,19 @@ describe('Serverless', () => {
}
})

it('should not have combined client-side chunks', () => {
expect(readdirSync(chunksDir).length).toBeGreaterThanOrEqual(2)
const buildId = readFileSync(buildIdFile, 'utf8').trim()

const pageContent = join(
appDir,
'.next/static',
buildId,
'pages/dynamic.js'
)
expect(readFileSync(pageContent, 'utf8')).not.toContain('Hello!')
})

it('should not output _app.js and _document.js to serverless build', () => {
expect(existsSync(join(serverlessDir, '_app.js'))).toBeFalsy()
expect(existsSync(join(serverlessDir, '_document.js'))).toBeFalsy()
Expand Down

0 comments on commit 67e7753

Please sign in to comment.