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

Add maxServerChunkSize with a 10MB default for server and edge chunks

Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 6e512bfc
......@@ -1845,7 +1845,7 @@ impl Project {
.next_config()
.turbopack_chunking()
.await?
.max_chunk_size,
.max_server_chunk_size,
};
Ok(if client_assets {
get_server_chunking_context_with_client_assets(options)
......@@ -1891,7 +1891,7 @@ impl Project {
.next_config()
.turbopack_chunking()
.await?
.max_chunk_size,
.max_server_chunk_size,
};
Ok(if client_assets {
get_edge_chunking_context_with_client_assets(options)
......
......@@ -927,8 +927,11 @@ const DEFAULT_REQUEST_COST: f32 = 20_000.0;
/// Default `weightDistribution` for the graph algorithm.
const DEFAULT_WEIGHT_DISTRIBUTION: f32 = 0.1;
/// Default `experimental.turbopackChunking.maxChunkSize`, in bytes.
/// Default `experimental.turbopackChunking.maxChunkSize` (client JS), in bytes.
const DEFAULT_MAX_CHUNK_SIZE: usize = 1_000_000;
/// Default `experimental.turbopackChunking.maxServerChunkSize` (Node.js server and edge JS), in
/// bytes.
const DEFAULT_MAX_SERVER_CHUNK_SIZE: usize = 10_000_000;
/// `experimental.turbopackChunking`: hints for Turbopack's production chunker.
#[derive(
......@@ -962,12 +965,15 @@ pub struct TurbopackChunkingConfig {
/// Don't merge chunks bigger than this size, in bytes (default `200000`), with other
/// chunks. This keeps code in big chunks from being duplicated across multiple chunks.
max_merge_chunk_size: Option<usize>,
/// Best-effort maximum estimated size of a JavaScript chunk, in bytes of uncompressed,
/// unminified code (default `1000000`). Bigger chunks are split deterministically into
/// smaller chunks, which may be smaller than `min_chunk_size` or exceed
/// `max_chunk_count_per_group`. Split merged chunks do not emit component chunks. `0` disables
/// splitting. Unlike the other size thresholds, this also applies to server and edge chunks.
/// Best-effort maximum estimated size of a client JavaScript chunk, in bytes of
/// uncompressed, unminified code (default `1000000`). Bigger chunks are split
/// deterministically into smaller chunks, which may be smaller than `min_chunk_size` or
/// exceed `max_chunk_count_per_group`. Split merged chunks do not emit component chunks. `0`
/// disables splitting.
max_chunk_size: Option<usize>,
/// Like `max_chunk_size`, but for Node.js server and edge JavaScript chunks (default
/// `10000000`). `0` disables splitting.
max_server_chunk_size: Option<usize>,
/// Emit each merged production chunk's constituent component chunks alongside it, so the
/// browser runtime can load only the chunks it doesn't already have (default `false`).
generate_component_chunks: Option<bool>,
......@@ -1001,8 +1007,10 @@ pub struct TurbopackChunking {
pub max_chunk_count_per_group: Option<usize>,
/// Override for the client JS `max_merge_chunk_size`, or `None` to use the default.
pub max_merge_chunk_size: Option<usize>,
/// Best-effort maximum JS chunk size for client, server and edge chunking (defaults applied).
/// Best-effort maximum client JS chunk size (default applied).
pub max_chunk_size: usize,
/// Best-effort maximum Node.js server and edge JS chunk size (default applied).
pub max_server_chunk_size: usize,
/// Override for the client JS `min_component_chunk_size`, or `None` to use the default.
pub min_component_chunk_size: Option<usize>,
/// Override for the Node.js server JS `min_chunk_size`, or `None` to use the default.
......@@ -1997,6 +2005,9 @@ impl NextConfig {
max_chunk_size: config
.and_then(|c| c.max_chunk_size)
.unwrap_or(DEFAULT_MAX_CHUNK_SIZE),
max_server_chunk_size: config
.and_then(|c| c.max_server_chunk_size)
.unwrap_or(DEFAULT_MAX_SERVER_CHUNK_SIZE),
min_component_chunk_size: config.and_then(|c| c.min_component_chunk_size),
min_server_chunk_size: config.and_then(|c| c.min_server_chunk_size),
max_merge_server_chunk_size: config.and_then(|c| c.max_merge_server_chunk_size),
......
......@@ -37,6 +37,7 @@ const nextConfig = {
maxChunkCountPerGroup: 40,
maxMergeChunkSize: 200000,
maxChunkSize: 1000000,
maxServerChunkSize: 10000000,
minComponentChunkSize: 20000,
generateComponentChunks: false,
},
......@@ -55,6 +56,7 @@ const nextConfig = {
maxChunkCountPerGroup: 40,
maxMergeChunkSize: 200000,
maxChunkSize: 1000000,
maxServerChunkSize: 10000000,
minComponentChunkSize: 20000,
generateComponentChunks: false,
},
......@@ -78,6 +80,7 @@ const nextConfig = {
maxChunkCountPerGroup: 40,
maxMergeChunkSize: 200000,
maxChunkSize: 1000000,
maxServerChunkSize: 10000000,
},
},
} satisfies NextConfig
......@@ -94,6 +97,7 @@ const nextConfig = {
maxChunkCountPerGroup: 40,
maxMergeChunkSize: 200000,
maxChunkSize: 1000000,
maxServerChunkSize: 10000000,
},
},
}
......@@ -122,14 +126,16 @@ the size of the compressed, minified output).
larger than this size with other chunks. This keeps the code in large chunks
from being duplicated across multiple large output chunks.
- **`maxChunkSize`** (default `1000000`): a best-effort maximum estimated
chunk size. After merging, Turbopack splits JavaScript chunks bigger than
this into smaller chunks. It sorts their modules by identifier and prefers
chunk size. After merging, Turbopack splits client JavaScript chunks bigger
than this into smaller chunks. It sorts their modules by identifier and prefers
splitting where paths differ most (e.g. between two packages), which keeps
splits deterministic and helps avoid unrelated chunk changes. To stay under
the limit, it may create chunks smaller than `minChunkSize` or more chunks
than `maxChunkCountPerGroup`. A single module bigger than this size still
produces a bigger chunk. Unlike the other thresholds, this also applies to
server and edge chunks. Set it to `0` to disable splitting.
produces a bigger chunk. Set it to `0` to disable splitting.
- **`maxServerChunkSize`** (default `10000000`): like `maxChunkSize`, but for
server (Node.js and edge) JavaScript chunks instead of client chunks. Set it
to `0` to disable splitting.
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
......
......@@ -442,6 +442,7 @@ export const experimentalSchema = {
maxChunkCountPerGroup: z.number().min(0).optional(),
maxMergeChunkSize: z.number().min(0).optional(),
maxChunkSize: z.number().min(0).optional(),
maxServerChunkSize: z.number().min(0).optional(),
minComponentChunkSize: z.number().min(0).optional(),
generateComponentChunks: z.boolean().optional(),
minServerChunkSize: z.number().min(0).optional(),
......
......@@ -953,14 +953,18 @@ export interface ExperimentalConfig {
*/
maxMergeChunkSize?: number
/**
* Best-effort maximum estimated size of a JavaScript chunk, in bytes of uncompressed,
* Best-effort maximum estimated size of a client JavaScript chunk, in bytes of uncompressed,
* unminified code. Bigger chunks are split deterministically (by module identifier) into
* smaller chunks. Splitting can create chunks smaller than `minChunkSize` or exceed
* `maxChunkCountPerGroup`. Split merged chunks do not emit component chunks. Unlike the other
* size thresholds, this also applies to server and edge chunks. Set to `0` to disable
* splitting. Defaults to `1000000` (1 MB).
* `maxChunkCountPerGroup`. Split merged chunks do not emit component chunks. Set to `0` to
* disable splitting. Defaults to `1000000` (1 MB).
*/
maxChunkSize?: number
/**
* Like `maxChunkSize`, but for server (Node.js and edge) JavaScript chunks. Set to `0` to
* disable splitting. Defaults to `10000000` (10 MB).
*/
maxServerChunkSize?: number
/**
* Emit each merged production chunk's constituent component chunks alongside it, so the
* browser runtime can load only the ones it doesn't already have. Defaults to `false`.
......
......@@ -25,10 +25,14 @@ import { join, relative } from 'path'
async function buildWith(turbopackChunking: object | undefined) {
await build(turbopackChunking)
const chunks = await listClientChunks(join(next.testDir, '.next'))
return chunks.filter(
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'))
return { clientChunks, serverChunks }
}
// Lists the Node.js server JS chunks of the last build. The server size
......@@ -61,32 +65,45 @@ import { join, relative } from 'path'
maxMergeServerChunkSize: 100_000_000,
}
let mergeAllChunks: string[]
let noMergeChunks: string[]
let mergeAllWithComponentChunks: string[]
let mergeAllWithMaxChunkSizeChunks: string[]
type BuildOutput = Awaited<ReturnType<typeof buildWith>>
let mergeAllOutput: BuildOutput
let noMergeOutput: BuildOutput
let mergeAllWithComponentChunksOutput: BuildOutput
let mergeAllWithMaxChunkSizeOutput: BuildOutput
let mergeAllWithMaxServerChunkSizeOutput: BuildOutput
// Merge everything, but split chunks bigger than a small maximum size again.
// Merge everything, but split client chunks bigger than a small maximum size again.
const mergeAllWithMaxChunkSize = {
...mergeAll,
maxChunkSize: 20_000,
}
// Merge everything, but split server chunks bigger than a small maximum size again.
const mergeAllWithMaxServerChunkSize = {
...mergeAll,
maxServerChunkSize: 20_000,
}
beforeAll(async () => {
mergeAllChunks = await buildWith(mergeAll)
noMergeChunks = await buildWith(noMerge)
mergeAllWithComponentChunks = await buildWith({
mergeAllOutput = await buildWith(mergeAll)
noMergeOutput = await buildWith(noMerge)
mergeAllWithComponentChunksOutput = await buildWith({
...mergeAll,
generateComponentChunks: true,
minComponentChunkSize: 0,
})
mergeAllWithMaxChunkSizeChunks = await buildWith(mergeAllWithMaxChunkSize)
mergeAllWithMaxChunkSizeOutput = await buildWith(mergeAllWithMaxChunkSize)
mergeAllWithMaxServerChunkSizeOutput = await buildWith(
mergeAllWithMaxServerChunkSize
)
})
it('changes the client chunk output based on the size thresholds', () => {
// Disabling merging produces strictly more client chunks than merging
// everything, proving the size thresholds are wired through.
expect(noMergeChunks.length).toBeGreaterThan(mergeAllChunks.length)
expect(noMergeOutput.clientChunks.length).toBeGreaterThan(
mergeAllOutput.clientChunks.length
)
})
describe('server size thresholds', () => {
......@@ -132,22 +149,37 @@ import { join, relative } from 'path'
it('emits additional component chunks when generateComponentChunks is enabled', () => {
// With the same merge settings, enabling component chunks emits extra
// per-component chunks alongside the merged chunks.
expect(mergeAllWithComponentChunks.length).toBeGreaterThan(
mergeAllChunks.length
)
expect(
mergeAllWithComponentChunksOutput.clientChunks.length
).toBeGreaterThan(mergeAllOutput.clientChunks.length)
})
it('splits chunks bigger than maxChunkSize', () => {
it('splits client chunks bigger than maxChunkSize', () => {
// With the same merge settings, a small maxChunkSize splits the big merged chunks into
// more, smaller chunks.
expect(mergeAllWithMaxChunkSizeChunks.length).toBeGreaterThan(
mergeAllChunks.length
expect(
mergeAllWithMaxChunkSizeOutput.clientChunks.length
).toBeGreaterThan(mergeAllOutput.clientChunks.length)
// maxChunkSize only applies to client chunks.
expect(mergeAllWithMaxChunkSizeOutput.serverChunks).toEqual(
mergeAllOutput.serverChunks
)
})
it('splits server chunks bigger than maxServerChunkSize', () => {
expect(
mergeAllWithMaxServerChunkSizeOutput.serverChunks.length
).toBeGreaterThan(mergeAllOutput.serverChunks.length)
// maxServerChunkSize only applies to server chunks.
expect(mergeAllWithMaxServerChunkSizeOutput.clientChunks).toEqual(
mergeAllOutput.clientChunks
)
})
it.each([
['custom chunking', mergeAll],
['maxChunkSize', mergeAllWithMaxChunkSize],
['maxServerChunkSize', mergeAllWithMaxServerChunkSize],
])('renders and navigates correctly with %s', async (_, chunking) => {
await build({ ...chunking, ...serverMergeAll })
await next.start()
......
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