Skip to content
Source
Sign inSign up
TanStack/router

[Start] With Vite 8 (Rolldown), route preloads only include direct chunk imports, so second-level chunks load as a request waterfall #8511

Send to my agent
ClosedSnevaum(GitHub profile, opens in a new tab) opened this issue 3 comments

Which project does this relate to?

Start

Describe the bug

With Vite 8 (Rolldown), route preloads only include a chunk's direct imports, so second-level chunks load as a request waterfall.

getChunkPreloads in @tanstack/start-plugin-core (dist/esm/start-manifest-plugin/manifestBuilder.js) builds the modulepreload list one level deep:

const preloads = [getAssetPath(chunk.fileName)];
for (let i = 0; i < chunk.imports.length; i++) preloads.push(getAssetPath(chunk.imports[i]));

Under Vite 7, Rollup hoisted transitive static imports into each chunk's imports, so one level covered everything. Rolldown lists only direct imports in chunk.imports. For entry → a → b, only a is preloaded. The browser discovers b after a downloads and executes, which adds a network round trip before first render.

CSS collection in the same file (createChunkCssAssetCollector) already walks the import graph recursively, so CSS and JS preloads now disagree.

The same issue was fixed in Symfony's Vite integration: https://github.com/symfony/reprise/pull/137

Complete minimal reproducer

I can't share the app, but the defect is visible in the source above and easy to confirm on any Start + Vite 8 build. If a standalone reproducer would help, I can put one together.

Steps to Reproduce the Bug

  1. TanStack Start with Vite 8.3.1 (@tanstack/react-start@1.168.57, @tanstack/start-plugin-core@1.171.47). Any route whose chunk statically imports a shared chunk that itself imports another chunk.
  2. vite build, then read the modulepreload links the server renders (or the prerendered SPA shell).
  3. For each preloaded chunk, collect its static import … from "./x.js". Some of those chunks are not in the preload list. In our app, 4 chunks imported by the index route's chunk were missing. With Rolldown's default chunking, 10 were missing.

Expected behavior

A route's preload list includes the transitive closure of its static imports, as it effectively did with Vite 7/Rollup. Dynamic imports stay out.

Platform

  • OS: Linux
  • Browser: Chrome (headless, CDP network throttling)
  • Deployed on Cloudflare Workers
  • Versions: @tanstack/react-start@1.168.57, @tanstack/start-plugin-core@1.171.47, vite@8.3.1

Additional context

Impact. Cold first load on "Slow 4G" (150 ms RTT, 1.6 Mbps). The same build with the fix below reached interactive ~0.4 s sooner.

Fix we ship via bun patch.

-function createManifestAssetResolvers(basePath) {
+function createManifestAssetResolvers(basePath, chunksByFileName) {
 ...
 	const preloads = [getAssetPath(chunk.fileName)];
-	for (let i = 0; i < chunk.imports.length; i++) preloads.push(getAssetPath(chunk.imports[i]));
+	const seen = new Set([chunk.fileName]);
+	const queue = [...chunk.imports];
+	while (queue.length > 0) {
+		const name = queue.shift();
+		if (seen.has(name)) continue;
+		seen.add(name);
+		preloads.push(getAssetPath(name));
+		const imported = chunksByFileName?.get(name);
+		if (imported) queue.push(...imported.imports);
+	}

The caller in buildStartManifest passes scannedChunks.chunksByFileName. Dynamic imports aren't in chunk.imports, so lazy route chunks are not pulled in. Happy to open a PR if you'd like this approach.

  1. please provide a complete minimal reproducer project

  2. Here's a standalone reproducer: https://github.com/Snevaum/tanstack-start-vite8-preload-repro

    npm install && npm run build && npm run check: the prerendered shell preloads routes → a but not b, which a imports statically. The README also shows that the same app passes on Vite 7 (Rollup hoists the import), and that it passes on Vite 8 with the proposed patch (npm run fix:apply).

    Still reproduces on the latest versions (@tanstack/react-start@1.168.58, start-plugin-core@1.171.47, vite@8.3.1).

  3. We see the same thing on a production Start app with Vite 8.3.0 (Rolldown 1.2.8), @tanstack/react-start 1.168.27 and @tanstack/start-plugin-core 1.171.19, with about 250 client chunks.

    For each route chain we took the manifest preloads and the static-import closure of those chunks. Between 20 and 35 chunks of each first page load (31 to 55 KB gzip) were not in the preload list, so the browser requested them one or more round trips late. With the change from #8520 applied as a local patch, every route of the app (23 routes) has 0 statically imported chunks without a preload, and the preload lists match the closure exactly. Dynamic imports stay out.

    +1 for #8520.

Sign in to join this conversation.