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