data-tsd-source inject-source plugin causes hydration mismatch in SSR (TanStack Start)
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- TypeScript
- Star
- 499
- Fork
- 100
- Merge trung bình
- 1 ngày 17 giờ
- Pull request đã merge (30 ngày)
- 4
Mô tả
Description
The inject-source plugin in @tanstack/devtools-vite causes React hydration mismatch warnings in TanStack Start (SSR) projects. The data-tsd-source attributes are injected with different line numbers on the server vs client transform passes, because other Vite plugins (e.g. tanstackStart(), viteReact()) shift line numbers differently between SSR and client bundles.
Reproduction
- Create a TanStack Start project with
@tanstack/devtools-vitev0.6.0 - Add
devtools()tovite.config.ts - Navigate to any SSR-rendered route
- Observe console warning:
A tree hydrated but some attributes of the server rendered HTML didn't match the client properties.
<div
+ data-tsd-source="/src/routes/simulations/$slug/editor.tsx:632:5"
- data-tsd-source="/src/routes/simulations/$slug/editor.tsx:636:5"
>
Line numbers are consistently off by a fixed offset (4 lines in my case) — the server and client transforms produce different source locations for the same JSX elements.
Root Cause
In plugin.ts, the inject-source Vite plugin's apply function only checks config.mode === 'development':
apply(config) {
return config.mode === 'development' && injectSourceConfig.enabled
},
The transform hook does not check the ssr boolean that Vite passes as the third argument. Since data-tsd-source is only useful on the client (for click-to-source in the devtools UI), injecting it during the SSR pass creates the mismatch.
Suggested Fix
Skip source injection during the SSR transform pass. The Vite transform hook receives { ssr: boolean } as the third argument:
transform(code, id, options) {
if (options?.ssr) return; // Only inject on client
// ... existing Babel transform
}
This would eliminate the hydration mismatch while preserving click-to-source functionality on the client.
Workaround
Disable source injection entirely:
devtools({ injectSource: { enabled: false } })
This removes the hydration warning but loses click-to-source in the devtools UI.
Environment
@tanstack/devtools-vite: 0.6.0@tanstack/react-start: latest- React 19
- Vite 7
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu trong plugin.ts và kiểm tra hook transform của plugin Vite inject-source, bao gồm tùy chọn ssr được Vite truyền vào. Bỏ qua việc chèn source đối với các phép transform SSR nhưng vẫn giữ việc chèn ở phía client, sau đó xác minh rằng hydration mismatch được báo cáo không còn xảy ra và click-to-source vẫn khả dụng.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- react, typescript
- Lĩnh vực
- devtools
- Loại issue
- Lỗi
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức độ hoạt động
- Ít trao đổi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức phù hợp với người mới
- 78/100