Unverified Commit 01128c0b authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

Compose maximum chunk sizes with server threshold options

Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 222041fc
...@@ -1841,11 +1841,7 @@ impl Project { ...@@ -1841,11 +1841,7 @@ impl Project {
.await?, .await?,
chunking_min_chunk_size: turbopack_chunking.min_server_chunk_size, chunking_min_chunk_size: turbopack_chunking.min_server_chunk_size,
chunking_max_merge_chunk_size: turbopack_chunking.max_merge_server_chunk_size, chunking_max_merge_chunk_size: turbopack_chunking.max_merge_server_chunk_size,
chunking_max_chunk_size: self chunking_max_chunk_size: turbopack_chunking.max_server_chunk_size,
.next_config()
.turbopack_chunking()
.await?
.max_server_chunk_size,
}; };
Ok(if client_assets { Ok(if client_assets {
get_server_chunking_context_with_client_assets(options) get_server_chunking_context_with_client_assets(options)
......
...@@ -1592,8 +1592,8 @@ pub const SERVER_CHUNKING_CONFIG_OPTION_NAMES: ChunkingConfigOptionNames = ...@@ -1592,8 +1592,8 @@ pub const SERVER_CHUNKING_CONFIG_OPTION_NAMES: ChunkingConfigOptionNames =
ChunkingConfigOptionNames { ChunkingConfigOptionNames {
chunks: "server", chunks: "server",
max_chunk_size: "maxServerChunkSize", max_chunk_size: "maxServerChunkSize",
min_chunk_size: None, min_chunk_size: Some("minServerChunkSize"),
max_merge_chunk_size: None, max_merge_chunk_size: Some("maxMergeServerChunkSize"),
}; };
/// [`ChunkingConfigOptionNames`] of edge server chunks. /// [`ChunkingConfigOptionNames`] of edge server chunks.
......
...@@ -37,6 +37,8 @@ const nextConfig = { ...@@ -37,6 +37,8 @@ const nextConfig = {
maxChunkCountPerGroup: 40, maxChunkCountPerGroup: 40,
maxMergeChunkSize: 200000, maxMergeChunkSize: 200000,
maxChunkSize: 1000000, maxChunkSize: 1000000,
minServerChunkSize: 4000,
maxMergeServerChunkSize: 100000,
maxServerChunkSize: 10000000, maxServerChunkSize: 10000000,
minComponentChunkSize: 20000, minComponentChunkSize: 20000,
generateComponentChunks: false, generateComponentChunks: false,
...@@ -56,6 +58,8 @@ const nextConfig = { ...@@ -56,6 +58,8 @@ const nextConfig = {
maxChunkCountPerGroup: 40, maxChunkCountPerGroup: 40,
maxMergeChunkSize: 200000, maxMergeChunkSize: 200000,
maxChunkSize: 1000000, maxChunkSize: 1000000,
minServerChunkSize: 4000,
maxMergeServerChunkSize: 100000,
maxServerChunkSize: 10000000, maxServerChunkSize: 10000000,
minComponentChunkSize: 20000, minComponentChunkSize: 20000,
generateComponentChunks: false, generateComponentChunks: false,
...@@ -80,6 +84,8 @@ const nextConfig = { ...@@ -80,6 +84,8 @@ const nextConfig = {
maxChunkCountPerGroup: 40, maxChunkCountPerGroup: 40,
maxMergeChunkSize: 200000, maxMergeChunkSize: 200000,
maxChunkSize: 1000000, maxChunkSize: 1000000,
minServerChunkSize: 4000,
maxMergeServerChunkSize: 100000,
maxServerChunkSize: 10000000, maxServerChunkSize: 10000000,
}, },
}, },
...@@ -97,6 +103,8 @@ const nextConfig = { ...@@ -97,6 +103,8 @@ const nextConfig = {
maxChunkCountPerGroup: 40, maxChunkCountPerGroup: 40,
maxMergeChunkSize: 200000, maxMergeChunkSize: 200000,
maxChunkSize: 1000000, maxChunkSize: 1000000,
minServerChunkSize: 4000,
maxMergeServerChunkSize: 100000,
maxServerChunkSize: 10000000, maxServerChunkSize: 10000000,
}, },
}, },
...@@ -137,11 +145,6 @@ the size of the compressed, minified output). ...@@ -137,11 +145,6 @@ the size of the compressed, minified output).
Otherwise, Turbopack increases `maxChunkSize` or decreases Otherwise, Turbopack increases `maxChunkSize` or decreases
`maxMergeChunkSize` accordingly, and shows a warning. Set it to `0` to disable `maxMergeChunkSize` accordingly, and shows a warning. Set it to `0` to disable
the limit. the limit.
- **`maxServerChunkSize`** (default `10000000`): like `maxChunkSize`, but for
server (Node.js and edge) JavaScript chunks instead of client chunks. Server
chunks use a fixed minimum chunk size (4 KB for Node.js, 20 KB for edge) and
a fixed `maxMergeChunkSize` of 100 KB for Node.js, which are adjusted the same
way. Set it to `0` to disable the limit.
When merging chunks, the trade-off being made is an improvement to performance on initial page When merging chunks, the trade-off being made is an improvement to performance on initial page
loads at the cost of navigation performance. This is because each additional network request loads at the cost of navigation performance. This is because each additional network request
...@@ -149,15 +152,23 @@ being made is costly, however, smaller chunks are more likely to be re-usable ac ...@@ -149,15 +152,23 @@ being made is costly, however, smaller chunks are more likely to be re-usable ac
## Server Chunks ## Server Chunks
The size thresholds above only apply to client-side JavaScript. Server-side JavaScript for the The size thresholds above only apply to client-side JavaScript. Server-side JavaScript uses
Node.js runtime uses separate thresholds: separate thresholds:
- **`minServerChunkSize`** (default `4000`): Turbopack will avoid creating more than one server - **`minServerChunkSize`** (default `4000`): Turbopack will avoid creating more than one Node.js
chunk smaller than this size by merging small chunks into larger ones. server chunk smaller than this size by merging small chunks into larger ones, unless the
- **`maxMergeServerChunkSize`** (default `100000`): Turbopack never merges a server chunk larger maximum chunk size takes precedence.
than this size with other chunks. - **`maxMergeServerChunkSize`** (default `100000`): Turbopack never merges a Node.js server chunk
larger than this size with other chunks.
These options don't apply to the Edge runtime, whose chunking is not configurable. - **`maxServerChunkSize`** (default `10000000`): like `maxChunkSize`, but for server (Node.js and
edge) JavaScript chunks instead of client chunks. For Node.js, it must be at least 3 times
`minServerChunkSize`, and `maxMergeServerChunkSize` must not be bigger. Otherwise, Turbopack
increases `maxServerChunkSize` or decreases `maxMergeServerChunkSize` accordingly, and shows a
warning. Set it to `0` to disable the maximum size limit.
`minServerChunkSize` and `maxMergeServerChunkSize` don't apply to the Edge runtime. Edge uses a
fixed minimum of 20 KB and the effective `maxServerChunkSize` as its merge limit. If its maximum
is enabled but below 60 KB (3 times the minimum), it is increased with a warning.
```js filename="next.config.js" ```js filename="next.config.js"
/** @type {import('next').NextConfig} */ /** @type {import('next').NextConfig} */
......
...@@ -962,10 +962,11 @@ export interface ExperimentalConfig { ...@@ -962,10 +962,11 @@ export interface ExperimentalConfig {
*/ */
maxChunkSize?: number maxChunkSize?: number
/** /**
* Like `maxChunkSize`, but for server (Node.js and edge) JavaScript chunks. Server chunks have * Like `maxChunkSize`, but for server (Node.js and edge) JavaScript chunks. For Node.js,
* fixed minimum chunk sizes. Node.js server chunks also have a fixed `maxMergeChunkSize`, while * must be at least 3 times `minServerChunkSize`, and not smaller than `maxMergeServerChunkSize`;
* edge chunks use the effective maximum chunk size as their merge limit. Set to `0` to disable * otherwise the values are adjusted with a warning. Edge uses a fixed minimum of 20 KB and
* the limit. Defaults to `10000000` (10 MB). * the effective maximum as its merge limit. Set to `0` to disable the maximum size limit.
* Defaults to `10000000` (10 MB).
*/ */
maxServerChunkSize?: number maxServerChunkSize?: number
/** /**
...@@ -981,8 +982,8 @@ export interface ExperimentalConfig { ...@@ -981,8 +982,8 @@ export interface ExperimentalConfig {
minComponentChunkSize?: number minComponentChunkSize?: number
/** /**
* Avoid creating more than one Node.js server chunk smaller than this size, in bytes. * Avoid creating more than one Node.js server chunk smaller than this size, in bytes.
* Smaller chunks are merged into bigger ones to avoid that. Defaults to `4000` (4 KB). Doesn't * Smaller chunks are merged into bigger ones to avoid that, unless `maxServerChunkSize` takes
* apply to Edge runtime chunks. * precedence. Defaults to `4000` (4 KB). Doesn't apply to Edge runtime chunks.
*/ */
minServerChunkSize?: number minServerChunkSize?: number
/** /**
......
...@@ -21,22 +21,21 @@ import { join, relative } from 'path' ...@@ -21,22 +21,21 @@ import { join, relative } from 'path'
) )
const { exitCode, cliOutput } = await next.build() const { exitCode, cliOutput } = await next.build()
expect(exitCode).toBe(0) expect(exitCode).toBe(0)
return cliOutput
} }
async function buildWith(turbopackChunking: object | undefined) { async function buildWith(turbopackChunking: object | undefined) {
await build(turbopackChunking) const cliOutput = await build(turbopackChunking)
const distDir = join(next.testDir, '.next') const distDir = join(next.testDir, '.next')
const clientChunks = (await listClientChunks(distDir)).filter( const clientChunks = (await listClientChunks(distDir)).filter(
(name) => name.includes('chunks') && name.endsWith('.js') (name) => name.includes('chunks') && name.endsWith('.js')
) )
const serverChunks = ( const serverChunks = await listServerChunks()
await recursiveReadDir(join(distDir, 'server', 'chunks'))
).filter((name) => name.endsWith('.js'))
return { clientChunks, serverChunks, cliOutput } return { clientChunks, serverChunks, cliOutput }
} }
// Lists the Node.js server JS chunks of the last build. The server size // Lists the Node.js server JS chunks of the last build. The configurable
// thresholds don't apply to edge chunks. // server minimum and merge thresholds don't apply to edge chunks.
async function listServerChunks() { async function listServerChunks() {
const serverDir = join(next.testDir, '.next', 'server') const serverDir = join(next.testDir, '.next', 'server')
return (await recursiveReadDir(serverDir, { relativePathnames: false })) return (await recursiveReadDir(serverDir, { relativePathnames: false }))
...@@ -64,10 +63,12 @@ import { join, relative } from 'path' ...@@ -64,10 +63,12 @@ import { join, relative } from 'path'
maxChunkSize: 0, maxChunkSize: 0,
} }
// Server-side equivalent of `mergeAll`. // Server-side equivalent of `mergeAll`, with the maximum disabled so the
// large minimum doesn't raise it or produce a normalization warning.
const serverMergeAll = { const serverMergeAll = {
minServerChunkSize: 100_000_000, minServerChunkSize: 100_000_000,
maxMergeServerChunkSize: 100_000_000, maxMergeServerChunkSize: 100_000_000,
maxServerChunkSize: 0,
} }
type BuildOutput = Awaited<ReturnType<typeof buildWith>> type BuildOutput = Awaited<ReturnType<typeof buildWith>>
...@@ -86,8 +87,8 @@ import { join, relative } from 'path' ...@@ -86,8 +87,8 @@ import { join, relative } from 'path'
maxChunkSize: 20_000, maxChunkSize: 20_000,
} }
// Merge everything, but split server chunks bigger than a small maximum size. Node.js server // Merge everything on the client, but split server chunks bigger than a small maximum size.
// chunks have a fixed `maxMergeChunkSize` of 100 KB, so a smaller maximum size would be adjusted. // The default Node.js server merge threshold is 100 KB, so use a matching maximum.
const mergeAllWithMaxServerChunkSize = { const mergeAllWithMaxServerChunkSize = {
...mergeAll, ...mergeAll,
maxServerChunkSize: 100_000, maxServerChunkSize: 100_000,
...@@ -222,6 +223,33 @@ import { join, relative } from 'path' ...@@ -222,6 +223,33 @@ import { join, relative } from 'path'
expect(clientChunks.length).toBeGreaterThan(0) expect(clientChunks.length).toBeGreaterThan(0)
}) })
it.each([100_000, 0])(
'normalizes configured server size thresholds with maxMergeServerChunkSize=%s',
async (maxMergeServerChunkSize) => {
const { cliOutput, serverChunks } = await buildWith({
minServerChunkSize: 10_000,
maxMergeServerChunkSize,
maxServerChunkSize: 20_000,
})
expect(cliOutput).toContain(
'Inconsistent chunk size limits for server chunks were adjusted'
)
expect(cliOutput).toContain(
'`experimental.turbopackChunking.maxServerChunkSize` (20000) was increased to 30000'
)
expect(cliOutput).toContain(
'3 times `experimental.turbopackChunking.minServerChunkSize` of 10000'
)
expect(cliOutput).toContain(
`\`experimental.turbopackChunking.maxMergeServerChunkSize\` (${maxMergeServerChunkSize || 'unlimited'}) was decreased to 30000`
)
expect(cliOutput).not.toContain(
'Inconsistent chunk size limits for client chunks'
)
expect(serverChunks.length).toBeGreaterThan(0)
}
)
it('warns when an enabled maxChunkSize limits an unlimited maxMergeChunkSize', async () => { it('warns when an enabled maxChunkSize limits an unlimited maxMergeChunkSize', async () => {
const { cliOutput } = await buildWith({ const { cliOutput } = await buildWith({
minChunkSize: 0, minChunkSize: 0,
...@@ -235,7 +263,7 @@ import { join, relative } from 'path' ...@@ -235,7 +263,7 @@ import { join, relative } from 'path'
}) })
it.each([ it.each([
['custom chunking', mergeAll], ['custom chunking', { ...mergeAll, ...serverMergeAll }],
['maxChunkSize', mergeAllWithMaxChunkSize], ['maxChunkSize', mergeAllWithMaxChunkSize],
[ [
'maxChunkSize and component chunks', 'maxChunkSize and component chunks',
...@@ -243,7 +271,7 @@ import { join, relative } from 'path' ...@@ -243,7 +271,7 @@ import { join, relative } from 'path'
], ],
['maxServerChunkSize', mergeAllWithMaxServerChunkSize], ['maxServerChunkSize', mergeAllWithMaxServerChunkSize],
])('renders and navigates correctly with %s', async (_, chunking) => { ])('renders and navigates correctly with %s', async (_, chunking) => {
await build({ ...chunking, ...serverMergeAll }) await build(chunking)
await next.start() await next.start()
try { try {
const browser = await next.browser('/') const browser = await next.browser('/')
......
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