Route CSS removed on first client-side navigation and never restored (client manifest is pruned to SSR'd matches)

オープン
#8,224 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

  • #8225 @breken-ai による — マージされずにクローズ

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
72/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
react, typescript
領域
frontend, web-dev

調査の方向性

まず、router-core/dist/esm/ssr/ssr-server.js でのマニフェストの脱水化、load-client.js でのクライアント割り当て、react-router/dist/esm/headContentUtils.js での CSS の検索・削除を追跡します。SSR ルートを読み込み、クライアント側で共有コンポーネントのルートに移動して本番ビルドを再現します。CSS の分割を無効にせず、ナビゲーション後も共有 CSS リンクが利用可能なままになるか、復元されれば完了です。

索引モデルが issue の本文から書いたものです。

説明

Summary

On the first client-side navigation away from the server-rendered route, HeadContent removes that route's manifest CSS <link> elements. If the incoming route reuses the same chunk — a component rendered by both routes — nothing re-declares those stylesheets and nothing re-injects them, so the page keeps its class names and loses every rule.

The result looks like a stylesheet that failed to load, but DevTools shows every .css at 200. They loaded, and then their <link> elements were deleted.

Versions

@tanstack/react-start   1.168.49   (latest)
@tanstack/react-router  1.170.32   (latest)
@tanstack/router-core   1.171.27   (latest)
vite 7.3.6 · react 18.3.1 · SSR via Nitro

Reproduction

  1. A component <Dashboard /> with a CSS-module import, rendered by two routes:
    • / (index)
    • /pool (a layout route, parent of /pool/$id)
  2. Build for production and load / directly, so it is server-rendered.
  3. Client-side navigate to /pool/$id.

Dashboard's stylesheet is removed from <head> and never restored.

It does not reproduce if you arrive at / by client-side navigation from some other route — then the CSS came from Vite's runtime injection, which the router doesn't own and therefore doesn't remove. That asymmetry is what makes it look intermittent in the wild.

What's happening

  1. getStartManifest() returns assets for all routes.
  2. serverSsr.dehydrate() prunes to only the matched routes before sending to the client — router-core/dist/esm/ssr/ssr-server.js:301, via getPreparedMatchedManifestRoutes → prepareMatchedManifestRoutes (:145-161, which iterates matches only).
  3. The client assigns that pruned object as the whole manifest — load-client.js:1075:
    router.ssr = { manifest: dehydratedRouter.manifest };
    
  4. HeadContent looks up manifest.routes[match.routeId]?.css (react-router/dist/esm/headContentUtils.js:65). For any route not in the original SSR match set this is undefined, so no CSS tags are produced.
  5. React unmounts the outgoing route's <link> elements — correctly, they're its own keyed children.
  6. Vite does not re-inject, because the chunk is already in its module cache.

Observed client-side after loading /:

routes in client manifest: 2 -> __root__, /
  "/"     css: TooltipButton, GalleryIcon, FocusReveal, BookmarkDashboard
  "/pool" css: (NONE)

The server-side manifest has all 34 routes, and /pool there correctly lists all four stylesheets. Only the client's copy is missing them.

MutationObserver on <head> across the navigation, bracketed by the title swap:

12947ms add    LINK stylesheet    pool-PDzBNjqJ.css        <- Vite, for the newly loaded chunk
13464ms REMOVE TITLE
13464ms REMOVE LINK modulepreload  ...16 entries...
13464ms REMOVE LINK stylesheet    TooltipButton-Dla7QkuC.css
13464ms REMOVE LINK stylesheet    GalleryIcon-BwNEuhX0.css
13464ms REMOVE LINK stylesheet    FocusReveal-CakzEOUS.css
13464ms REMOVE LINK stylesheet    BookmarkDashboard-5xTt8aTr.css
13464ms add    TITLE

All three matches are success at that moment (__root__, /pool, /pool/$idSlug), so _getAssetMatches truncation is not involved.

Elements keep their hashed class names (_searchContainer_15j3j_55); the rules are simply gone — padding: 20px 20px 15px → 0px, display: flex → block.

Expected

Either:

  • the dehydrated manifest carries assets for all routes (or is extended as routes load), so HeadContent can re-declare a stylesheet a newly matched route needs; or
  • HeadContent does not remove a stylesheet <link> it cannot guarantee re-adding.

The current combination — prune the client manifest to the SSR'd matches, then remove head assets by route on navigation — means any chunk shared between an SSR'd route and a later-visited route loses its CSS.

Workaround

build.cssCodeSplit: false. One stylesheet, which the manifest attaches to __root__ — always matched, never unmounted, so there is nothing to tear down. Costs ~23 KB gzipped on first load for us.

Note

Filed from a production incident. Happy to put together a minimal standalone reproduction if that would help — the shape above is the whole of it, and the internal trace is included so it can be verified by inspection in the meantime.

主要言語
TypeScript
スター
15.3k
フォーク
1.9k
平均マージ
1日 5時間
マージ済み PR(30日)
46

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

TanStack/router のほかの issue

TanStack/router の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。