Skip to content

Commit 67e7753

Browse files
authored
Fix serverless chunking (#8569)
* Fix Serverless Chunking * Remove old test & environment variable * Update serverless trace test case
1 parent 3405943 commit 67e7753

File tree

9 files changed

+67
-65
lines changed

9 files changed

+67
-65
lines changed

packages/next-server/server/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const defaultConfig: { [key: string]: any } = {
1818
generateBuildId: () => null,
1919
generateEtags: true,
2020
pageExtensions: ['tsx', 'ts', 'jsx', 'js'],
21-
target: process.env.__NEXT_BUILDER_EXPERIMENTAL_TARGET || 'server',
21+
target: 'server',
2222
poweredByHeader: true,
2323
compress: true,
2424
onDemandEntries: {

packages/next/build/webpack-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ export default async function getBaseWebpackConfig(
590590
)
591591
},
592592
}),
593-
isServerless && new ServerlessPlugin(),
593+
isServerless && isServer && new ServerlessPlugin(),
594594
isServer && new PagesManifestPlugin(isLikeServerless),
595595
target === 'server' &&
596596
isServer &&

test/integration/serverless-now/next.config.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

test/integration/serverless-now/pages/index.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

test/integration/serverless-now/test/index.test.js

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default ({ query }, res) => {
2+
res.json(query)
3+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export default (req, res) => {
2-
res.json({ post: req.query.id })
1+
export default ({ query }, res) => {
2+
res.json(query)
33
}

test/integration/serverless-trace/test/index.test.js

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/* global jasmine */
33
import webdriver from 'next-webdriver'
44
import { join } from 'path'
5-
import { existsSync } from 'fs'
5+
import { existsSync, readdirSync, readFileSync } from 'fs'
66
import {
77
killApp,
88
findPort,
@@ -15,11 +15,13 @@ import fetch from 'node-fetch'
1515

1616
const appDir = join(__dirname, '../')
1717
const serverlessDir = join(appDir, '.next/serverless/pages')
18+
const chunksDir = join(appDir, '.next/static/chunks')
19+
const buildIdFile = join(appDir, '.next/BUILD_ID')
1820
let appPort
1921
let app
2022
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5
2123

22-
describe('Serverless', () => {
24+
describe('Serverless Trace', () => {
2325
beforeAll(async () => {
2426
await nextBuild(appDir)
2527
appPort = await findPort()
@@ -52,6 +54,11 @@ describe('Serverless', () => {
5254
expect(html).toMatch(/This page could not be found/)
5355
})
5456

57+
it('should render 404 for /_next/static', async () => {
58+
const html = await renderViaHTTP(appPort, '/_next/static')
59+
expect(html).toMatch(/This page could not be found/)
60+
})
61+
5562
it('should render an AMP page', async () => {
5663
const html = await renderViaHTTP(appPort, '/some-amp?amp=1')
5764
expect(html).toMatch(/Hi Im an AMP page/)
@@ -93,6 +100,19 @@ describe('Serverless', () => {
93100
}
94101
})
95102

103+
it('should not have combined client-side chunks', () => {
104+
expect(readdirSync(chunksDir).length).toBeGreaterThanOrEqual(2)
105+
const buildId = readFileSync(buildIdFile, 'utf8').trim()
106+
107+
const pageContent = join(
108+
appDir,
109+
'.next/static',
110+
buildId,
111+
'pages/dynamic.js'
112+
)
113+
expect(readFileSync(pageContent, 'utf8')).not.toContain('Hello!')
114+
})
115+
96116
it('should not output _app.js and _document.js to serverless build', () => {
97117
expect(existsSync(join(serverlessDir, '_app.js'))).toBeFalsy()
98118
expect(existsSync(join(serverlessDir, '_document.js'))).toBeFalsy()
@@ -121,8 +141,28 @@ describe('Serverless', () => {
121141

122142
it('should reply on dynamic API request successfully', async () => {
123143
const result = await renderViaHTTP(appPort, '/api/posts/post-1')
124-
const { post } = JSON.parse(result)
125-
expect(post).toBe('post-1')
144+
const { id } = JSON.parse(result)
145+
expect(id).toBe('post-1')
146+
})
147+
148+
it('should reply on dynamic API request successfully with query parameters', async () => {
149+
const result = await renderViaHTTP(appPort, '/api/posts/post-1?param=val')
150+
const { id, param } = JSON.parse(result)
151+
expect(id).toBe('post-1')
152+
expect(param).toBe('val')
153+
})
154+
155+
it('should reply on dynamic API index request successfully', async () => {
156+
const result = await renderViaHTTP(appPort, '/api/dynamic/post-1')
157+
const { path } = JSON.parse(result)
158+
expect(path).toBe('post-1')
159+
})
160+
161+
it('should reply on dynamic API index request successfully with query parameters', async () => {
162+
const result = await renderViaHTTP(appPort, '/api/dynamic/post-1?param=val')
163+
const { path, param } = JSON.parse(result)
164+
expect(path).toBe('post-1')
165+
expect(param).toBe('val')
126166
})
127167

128168
it('should 404 on API request with trailing slash', async () => {

test/integration/serverless/test/index.test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/* global jasmine */
33
import webdriver from 'next-webdriver'
44
import { join } from 'path'
5-
import { existsSync } from 'fs'
5+
import { existsSync, readdirSync, readFileSync } from 'fs'
66
import {
77
killApp,
88
findPort,
@@ -15,6 +15,8 @@ import fetch from 'node-fetch'
1515

1616
const appDir = join(__dirname, '../')
1717
const serverlessDir = join(appDir, '.next/serverless/pages')
18+
const chunksDir = join(appDir, '.next/static/chunks')
19+
const buildIdFile = join(appDir, '.next/BUILD_ID')
1820
let appPort
1921
let app
2022
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5
@@ -98,6 +100,19 @@ describe('Serverless', () => {
98100
}
99101
})
100102

103+
it('should not have combined client-side chunks', () => {
104+
expect(readdirSync(chunksDir).length).toBeGreaterThanOrEqual(2)
105+
const buildId = readFileSync(buildIdFile, 'utf8').trim()
106+
107+
const pageContent = join(
108+
appDir,
109+
'.next/static',
110+
buildId,
111+
'pages/dynamic.js'
112+
)
113+
expect(readFileSync(pageContent, 'utf8')).not.toContain('Hello!')
114+
})
115+
101116
it('should not output _app.js and _document.js to serverless build', () => {
102117
expect(existsSync(join(serverlessDir, '_app.js'))).toBeFalsy()
103118
expect(existsSync(join(serverlessDir, '_document.js'))).toBeFalsy()

0 commit comments

Comments
 (0)