Skip to content
Source
Sign inSign up
TanStack/router

Start RSC: a chunk in both the request assets and a route's preloads is preloaded twice (head and dehydrated manifest) #8726

Closedhavalex(GitHub profile, opens in a new tab) opened this issue 0 comments

Which project does this relate to?

Start with RSC (@tanstack/react-start 1.169.2, @tanstack/react-start-rsc 0.1.63, @tanstack/react-router 1.170.44, @tanstack/router-core 1.171.37)

Describe the bug

react-start-rsc's server serialization adds the JS deps of each client reference the render touches to ctx.requestAssets.preloads (setOnClientReference in serialization.server.js). It deduplicates against the request assets only, not against the manifest's route preloads. Two places then concatenate the lists without deduplicating:

  1. mergeRequestAssetsIntoRootRoute (router-core/src/ssr/ssr-server.ts): [...requestAssets.preloads, ...rootRoute.preloads]. A chunk in both (a shared vendor chunk, a client reference the root route also imports) is listed twice in the dehydrated manifest.
  2. buildTagsFromMatches (react-router/src/headContentUtils.tsx) emits one <link rel="modulepreload"> per preload of every matched route. A chunk preloaded by the root and by a child route, or present twice in the root's merged list, gets two identical links.

The browser fetches once, but the HTML and the dehydrated manifest carry the duplicates, and both server and client build the same doubled list.

Expected behavior

One preload per href, in first-occurrence order (request assets first, then routes in match order).

Proposed fix

 // router-core ssr-server.ts, mergeRequestAssetsIntoRootRoute
-const preloads = requestAssets?.preloads?.length
-  ? [...requestAssets.preloads, ...(rootRoute?.preloads ?? [])]
-  : rootRoute?.preloads
+const seen = new Set<string>()
+const preloads = requestAssets?.preloads?.length
+  ? [...requestAssets.preloads, ...(rootRoute?.preloads ?? [])].filter((preload) => {
+      const href = typeof preload === 'string' ? preload : preload.href
+      if (seen.has(href)) return false
+      seen.add(href)
+      return true
+    })
+  : rootRoute?.preloads
 // react-router headContentUtils.tsx, buildTagsFromMatches
 const preloadLinks = []
+const preloadHrefs = new Set<string>()
 if (manifest) matches.forEach((match) => {
   manifest.routes[match.routeId]?.preloads?.forEach((preload) => {
+    const href = typeof preload === 'string' ? preload : preload.href
+    if (preloadHrefs.has(href)) return
+    preloadHrefs.add(href)
     preloadLinks.push(/* … */)

The same probably applies to solid-router / vue-router's copies of headContentUtils. We run both hunks as a local patch in production. Happy to open a PR with tests.

Sign in to join this conversation.