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 {
.await?,
chunking_min_chunk_size: turbopack_chunking.min_server_chunk_size,
chunking_max_merge_chunk_size: turbopack_chunking.max_merge_server_chunk_size,
chunking_max_chunk_size: self
.next_config()
.turbopack_chunking()
.await?
.max_server_chunk_size,
chunking_max_chunk_size: turbopack_chunking.max_server_chunk_size,
};
Ok(if client_assets {
get_server_chunking_context_with_client_assets(options)
......
......@@ -1592,8 +1592,8 @@ pub const SERVER_CHUNKING_CONFIG_OPTION_NAMES: ChunkingConfigOptionNames =
ChunkingConfigOptionNames {
chunks: "server",
max_chunk_size: "maxServerChunkSize",
min_chunk_size: None,
max_merge_chunk_size: None,
min_chunk_size: Some("minServerChunkSize"),
max_merge_chunk_size: Some("maxMergeServerChunkSize"),
};
/// [`ChunkingConfigOptionNames`] of edge server chunks.
......
......@@ -37,6 +37,8 @@ const nextConfig = {
maxChunkCountPerGroup: 40,
maxMergeChunkSize: 200000,
maxChunkSize: 1000000,
minServerChunkSize: 4000,
maxMergeServerChunkSize: 100000,
maxServerChunkSize: 10000000,
minComponentChunkSize: 20000,
generateComponentChunks: false,
......@@ -56,6 +58,8 @@ const nextConfig = {
maxChunkCountPerGroup: 40,
maxMergeChunkSize: 200000,
maxChunkSize: 1000000,
minServerChunkSize: 4000,
maxMergeServerChunkSize: 100000,
maxServerChunkSize: 10000000,
minComponentChunkSize: 20000,
generateComponentChunks: false,
......@@ -80,6 +84,8 @@ const nextConfig = {
maxChunkCountPerGroup: 40,
maxMergeChunkSize: 200000,
maxChunkSize: 1000000,
minServerChunkSize: 4000,
maxMergeServerChunkSize: 100000,
maxServerChunkSize: 10000000,
},
},
......@@ -97,6 +103,8 @@ const nextConfig = {
maxChunkCountPerGroup: 40,
maxMergeChunkSize: 200000,
maxChunkSize: 1000000,
minServerChunkSize: 4000,
maxMergeServerChunkSize: 100000,
maxServerChunkSize: 10000000,
},
},
......@@ -137,11 +145,6 @@ the size of the compressed, minified output).
Otherwise, Turbopack increases `maxChunkSize` or decreases
`maxMergeChunkSize` accordingly, and shows a warning. Set it to `0` to disable
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
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
## Server Chunks
The size thresholds above only apply to client-side JavaScript. Server-side JavaScript for the
Node.js runtime uses separate thresholds:
- **`minServerChunkSize`** (default `4000`): Turbopack will avoid creating more than one server
chunk smaller than this size by merging small chunks into larger ones.
- **`maxMergeServerChunkSize`** (default `100000`): Turbopack never merges a server chunk larger
than this size with other chunks.
These options don't apply to the Edge runtime, whose chunking is not configurable.
The size thresholds above only apply to client-side JavaScript. Server-side JavaScript uses
separate thresholds:
- **`minServerChunkSize`** (default `4000`): Turbopack will avoid creating more than one Node.js
server chunk smaller than this size by merging small chunks into larger ones, unless the
maximum chunk size takes precedence.
- **`maxMergeServerChunkSize`** (default `100000`): Turbopack never merges a Node.js server chunk
larger than this size with other chunks.
- **`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"
/** @type {import('next').NextConfig} */
......
......@@ -962,10 +962,11 @@ export interface ExperimentalConfig {
*/
maxChunkSize?: number
/**
* Like `maxChunkSize`, but for server (Node.js and edge) JavaScript chunks. Server chunks have
* fixed minimum chunk sizes. Node.js server chunks also have a fixed `maxMergeChunkSize`, while
* edge chunks use the effective maximum chunk size as their merge limit. Set to `0` to disable
* the limit. Defaults to `10000000` (10 MB).
* Like `maxChunkSize`, but for server (Node.js and edge) JavaScript chunks. For Node.js,
* must be at least 3 times `minServerChunkSize`, and not smaller than `maxMergeServerChunkSize`;
* otherwise the values are adjusted with a warning. Edge uses a fixed minimum of 20 KB and
* the effective maximum as its merge limit. Set to `0` to disable the maximum size limit.
* Defaults to `10000000` (10 MB).
*/
maxServerChunkSize?: number
/**
......@@ -981,8 +982,8 @@ export interface ExperimentalConfig {
minComponentChunkSize?: number
/**
* 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
* apply to Edge runtime chunks.
* Smaller chunks are merged into bigger ones to avoid that, unless `maxServerChunkSize` takes
* precedence. Defaults to `4000` (4 KB). Doesn't apply to Edge runtime chunks.
*/
minServerChunkSize?: number
/**
......
......@@ -21,22 +21,21 @@ import { join, relative } from 'path'
)
const { exitCode, cliOutput } = await next.build()
expect(exitCode).toBe(0)
return cliOutput
}
async function buildWith(turbopackChunking: object | undefined) {
await build(turbopackChunking)
const cliOutput = await build(turbopackChunking)
const distDir = join(next.testDir, '.next')
const clientChunks = (await listClientChunks(distDir)).filter(
(name) => name.includes('chunks') && name.endsWith('.js')
)
const serverChunks = (
await recursiveReadDir(join(distDir, 'server', 'chunks'))
).filter((name) => name.endsWith('.js'))
const serverChunks = await listServerChunks()
return { clientChunks, serverChunks, cliOutput }
}
// Lists the Node.js server JS chunks of the last build. The server size
// thresholds don't apply to edge chunks.
// Lists the Node.js server JS chunks of the last build. The configurable
// server minimum and merge thresholds don't apply to edge chunks.
async function listServerChunks() {
const serverDir = join(next.testDir, '.next', 'server')
return (await recursiveReadDir(serverDir, { relativePathnames: false }))
......@@ -64,10 +63,12 @@ import { join, relative } from 'path'
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 = {
minServerChunkSize: 100_000_000,
maxMergeServerChunkSize: 100_000_000,
maxServerChunkSize: 0,
}
type BuildOutput = Awaited<ReturnType<typeof buildWith>>
......@@ -86,8 +87,8 @@ import { join, relative } from 'path'
maxChunkSize: 20_000,
}
// Merge everything, but split server chunks bigger than a small maximum size. Node.js server
// chunks have a fixed `maxMergeChunkSize` of 100 KB, so a smaller maximum size would be adjusted.
// Merge everything on the client, but split server chunks bigger than a small maximum size.
// The default Node.js server merge threshold is 100 KB, so use a matching maximum.
const mergeAllWithMaxServerChunkSize = {
...mergeAll,
maxServerChunkSize: 100_000,
......@@ -222,6 +223,33 @@ import { join, relative } from 'path'
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 () => {
const { cliOutput } = await buildWith({
minChunkSize: 0,
......@@ -235,7 +263,7 @@ import { join, relative } from 'path'
})
it.each([
['custom chunking', mergeAll],
['custom chunking', { ...mergeAll, ...serverMergeAll }],
['maxChunkSize', mergeAllWithMaxChunkSize],
[
'maxChunkSize and component chunks',
......@@ -243,7 +271,7 @@ import { join, relative } from 'path'
],
['maxServerChunkSize', mergeAllWithMaxServerChunkSize],
])('renders and navigates correctly with %s', async (_, chunking) => {
await build({ ...chunking, ...serverMergeAll })
await build(chunking)
await next.start()
try {
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