Unverified Commit 99aba86a authored by Sam Poder's avatar Sam Poder Committed by GitHub

[turbopack] Add an experimental `chunkingHeuristics` to `next.config.js` (#95019)

This PR adds new experimental options to `next.config.js`, they are: the
request cost, “clusters” of routes where should chunk more (because the
Z+Z case in `make_production_chunks` is more likely), “entry points”
where we prioritize larger merged chunks for fast initial page loads
(achieved by increasing P(N=1)), and a bounce rate to configure P(N=1)
and P(N=2) based on site data.
parent 3767dfae
......@@ -1027,6 +1027,25 @@ function bindingToApi(
nextConfigSerializable.turbopack = turbopack
}
// Serialize `experimental.turbopackChunkingHeuristics` route patterns: convert each RegExp to
// {source, flags} since RegExp objects are not JSON-serializable.
const chunkingHeuristics =
nextConfigSerializable.experimental?.turbopackChunkingHeuristics
if (chunkingHeuristics) {
const regexComponents = (regex: RegExp) => ({
source: regex.source,
flags: regex.flags,
})
nextConfigSerializable.experimental = {
...nextConfigSerializable.experimental,
turbopackChunkingHeuristics: {
...chunkingHeuristics,
priorityRoutes:
chunkingHeuristics.priorityRoutes?.map(regexComponents),
},
}
}
return JSON.stringify(nextConfigSerializable, null, 2)
}
......
......@@ -385,6 +385,14 @@ export const experimentalSchema = {
turbopackRemoveUnusedImports: z.boolean().optional(),
turbopackRemoveUnusedExports: z.boolean().optional(),
turbopackScopeHoisting: z.boolean().optional(),
turbopackChunkingHeuristics: z
.object({
firstPageLoadPriority: z.number().min(0).max(1).optional(),
priorityRoutes: z.array(z.instanceof(RegExp)).optional(),
priorityBoost: z.number().min(1).optional(),
requestCost: z.number().min(0).max(1_000_000).optional(),
})
.optional(),
turbopackWorkerAssetPrefix: z.string().optional(),
turbopackClientSideNestedAsyncChunking: z.boolean().optional(),
turbopackServerSideNestedAsyncChunking: z.boolean().optional(),
......
......@@ -764,6 +764,39 @@ export interface ExperimentalConfig {
*/
turbopackScopeHoisting?: boolean
/**
* (`next --turbopack` only) Traffic-related hints for the production chunker. These change the
* assumptions Turbopack makes when making chunk merging decisions.
*/
turbopackChunkingHeuristics?: {
/**
* This is a number between `0..1`, when higher, we weight the benefits of
* merging chunks for a signal page load higher. If you don't know a good
* number for this, your bounce rate is a good approximate for this value.
*/
firstPageLoadPriority?: number
/**
* Regular expressions matching routes that are often the first page
* visited and whose client-side bundles should be merged more eagerly to reduce the single-route
* request cost (e.g. the homepage). This is at the cost of extra requests on other pages.
*/
priorityRoutes?: RegExp[]
/**
* How much more eagerly to merge the client-side bundles of
* `priorityRoutes` routes, as a multiplier on their single-request probability (default
* `1.5`). Higher values merge more aggressively for those routes at the cost of extra requests
* elsewhere.
*/
priorityBoost?: number
/**
* Estimated cost of an additional request, in bytes (uncompressed
* and unminfified bytes of code, default is 200 KB and the max is 1 MB), used by the chunker to
* trade off request count against preventing double-fetching. Uncompressed and unminfified code
* is approximately 5x the size of compressed and minified code.
*/
requestCost?: number
}
/**
* (`next --turbopack` only) A custom URL prefix for Web Worker URLs
* produced by `new Worker(new URL(..., import.meta.url))` — both the
......
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