Repository navigation
perf(pages-router): load Next.js-like client chunks - #4726
Draft
james-elicx wants to merge 1 commit into
Draft
james-elicx wants to merge 1 commit into
james-elicx wants to merge 1 commit into
Conversation
@vinext/cloudflare
create-vinext-app
@vinext/types
vinext
@cloudflare/workers-response-store
commit: |
Contributor
Performance benchmarksCompared 0 improved · 0 regressed · 7 within ±1.5%
View detailed results and traces 🟢 improvement · 🔴 regression · ⚫ change below 1.5% · paired base/head |
Contributor
|
james-elicx
force-pushed
the
perf/pages-router-client-chunks
branch
4 times, most recently
from
November 16, 2026 17:28
f1d49e3 to
17c6cc1
Compare
Pages Router production builds loaded 14-23 JS files per route on the pages-router-complex fixture, against 5-6 for Next.js webpack and 10 for Turbopack. Two causes: - `_app`'s imports split into many chunks: a module that `_app` and some pages both import got a shared chunk of its own, though every page loads `_app`. vinext:pages-client-chunks now puts `_app` and its static imports in one "pages-app" chunk, as Next.js keeps `pages/_app` unsplit. - Small modules shared by different sets of pages each got a tiny common chunk. With Rolldown 1.2.18+, small common chunks (under 20 kB, Next.js' webpack `minSize`) are now copied into their importers through `codeSplitting.experimentalInlineCommonChunks`. Each module still evaluates once and only when imported, as in webpack and Turbopack. Stylesheets keep their own chunk, as Turbopack keeps a shared CSS module in one stylesheet. `disableNextLikeChunking` turns both off. Hybrid App + Pages builds keep their current chunking. The pages-router-complex fixture gains workbench desks that produce a varied client chunk graph (shared panels, a 36 kB shared table, local vendor packages, next/dynamic, React.lazy, on-demand imports, next/image, next/script, next/form, next/navigation) with e2e specs, including one that checks a module copied into several page chunks evaluates once.
james-elicx
force-pushed
the
perf/pages-router-client-chunks
branch
from
November 16, 2026 17:42
17c6cc1 to
b371b8e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pages Router production builds load many more client JS files than Next.js does. On
examples/pages-router-complexa desk page loaded 22–23 files, against 5–6 for Next.js webpack and 10 for Turbopack. There were two causes._app's imports were split into many chunks. A module that_appand some pages both import (next/link's helpers,next/script,next/dynamic, app components) got a shared chunk of its own, even though every page loads_app. That added up to 8 tiny files on every route.vinext:pages-client-chunksnow puts_app's static imports, and what Rolldown finds they use, in onepages-appchunk, short of theframeworkandvinextchunks. Next.js keepspages/_appunsplit for the same reason. Nothing is over-fetched, since every page loads_appanyway._appitself keeps its own chunk, because the client entry imports it and the Pages manifests (CSS order, preloads) look it up by source path. The group starts from_app's direct imports and leaves the rest to Rolldown, so a page-only export of a side-effect-free barrel that_appalso imports stays out of the chunk every page loads. An import that statically leads back to_app, to a page or next/dynamic target, or to a module the client entry loads (instrumentation-client's imports, short of the runtime helpers vinext's first group takes) stays in_app's chunk. Rolldown would pull that module into the group too, and in the last case the entry would then load the whole group before_app.The
pages-appchunk holds_app's stylesheets too, in import order. The SSR-manifest backfill now also lists the stylesheets of the chunks a chunk statically imports, ahead of the chunk's own, so a server bundle without the CSS graph (server built before the client) still links_app's CSS.Small modules shared by different sets of pages each got a tiny common chunk. webpack and Turbopack copy small shared modules into each page instead. With Rolldown 1.2.18+, vinext now turns on
codeSplitting.experimentalInlineCommonChunksat 20 kB, Next.js' webpacksplitChunks.minSize. Rolldown keeps one instance of each copied module and evaluates it only when an importer needs it, so module evaluation stays the same as in Next.js. Stylesheets are excluded, so a shared CSS module keeps one stylesheet, as in Turbopack (webpack copies it).I tried merging small shared modules into one chunk and rejected it: a page would then evaluate modules it never imports.
Inlining is skipped on older Rolldown and with options Rolldown rejects in combination with it:
strictExecutionOrder: false, non-ESM output,preserveModules, strict entry signatures,onDemandWrapping. It's also skipped when the output already configures it.disableNextLikeChunking: truenow turns both changes off. Hybrid App + Pages builds and library builds keep their current chunking, and so does the plain Pages SSR build, which inherits the client output options.Results
JS files per route on
examples/pages-router-complex(production build). Next.js also loads 3 manifest or polyfill files per route on top of these./,/journal,/workbenchAgainst webpack, one extra file per route is
_app's own chunk. The other extra desk-page files are chunks holding CSS modules, kept on purpose, plus a 1 kB chunk that Rolldown declines to inline: it contains animport()of a module that's also inlined elsewhere.Inlining makes Rolldown use strict execution order, which wraps each module. That costs about 1.5 kB gzip per route (1.0–2.8 kB) against main, including on routes that inline nothing. Rolldown rejects inlining with strict execution order turned off and with
onDemandWrapping.Fixture
examples/pages-router-complexgains workbench desks that give the client build a varied chunk graph:next/dynamicwith and without SSR,React.lazy, and on-demandimport();next/image,next/script,next/formandnext/navigation.tests/e2e/pages-router-complex/chunk-scenarios.spec.tschecks each scenario still loads and hydrates. One test checks that a module copied into five page chunks evaluates once across client navigations, and not on a page that doesn't import it.Tests
tests/pages-client-chunks.test.tsbuilds the newtests/fixtures/pages-client-chunksclient and checks several things:_appkeeps its own chunk, which imports the one chunk holding a module_appshares with a page, even when a module_appimports imports_appback, and the client entry doesn't load that chunk when_appshares a module with instrumentation-client; an import that usesnext/linkstill joins it;_appuses joins that chunk;_app's imports alone;disableNextLikeChunkingleaves all of this to the bundler.It also unit-tests picking
_app's imports (including leaving out imports that lead back to_app, to animport()target or to a module the client entry loads, but not ones that only share vinext's first-group helpers with it), the Rolldown version check and the inlining guards.tests/build-optimization.test.tschecks that the SSR-manifest backfill lists the stylesheets of statically imported chunks, deepest first and ahead of the chunk's own.tests/pages-css-order.test.tsnow compares cascade positions across the linked stylesheets, since_app's CSS and a component CSS module it shares with a page now share one stylesheet, in import order.chunk-scenarios.spec.tsruns in CI againstvp devand, as the newpages-router-complex-prodproject, against a production build.Performance
Medians of 5 alternating runs, main vs branch:
vp build,examples/pages-router-complexvp build,tests/fixtures/pages-basicvp devcold start to first/response,examples/pages-router-complexDev is unaffected: the plugin only runs in builds. These timings were measured before the review fixes, which only change which modules the
pages-appgroup starts from.